From a401585f070e44b96052fd4649863eb7c8da5af6 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Mon, 7 Nov 2022 12:15:22 +0800 Subject: [PATCH] Optimize context cascade in PDT evaluator. --- Assets/Cryville/Crtr/PdtEvaluator.cs | 39 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/Assets/Cryville/Crtr/PdtEvaluator.cs b/Assets/Cryville/Crtr/PdtEvaluator.cs index c8de6fa..46a8e54 100644 --- a/Assets/Cryville/Crtr/PdtEvaluator.cs +++ b/Assets/Cryville/Crtr/PdtEvaluator.cs @@ -32,17 +32,15 @@ namespace Cryville.Crtr { RevokePotentialConstant(); } else { - PropSrc.Arbitrary result; - for (int i = ContextCascade.Count - 1; i >= 0; i--) { - Dictionary cas = ContextCascade[i]; - if (cas.TryGetValue(name, out result)) { - result.Get(out type, out value); - return; - } + PropSrc.Arbitrary result = ContextCascadeLookup(name); + if (result != null) { + result.Get(out type, out value); + } + else { + type = PdtInternalType.Undefined; + LoadNum(name); + value = _numbuf; } - type = PdtInternalType.Undefined; - LoadNum(name); - value = _numbuf; } return; } @@ -97,18 +95,31 @@ namespace Cryville.Crtr { public Judge ContextJudge { private get; set; } public PropSrc ContextSelfValue { private get; set; } - readonly List> ContextCascade = new List>(); + readonly Dictionary[] ContextCascade = new Dictionary[256]; + int _cascadeHeight; public void ContextCascadeInsert() { - ContextCascade.Add(new Dictionary()); + ContextCascade[_cascadeHeight++].Clear(); } public void ContextCascadeUpdate(int key, PropSrc.Arbitrary value) { - ContextCascade[ContextCascade.Count - 1][key] = value; + ContextCascade[_cascadeHeight - 1][key] = value; + } + public PropSrc.Arbitrary ContextCascadeLookup(int name) { + PropSrc.Arbitrary result; + for (int i = _cascadeHeight - 1; i >= 0; i--) { + Dictionary cas = ContextCascade[i]; + if (cas.TryGetValue(name, out result)) { + return result; + } + } + return null; } public void ContextCascadeDiscard() { - ContextCascade.RemoveAt(ContextCascade.Count - 1); + --_cascadeHeight; } public PdtEvaluator() { + for (int i = 0; i < ContextCascade.Length; i++) ContextCascade[i] = new Dictionary(); + _ctxops.Add(IdentifierManager.SharedInstance.Request("screen_edge"), new func_screen_edge(() => ContextTransform)); _ctxops.Add(IdentifierManager.SharedInstance.Request("int"), new func_int(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("clamp"), new func_clamp(() => ContextSelfValue));