From c1fa9e595c4890a027505af1b441239960300f13 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 6 May 2023 14:58:32 +0300 Subject: [PATCH] Sort profiler window assets when the view is active --- Source/Editor/Windows/Profiler/Assets.cs | 27 +++++++++------------ Source/Editor/Windows/Profiler/MemoryGPU.cs | 23 ++++++++---------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/Source/Editor/Windows/Profiler/Assets.cs b/Source/Editor/Windows/Profiler/Assets.cs index 6987a09e0..1856ff7a1 100644 --- a/Source/Editor/Windows/Profiler/Assets.cs +++ b/Source/Editor/Windows/Profiler/Assets.cs @@ -8,6 +8,7 @@ using FlaxEngine; using FlaxEngine.Json; using FlaxEngine.GUI; using FlaxEditor.GUI.ContextMenu; +using System.Linq; namespace FlaxEditor.Windows.Profiler { @@ -17,7 +18,7 @@ namespace FlaxEditor.Windows.Profiler /// internal sealed class Assets : ProfilerMode { - private struct Resource + private class Resource { public string Name; public string TypeName; @@ -120,19 +121,18 @@ namespace FlaxEditor.Windows.Profiler // Capture current assets usage info var assets = FlaxEngine.Content.Assets; - var sb = _stringBuilder; var resources = new Resource[assets.Length]; - var contentDatabase = Editor.Instance.ContentDatabase; ulong totalMemoryUsage = 0; for (int i = 0; i < resources.Length; i++) { var asset = assets[i]; + ref var resource = ref resources[i]; if (!asset) continue; // Try to reuse cached resource info var assetId = asset.ID; - if (!_resourceCache.TryGetValue(assetId, out var resource)) + if (!_resourceCache.TryGetValue(assetId, out resource)) { resource = new Resource { @@ -154,12 +154,10 @@ namespace FlaxEditor.Windows.Profiler } resource.MemoryUsage = asset.MemoryUsage; - totalMemoryUsage += resource.MemoryUsage; resource.ReferencesCount = asset.ReferencesCount; - resources[i] = resource; + totalMemoryUsage += resource.MemoryUsage; } _memoryUsageChart.AddSample((float)totalMemoryUsage); - Array.Sort(resources, SortResources); if (_resources == null) _resources = new SamplesBuffer(); _resources.Add(resources); @@ -188,11 +186,6 @@ namespace FlaxEditor.Windows.Profiler base.OnDestroy(); } - private static int SortResources(Resource a, Resource b) - { - return (int)(b.MemoryUsage - a.MemoryUsage); - } - private void UpdateTable() { _table.IsLayoutLocked = true; @@ -226,12 +219,13 @@ namespace FlaxEditor.Windows.Profiler if (resources == null || resources.Length == 0) return; + var resourcesOrdered = resources.OrderByDescending(x => x.MemoryUsage); + // Add rows var rowColor2 = Style.Current.Background * 1.4f; - for (int i = 0; i < resources.Length; i++) + int rowIndex = 0; + foreach (var e in resourcesOrdered) { - ref var e = ref resources[i]; - ClickableRow row; if (_tableRowsCache.Count != 0) { @@ -257,8 +251,9 @@ namespace FlaxEditor.Windows.Profiler // Add row to the table row.Width = _table.Width; - row.BackgroundColor = i % 2 == 0 ? rowColor2 : Color.Transparent; + row.BackgroundColor = rowIndex % 2 == 0 ? rowColor2 : Color.Transparent; row.Parent = _table; + rowIndex++; } } diff --git a/Source/Editor/Windows/Profiler/MemoryGPU.cs b/Source/Editor/Windows/Profiler/MemoryGPU.cs index f84e4b697..d6c0e1b08 100644 --- a/Source/Editor/Windows/Profiler/MemoryGPU.cs +++ b/Source/Editor/Windows/Profiler/MemoryGPU.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using FlaxEditor.GUI; using FlaxEngine; @@ -15,7 +16,7 @@ namespace FlaxEditor.Windows.Profiler /// internal sealed class MemoryGPU : ProfilerMode { - private struct Resource + private class Resource { public string Name; public string Tooltip; @@ -126,10 +127,11 @@ namespace FlaxEditor.Windows.Profiler for (int i = 0; i < resources.Length; i++) { var gpuResource = gpuResources[i]; + ref var resource = ref resources[i]; // Try to reuse cached resource info var gpuResourceId = gpuResource.ID; - if (!_resourceCache.TryGetValue(gpuResourceId, out var resource)) + if (!_resourceCache.TryGetValue(gpuResourceId, out resource)) { resource = new Resource { @@ -194,9 +196,7 @@ namespace FlaxEditor.Windows.Profiler resource.MemoryUsage = gpuResource.MemoryUsage; if (resource.MemoryUsage == 1) resource.MemoryUsage = 0; // Sometimes GPU backend fakes memory usage as 1 to mark as allocated but not resided in actual GPU memory - resources[i] = resource; } - Array.Sort(resources, SortResources); if (_resources == null) _resources = new SamplesBuffer(); _resources.Add(resources); @@ -240,11 +240,6 @@ namespace FlaxEditor.Windows.Profiler base.OnDestroy(); } - private static int SortResources(Resource a, Resource b) - { - return (int)(b.MemoryUsage - a.MemoryUsage); - } - private void UpdateTable() { _table.IsLayoutLocked = true; @@ -278,12 +273,13 @@ namespace FlaxEditor.Windows.Profiler if (resources == null || resources.Length == 0) return; + var resourcesOrdered = resources.OrderByDescending(x => x.MemoryUsage); + // Add rows var rowColor2 = Style.Current.Background * 1.4f; - for (int i = 0; i < resources.Length; i++) + int rowIndex = 0; + foreach (var e in resourcesOrdered) { - ref var e = ref resources[i]; - ClickableRow row; if (_tableRowsCache.Count != 0) { @@ -314,8 +310,9 @@ namespace FlaxEditor.Windows.Profiler // Add row to the table row.Width = _table.Width; - row.BackgroundColor = i % 2 == 0 ? rowColor2 : Color.Transparent; + row.BackgroundColor = rowIndex % 2 == 0 ? rowColor2 : Color.Transparent; row.Parent = _table; + rowIndex++; } }