Skip to content

Commit

Permalink
zzre: Reduce allocations in AssetExplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Oct 20, 2024
1 parent 8752f7c commit a025474
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 19 deletions.
8 changes: 7 additions & 1 deletion zzre.core/assetregistry/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public abstract class Asset : IAsset
/// <summary>The <see cref="ITagContainer"/> of the apparent registry to be used during loading</summary>
protected readonly ITagContainer diContainer;
private readonly TaskCompletionSource completionSource = new();
private string? description;
private AssetHandle[] secondaryAssets = [];
private int refCount;

Expand Down Expand Up @@ -236,5 +237,10 @@ private void EnsureLocality(AssetHandle[] secondaryAssets)

/// <summary>Produces a description of the asset to be shown in debug logs and tools</summary>
/// <returns>A description of the asset instance for debugging</returns>
public override string ToString() => $"{GetType().Name} {ID}";
public override sealed string ToString() => description ??= ToStringInner();

/// <summary>Produces a description of the asset to be shown in debug logs and tools</summary>
/// <remarks>Used to cache description strings</remarks>
/// <returns>A description of the asset instance for debugging</returns>
protected virtual string ToStringInner() => $"{GetType().Name} {ID}";
}
41 changes: 37 additions & 4 deletions zzre.core/imgui/ImGuiEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
Expand Down Expand Up @@ -171,16 +172,16 @@ public static bool Hyperlink(string label, string text, bool addIcon = true, boo
{
if (label.Length > 0)
{
Text(label);
ImGui.Text(label);
SameLine();
}
if (!isEnabled)
{
Text(text);
ImGui.Text(text);
return false;
}
PushStyleColor(ImGuiCol.Text, GetStyle().Colors[(int)ImGuiCol.ButtonHovered]);
Text(text + (addIcon ? " " + IconFonts.ForkAwesome.ExternalLink : ""));
ImGui.Text(text + (addIcon ? " " + IconFonts.ForkAwesome.ExternalLink : ""));
PopStyleColor();
AddUnderLine(GetStyle().Colors[(int)(IsItemHovered()
? IsMouseDown(ImGuiMouseButton.Left)
Expand Down Expand Up @@ -234,7 +235,7 @@ public static bool ColorEdit4(string label, ref FColor color, ImGuiColorEditFlag
public static bool ValueRangeAnimation(string label, ref ValueRangeAnimation a, float min = float.MinValue, float max = float.MaxValue)
{
PushID(label);
Text(label + ':');
ImGui.Text(label + ':');
Indent();
float minValue = a.value - a.width, maxValue = a.value + a.width;
var result = DragFloatRange2("Range", ref minValue, ref maxValue, 1f, min, max);
Expand Down Expand Up @@ -275,4 +276,36 @@ public static bool InputInt(string label, ref byte cur)
cur = (byte)Math.Clamp(curI, byte.MinValue, byte.MaxValue);
return result;
}

private static readonly int GuidCharLength = Guid.Empty.ToString().Length;
public static void Text(in Guid gguid)
{
Span<char> chars = stackalloc char[GuidCharLength];
var success = gguid.TryFormat(chars, out _);
System.Diagnostics.Debug.Assert(success);
ImGui.Text(chars);
}

private static readonly Dictionary<Type, char[][]> enumNames = [];
public static void Text<TEnum>(TEnum value) where TEnum : struct, Enum
{
if (!enumNames.TryGetValue(typeof(TEnum), out var names))
{
var values = Enum.GetValues<TEnum>();
var minValue = values.Min(e => e.GetHashCode());
var maxValue = values.Max(e => e.GetHashCode());
if (minValue != 0 || maxValue - minValue + 1 != values.Length)
throw new ArgumentException($"Enum is not sequential");
names = new char[values.Length][];
foreach (var v in values)
{
if (names[v.GetHashCode()] is not null)
throw new ArgumentException("Enum has duplicates");
names[v.GetHashCode()] = v.ToString().ToCharArray();
}
enumNames.Add(typeof(TEnum), names);
}

ImGui.Text(names[value.GetHashCode()]);
}
}
2 changes: 1 addition & 1 deletion zzre/assets/ActorAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ protected override void Unload()
wingsAnimations = [];
}

public override string ToString() => $"Actor {info.Name}";
protected override string ToStringInner() => $"Actor {info.Name}";
}
2 changes: 1 addition & 1 deletion zzre/assets/AnimationAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ protected override void Unload()
animation = null;
}

public override string ToString() => $"Animation {info.Name}";
protected override string ToStringInner() => $"Animation {info.Name}";
}
2 changes: 1 addition & 1 deletion zzre/assets/ClumpAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override void Unload()
mesh = null;
}

