perf: reuse pre-allocated arrays in TailCall trampoline - #1119
Open
He-Pin wants to merge 1 commit into
Open
Conversation
He-Pin
marked this pull request as draft
August 12, 2026 05:34
He-Pin
force-pushed
the
perf/trampoline-buffer-reuse
branch
from
August 13, 2026 06:31
da42b64 to
0df8c34
Compare
Motivation: Every tail-call position (Apply1/Apply2/Apply3) allocates a fresh Array[Eval](N) per trampoline step. In deep recursion (std.foldl, recursive functions), this creates millions of short-lived arrays, increasing GC pressure and L1/L2 cache pollution. Modification: - Rotating pool of 4 pre-allocated buffers per arity (1/2/3) in Evaluator - nextTailCallBufIdx() cycles through slots with (i+1) & 3 - Buffer is consumed by Func.apply (copies into new scope) before the next TailCall overwrites it — 4 slots provide ample safety margin. Safety: Func.apply copies Eval refs out of the buffer (extendSimple/arraycopy) before evalRhs can produce the next TailCall, so no slot is overwritten while live. - Zero allocation in the hot tail-call loop Result: MainBenchmark.main: 3.028 ± 0.536 → 2.588 ± 0.367 ms/op (-14.5%) JMH config: -f 2 -wi 5 -i 10 -w 1 -r 1 (2 forks × 10 measurement iterations) All 424 tests pass. Thread-safe: Evaluator is single-threaded per interpreter instance (see CLAUDE.md threading model).
He-Pin
force-pushed
the
perf/trampoline-buffer-reuse
branch
from
August 13, 2026 10:19
0df8c34 to
d43b163
Compare
He-Pin
marked this pull request as ready for review
August 13, 2026 10:19
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.
Motivation
Every tail-call position (
Apply1/Apply2/Apply3) allocates a freshArray[Eval](N)per trampoline step. In deep recursion (std.foldl, recursive functions), this creates millions of short-lived arrays, increasing GC pressure and L1/L2 cache pollution.Modification
EvaluatornextTcBufIdx()cycles through slots with(i+1) & 3Func.apply(copies into new scope) before the nextTailCalloverwrites it — 4 slots provide ample safety marginResult
JMH config:
-f 2 -wi 5 -i 10 -w 1 -r 1(2 forks × 10 measurement iterations)All 424 tests pass. Thread-safe:
Evaluatoris single-threaded per interpreter instance (see CLAUDE.md threading model).Test plan
./mill 'sjsonnet.jvm[_]'.test— all pass./mill bench.runJmh ".*MainBenchmark.*"— 14.5% improvementbyterenderer_deep_nesting.jsonnet) passes