Optimize context cascade in PDT evaluator.

This commit is contained in:
2022-11-07 12:15:22 +08:00
parent 7e025f9268
commit a401585f07

View File

@@ -32,18 +32,16 @@ namespace Cryville.Crtr {
RevokePotentialConstant(); RevokePotentialConstant();
} }
else { else {
PropSrc.Arbitrary result; PropSrc.Arbitrary result = ContextCascadeLookup(name);
for (int i = ContextCascade.Count - 1; i >= 0; i--) { if (result != null) {
Dictionary<int, PropSrc.Arbitrary> cas = ContextCascade[i];
if (cas.TryGetValue(name, out result)) {
result.Get(out type, out value); result.Get(out type, out value);
return;
}
} }
else {
type = PdtInternalType.Undefined; type = PdtInternalType.Undefined;
LoadNum(name); LoadNum(name);
value = _numbuf; value = _numbuf;
} }
}
return; return;
} }
} }
@@ -97,18 +95,31 @@ namespace Cryville.Crtr {
public Judge ContextJudge { private get; set; } public Judge ContextJudge { private get; set; }
public PropSrc ContextSelfValue { private get; set; } public PropSrc ContextSelfValue { private get; set; }
readonly List<Dictionary<int, PropSrc.Arbitrary>> ContextCascade = new List<Dictionary<int, PropSrc.Arbitrary>>(); readonly Dictionary<int, PropSrc.Arbitrary>[] ContextCascade = new Dictionary<int, PropSrc.Arbitrary>[256];
int _cascadeHeight;
public void ContextCascadeInsert() { public void ContextCascadeInsert() {
ContextCascade.Add(new Dictionary<int, PropSrc.Arbitrary>()); ContextCascade[_cascadeHeight++].Clear();
} }
public void ContextCascadeUpdate(int key, PropSrc.Arbitrary value) { 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<int, PropSrc.Arbitrary> cas = ContextCascade[i];
if (cas.TryGetValue(name, out result)) {
return result;
}
}
return null;
} }
public void ContextCascadeDiscard() { public void ContextCascadeDiscard() {
ContextCascade.RemoveAt(ContextCascade.Count - 1); --_cascadeHeight;
} }
public PdtEvaluator() { public PdtEvaluator() {
for (int i = 0; i < ContextCascade.Length; i++) ContextCascade[i] = new Dictionary<int, PropSrc.Arbitrary>();
_ctxops.Add(IdentifierManager.SharedInstance.Request("screen_edge"), new func_screen_edge(() => ContextTransform)); _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("int"), new func_int(() => ContextSelfValue));
_ctxops.Add(IdentifierManager.SharedInstance.Request("clamp"), new func_clamp(() => ContextSelfValue)); _ctxops.Add(IdentifierManager.SharedInstance.Request("clamp"), new func_clamp(() => ContextSelfValue));