public override string ToString() => $"Clump {info.Name} ({info.Directory})";
protected override string ToStringInner() => $"Clump {info.Name} ({info.Directory})";
}

public static unsafe partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/EffectCombinerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void Unload()
effectCombiner = null;
}

public override string ToString() => $"EffectCombiner {info.Name}";
protected override string ToStringInner() => $"EffectCombiner {info.Name}";
}

partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/EffectMaterialAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected override void Unload()
material = null;
}

public override string ToString() => $"EffectMaterial {DebugName}";
protected override string ToStringInner() => $"EffectMaterial {DebugName}";
}

partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/ModelMaterialAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected override void Unload()
material = null;
}

public override string ToString() => DebugName;
protected override string ToStringInner() => DebugName;

protected abstract IReadOnlyList<FilePath> TextureBasePaths { get; }
protected abstract void SetMaterialVariant(ModelMaterial material);
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/SamplerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void Unload()
sampler = null;
}

public override string ToString() => DebugName;
protected override string ToStringInner() => DebugName;
}

public static unsafe partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/SoundAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected override void Unload()
buffer = null;
}

public override string ToString() => $"SoundAsset {info.FullPath}";
protected override string ToStringInner() => $"SoundAsset {info.FullPath}";
}

partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/TextureAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected override void Unload()
texture = null;
}

public override string ToString() => path.Parts.Count > 1
protected override string ToStringInner() => path.Parts.Count > 1
? $"Texture {path.Parts[^1]} ({path.Parts[^2]})"
: $"Texture {path.ToPOSIXString()}";
}
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/UIBitmapAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected override void Unload()
material = null;
}

public override string ToString() => DebugName;
protected override string ToStringInner() => DebugName;
}

partial class AssetExtensions
Expand Down
2 changes: 1 addition & 1 deletion zzre/assets/WorldAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void Unload()
mesh = null;
}

public override string ToString() => $"World {info.FullPath.Parts[^1]}";
protected override string ToStringInner() => $"World {info.FullPath.Parts[^1]}";
}

public static unsafe partial class AssetExtensions
Expand Down
6 changes: 3 additions & 3 deletions zzre/tools/AssetExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ private void HandleContentFor(IAssetRegistryDebug registry)
{
TableNextRow();
int i = 0;
if (TableSetColumnIndex(i++)) Text(asset.ID.ToString());
if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.ID);
if (TableSetColumnIndex(i++)) Text(asset.Type.Name);
if (TableSetColumnIndex(i++))
if (Selectable(asset.Name, selectedRow == asset.ID, ImGuiSelectableFlags.SpanAllColumns))
selectedRow = asset.ID;
if (TableSetColumnIndex(i++)) Text(asset.State.ToString());
if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.State);
if (TableSetColumnIndex(i++)) Text(asset.RefCount.ToString());
if (TableSetColumnIndex(i++)) Text(asset.Priority.ToString());
if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.Priority);
if (selectedRow == asset.ID && focusSelectedRow)
{
SetScrollHereY();
Expand Down

0 comments on commit a025474

Please sign in to comment.