perf(template-engine): cache script compile verdicts by code hash — unchanged scripts compile once per process

Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
Joseph Doherty
2026-07-09 16:08:03 -04:00
parent f40f52950e
commit ef59c752ba
3 changed files with 126 additions and 10 deletions
@@ -135,4 +135,35 @@ public class ScriptCompilerTests
Assert.NotNull(error);
Assert.Contains("forbidden", error, StringComparison.OrdinalIgnoreCase);
}
// --- Compile-verdict cache: unchanged code compiles once per process ---
[Fact]
public void TryCompile_SameCodeTwice_SecondCallIsCacheHit()
{
ScriptCompileVerdictCache.Clear();
var c = new ScriptCompiler();
c.TryCompile("return 1;", "A");
var hitsBefore = ScriptCompileVerdictCache.Hits;
var r = c.TryCompile("return 1;", "B"); // same code, different name
Assert.True(r.IsSuccess);
Assert.Equal(hitsBefore + 1, ScriptCompileVerdictCache.Hits);
}
[Fact]
public void TryCompile_CachedFailure_FormatsNameForEachCaller()
{
// The stored verdict is name-free; the script name is formatted in at read
// time, so two callers of the same bad code get their own name in the message.
ScriptCompileVerdictCache.Clear();
var c = new ScriptCompiler();
var first = c.TryCompile("var x = NoSuchThing.Foo();", "Alpha");
var second = c.TryCompile("var x = NoSuchThing.Foo();", "Beta"); // cache hit
Assert.True(first.IsFailure);
Assert.True(second.IsFailure);
Assert.Contains("'Alpha'", first.Error);
Assert.Contains("'Beta'", second.Error);
Assert.Contains("compile", second.Error, StringComparison.OrdinalIgnoreCase);
}
}