From a018df921963277f468cee6d6135ddfbcda4b4ca Mon Sep 17 00:00:00 2001 From: bd_ Date: Wed, 25 Sep 2024 20:01:59 -0700 Subject: [PATCH] chore: improve PropCache debuggability by adding a name property (#1209) --- Editor/Inspector/Menu/MenuItemGUI.cs | 5 +++-- .../AnimationGeneration/PropCache.cs | 14 +++++++++----- .../AnimationGeneration/ReactiveObjectAnalyzer.cs | 2 +- Editor/ReactiveObjects/MaterialSetterPreview.cs | 2 +- Editor/ReactiveObjects/ShapeChangerPreview.cs | 4 ++-- UnitTests~/PropCacheTest/PropCacheTest.cs | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Editor/Inspector/Menu/MenuItemGUI.cs b/Editor/Inspector/Menu/MenuItemGUI.cs index 29c75b63..c89c3817 100644 --- a/Editor/Inspector/Menu/MenuItemGUI.cs +++ b/Editor/Inspector/Menu/MenuItemGUI.cs @@ -25,10 +25,11 @@ namespace nadena.dev.modular_avatar.core.editor internal static class ParameterIntrospectionCache { - internal static PropCache> ProvidedParameterCache = new (GetParametersForObject_miss); + internal static PropCache> ProvidedParameterCache = + new("GetParametersForObject", GetParametersForObject_miss); internal static PropCache> - ParameterRemappingCache = new(GetParameterRemappingsAt_miss); + ParameterRemappingCache = new("GetParameterRemappingsAt", GetParameterRemappingsAt_miss); private static ImmutableList GetParametersForObject_miss(ComputeContext ctx, GameObject obj) { diff --git a/Editor/ReactiveObjects/AnimationGeneration/PropCache.cs b/Editor/ReactiveObjects/AnimationGeneration/PropCache.cs index 63fc36ef..e0063697 100644 --- a/Editor/ReactiveObjects/AnimationGeneration/PropCache.cs +++ b/Editor/ReactiveObjects/AnimationGeneration/PropCache.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using nadena.dev.ndmf.preview; -using UnityEngine; namespace nadena.dev.modular_avatar.core.editor { @@ -13,21 +12,25 @@ namespace nadena.dev.modular_avatar.core.editor public PropCache Owner; public Key Key; public Value Value; + public string DebugName; } + private readonly string _debugName; private readonly Func _operator; private readonly Func _equalityComparer; private readonly Dictionary _cache = new(); - public PropCache(Func operatorFunc, Func equalityComparer = null) + public PropCache(string debugName, Func operatorFunc, + Func equalityComparer = null) { + _debugName = debugName; _operator = operatorFunc; _equalityComparer = equalityComparer; } private static void InvalidateEntry(CacheEntry entry) { - var newGenContext = new ComputeContext("PropCache for key " + entry.Key); + var newGenContext = new ComputeContext("PropCache/" + entry.DebugName + " key " + entry.Key); var newValue = entry.Owner._operator(newGenContext, entry.Key); if (entry.Owner._equalityComparer != null && entry.Owner._equalityComparer(entry.Value, newValue)) { @@ -44,14 +47,15 @@ namespace nadena.dev.modular_avatar.core.editor { if (!_cache.TryGetValue(key, out var entry) || entry.GenerateContext.IsInvalidated) { - var subContext = new ComputeContext("PropCache for key " + key); + var subContext = new ComputeContext("PropCache/" + _debugName + " key " + key); entry = new CacheEntry { GenerateContext = subContext, ObserverContext = new ComputeContext("Observer for PropCache for key " + key), Owner = this, Key = key, - Value = _operator(subContext, key) + Value = _operator(subContext, key), + DebugName = _debugName }; _cache[key] = entry; diff --git a/Editor/ReactiveObjects/AnimationGeneration/ReactiveObjectAnalyzer.cs b/Editor/ReactiveObjects/AnimationGeneration/ReactiveObjectAnalyzer.cs index 096c3e94..42f11821 100644 --- a/Editor/ReactiveObjects/AnimationGeneration/ReactiveObjectAnalyzer.cs +++ b/Editor/ReactiveObjects/AnimationGeneration/ReactiveObjectAnalyzer.cs @@ -67,7 +67,7 @@ namespace nadena.dev.modular_avatar.core.editor { if (_analysisCache == null) { - _analysisCache = new PropCache((ctx, root) => + _analysisCache = new PropCache("ROAnalyzer", (ctx, root) => { var analysis = new ReactiveObjectAnalyzer(ctx); analysis.ForcePropertyOverrides = ctx.Observe(ROSimulator.PropertyOverrides, a=>a, (a,b) => false) diff --git a/Editor/ReactiveObjects/MaterialSetterPreview.cs b/Editor/ReactiveObjects/MaterialSetterPreview.cs index 29f0b07d..a181429d 100644 --- a/Editor/ReactiveObjects/MaterialSetterPreview.cs +++ b/Editor/ReactiveObjects/MaterialSetterPreview.cs @@ -28,7 +28,7 @@ namespace nadena.dev.modular_avatar.core.editor private const string PREFIX = "m_Materials.Array.data["; private PropCache> _cache = new( - GetMaterialOverridesForRenderer, Enumerable.SequenceEqual + "GetMaterialOverridesForRenderer", GetMaterialOverridesForRenderer, Enumerable.SequenceEqual ); private static ImmutableList<(int, Material)> GetMaterialOverridesForRenderer(ComputeContext ctx, Renderer r) diff --git a/Editor/ReactiveObjects/ShapeChangerPreview.cs b/Editor/ReactiveObjects/ShapeChangerPreview.cs index 912fd99b..4977053c 100644 --- a/Editor/ReactiveObjects/ShapeChangerPreview.cs +++ b/Editor/ReactiveObjects/ShapeChangerPreview.cs @@ -60,8 +60,8 @@ namespace nadena.dev.modular_avatar.core.editor } } - private PropCache>> - _blendshapeCache = new(ShapesForAvatar); + private readonly PropCache>> + _blendshapeCache = new("ShapesForAvatar", ShapesForAvatar); private static ImmutableDictionary> ShapesForAvatar(ComputeContext context, GameObject avatarRoot) { diff --git a/UnitTests~/PropCacheTest/PropCacheTest.cs b/UnitTests~/PropCacheTest/PropCacheTest.cs index 2b63161c..2f0c9c5f 100644 --- a/UnitTests~/PropCacheTest/PropCacheTest.cs +++ b/UnitTests~/PropCacheTest/PropCacheTest.cs @@ -19,7 +19,7 @@ namespace UnitTests.PropCacheTest int seq = 0; Dictionary>> invalidators = new(); - PropCache cache = new PropCache((ctx, k) => + PropCache cache = new PropCache("test", (ctx, k) => { Debug.Log("Generating value for " + k); if (!invalidators.TryGetValue(k, out var list))