chore: improve PropCache debuggability by adding a name property (#1209)

This commit is contained in:
bd_ 2024-09-25 20:01:59 -07:00 committed by GitHub
parent 13b0ffe0b5
commit a018df9219
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 17 additions and 12 deletions

View File

@ -25,10 +25,11 @@ namespace nadena.dev.modular_avatar.core.editor
internal static class ParameterIntrospectionCache internal static class ParameterIntrospectionCache
{ {
internal static PropCache<GameObject, ImmutableList<ProvidedParameter>> ProvidedParameterCache = new (GetParametersForObject_miss); internal static PropCache<GameObject, ImmutableList<ProvidedParameter>> ProvidedParameterCache =
new("GetParametersForObject", GetParametersForObject_miss);
internal static PropCache<GameObject, ImmutableDictionary<(ParameterNamespace, string), ParameterMapping>> internal static PropCache<GameObject, ImmutableDictionary<(ParameterNamespace, string), ParameterMapping>>
ParameterRemappingCache = new(GetParameterRemappingsAt_miss); ParameterRemappingCache = new("GetParameterRemappingsAt", GetParameterRemappingsAt_miss);
private static ImmutableList<ProvidedParameter> GetParametersForObject_miss(ComputeContext ctx, GameObject obj) private static ImmutableList<ProvidedParameter> GetParametersForObject_miss(ComputeContext ctx, GameObject obj)
{ {

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using nadena.dev.ndmf.preview; using nadena.dev.ndmf.preview;
using UnityEngine;
namespace nadena.dev.modular_avatar.core.editor namespace nadena.dev.modular_avatar.core.editor
{ {
@ -13,21 +12,25 @@ namespace nadena.dev.modular_avatar.core.editor
public PropCache<Key, Value> Owner; public PropCache<Key, Value> Owner;
public Key Key; public Key Key;
public Value Value; public Value Value;
public string DebugName;
} }
private readonly string _debugName;
private readonly Func<ComputeContext, Key, Value> _operator; private readonly Func<ComputeContext, Key, Value> _operator;
private readonly Func<Value, Value, bool> _equalityComparer; private readonly Func<Value, Value, bool> _equalityComparer;
private readonly Dictionary<Key, CacheEntry> _cache = new(); private readonly Dictionary<Key, CacheEntry> _cache = new();
public PropCache(Func<ComputeContext, Key, Value> operatorFunc, Func<Value, Value, bool> equalityComparer = null) public PropCache(string debugName, Func<ComputeContext, Key, Value> operatorFunc,
Func<Value, Value, bool> equalityComparer = null)
{ {
_debugName = debugName;
_operator = operatorFunc; _operator = operatorFunc;
_equalityComparer = equalityComparer; _equalityComparer = equalityComparer;
} }
private static void InvalidateEntry(CacheEntry entry) 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); var newValue = entry.Owner._operator(newGenContext, entry.Key);
if (entry.Owner._equalityComparer != null && entry.Owner._equalityComparer(entry.Value, newValue)) 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) 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 entry = new CacheEntry
{ {
GenerateContext = subContext, GenerateContext = subContext,
ObserverContext = new ComputeContext("Observer for PropCache for key " + key), ObserverContext = new ComputeContext("Observer for PropCache for key " + key),
Owner = this, Owner = this,
Key = key, Key = key,
Value = _operator(subContext, key) Value = _operator(subContext, key),
DebugName = _debugName
}; };
_cache[key] = entry; _cache[key] = entry;

View File

@ -67,7 +67,7 @@ namespace nadena.dev.modular_avatar.core.editor
{ {
if (_analysisCache == null) if (_analysisCache == null)
{ {
_analysisCache = new PropCache<GameObject, AnalysisResult>((ctx, root) => _analysisCache = new PropCache<GameObject, AnalysisResult>("ROAnalyzer", (ctx, root) =>
{ {
var analysis = new ReactiveObjectAnalyzer(ctx); var analysis = new ReactiveObjectAnalyzer(ctx);
analysis.ForcePropertyOverrides = ctx.Observe(ROSimulator.PropertyOverrides, a=>a, (a,b) => false) analysis.ForcePropertyOverrides = ctx.Observe(ROSimulator.PropertyOverrides, a=>a, (a,b) => false)

View File

@ -28,7 +28,7 @@ namespace nadena.dev.modular_avatar.core.editor
private const string PREFIX = "m_Materials.Array.data["; private const string PREFIX = "m_Materials.Array.data[";
private PropCache<Renderer, ImmutableList<(int, Material)>> _cache = new( private PropCache<Renderer, ImmutableList<(int, Material)>> _cache = new(
GetMaterialOverridesForRenderer, Enumerable.SequenceEqual "GetMaterialOverridesForRenderer", GetMaterialOverridesForRenderer, Enumerable.SequenceEqual
); );
private static ImmutableList<(int, Material)> GetMaterialOverridesForRenderer(ComputeContext ctx, Renderer r) private static ImmutableList<(int, Material)> GetMaterialOverridesForRenderer(ComputeContext ctx, Renderer r)

View File

@ -60,8 +60,8 @@ namespace nadena.dev.modular_avatar.core.editor
} }
} }
private PropCache<GameObject, ImmutableDictionary<SkinnedMeshRenderer, ImmutableList<(int, float)>>> private readonly PropCache<GameObject, ImmutableDictionary<SkinnedMeshRenderer, ImmutableList<(int, float)>>>
_blendshapeCache = new(ShapesForAvatar); _blendshapeCache = new("ShapesForAvatar", ShapesForAvatar);
private static ImmutableDictionary<SkinnedMeshRenderer, ImmutableList<(int, float)>> ShapesForAvatar(ComputeContext context, GameObject avatarRoot) private static ImmutableDictionary<SkinnedMeshRenderer, ImmutableList<(int, float)>> ShapesForAvatar(ComputeContext context, GameObject avatarRoot)
{ {

View File

@ -19,7 +19,7 @@ namespace UnitTests.PropCacheTest
int seq = 0; int seq = 0;
Dictionary<int, List<WeakReference<ComputeContext>>> invalidators = new(); Dictionary<int, List<WeakReference<ComputeContext>>> invalidators = new();
PropCache<int,int> cache = new PropCache<int, int>((ctx, k) => PropCache<int,int> cache = new PropCache<int, int>("test", (ctx, k) =>
{ {
Debug.Log("Generating value for " + k); Debug.Log("Generating value for " + k);
if (!invalidators.TryGetValue(k, out var list)) if (!invalidators.TryGetValue(k, out var list))