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,17 +32,15 @@ namespace Cryville.Crtr {
RevokePotentialConstant();
}
else {
PropSrc.Arbitrary result;
for (int i = ContextCascade.Count - 1; i >= 0; i--) {
Dictionary<int, PropSrc.Arbitrary> 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<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() {
ContextCascade.Add(new Dictionary<int, PropSrc.Arbitrary>());
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<int, PropSrc.Arbitrary> 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<int, PropSrc.Arbitrary>();
_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));