perf: 83-88% faster template compilation via NoInlining JIT entry points - #668
Merged
Conversation
…writes and helper-literal dispatch
Dynamic methods are JIT-compiled at CreateDelegate, and the JIT honored
AggressiveInlining on EncodedTextWriter.Write<object> and the helper
options/context/arguments construction, inline-expanding that machinery into
every mustache call site of every compiled template. Emit thin NoInlining
static entry points instead (JIT-compiled once per process):
- EncodedTextWriter.WriteObjectTo for {{expr}} statement writes
- CompiledHelperInvokers.WriteInvoke/Invoke for zero-argument helper literals
Compilation: 10.80 -> 1.75 ms (-84%). CompileMany: N=10 57.7 -> 5.8 ms,
N=100 537.9 -> 69.4 ms (-87-90%). Probe: 40-statement dotted-path template
13.1 -> 1.5 ms/compile.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The NoInlining entry point for zero-argument helper-literal statements
({{name}}) saved compile time but cost ~2ns per value per render on the
hottest render shape (RenderToString clean +4%). The interface dispatch in
that route cannot be inline-expanded by the JIT anyway, so its compile cost
is moderate; keep it inline and retain the NoInlining entry points only
where they are render-neutral (WriteObjectTo, path-expression Invoke).
Render guardrail after this: RenderToString clean/html within noise of the
render branch; Compilation 1.85ms (-83% vs 10.80ms), CompileMany N=100
78.1ms (-85% vs 537.9ms).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Follow-up to #667, targeting the compile path. Template compilation is 83–88% faster; render performance is verified unchanged (that was a hard constraint — one intermediate change that cost ~4% on RenderToString was found by the guardrail benchmarks and walked back).
Results
All 1912 tests pass after each commit.
Root cause
Profiling showed ~95–99% of compile time inside
Expression.Compile()→LambdaCompiler.CreateDelegate, mostly unmanaged — the Handlebars visitor pipeline is under 4%. Synthetic-tree bisection isolated the driver: dynamic methods are JIT-compiled eagerly atCreateDelegate, and the JIT honors[AggressiveInlining]onEncodedTextWriter.Write<object>and the type-switch/encoder machinery behind it — inline-expanding all of it into every mustache call site of every template. A 40-statement template calling an empty-bodied struct method compiles in 0.07 ms; the identical tree shape callingWrite(object)took 2.9 ms. Template compile cost tracks the fatness of inlined callees, not tree size.The change
Emit thin
[MethodImpl(NoInlining)]static entry points, JIT-compiled once per process instead of re-inlined into every template:EncodedTextWriter.WriteObjectTo(in writer, object?)for{{expr}}statement writes (probe: 40×{{a.b}}13.1 → 1.5 ms/compile)CompiledHelperInvokers.Invokefor helper-literal path expressionsZero-argument helper-literal statements (
{{name}}— the hottest render shape) deliberately keep their inline dispatch: routing them through a wrapper cost ~2 ns per value per render (+4% RenderToString clean), and their interface dispatch can't be inline-expanded by the JIT anyway, so their compile cost is moderate. That trade-off is documented in the code.Tried and rejected
IExpressionCompilerbackend: 1315/1912 tests fail withInvalidProgramException— invalid IL for these tree shapes (likely thein-parameter struct delegates). May be worth an upstream issue.Remaining follow-ups (documented, not implemented): the
{{#if}}condition route (~63 µs/statement, auto-inlining of theIsTruthyOrNonEmptychain) and helpers-with-arguments (HelperFunctionBinder) could get the same treatment.🤖 Generated with Claude Code