Repository navigation
perf(vm): size dispatcher register files from emitted bytecode - #359
Merged
Merged
Conversation
The dispatcher reserved a blanket +16 register-slack buffer on every prototype's register tuple (added in #347 to cover multi-return constructor tails). `init_callee_regs/4` runs on every compiled-closure call, so this over-allocated a fresh tuple on every frame and regressed call-dense, work-light workloads ~26% — fib(30) most visibly (issue #324). Replace the slack with an exact per-prototype `reg_file_size`, derived in `Lua.Compiler.Bytecode` by walking the emitted bytecode for the highest register index any op references. This is authoritative even where codegen's `max_registers` undercounts the transient peak (a real undercount the slack had been masking — see the short-circuit/concat regression case added to the invariant test). Runtime-dynamic writes (vararg spread, multi-return distribution) still grow the tuple on demand via `grow_regs/2`, so the static size only covers the syntactic peak. fib(30): ~1.32x slower than Luerl -> ~1.11x (drift-controlled), no slower on any other workload. Closes #324.
davydog187
added a commit
that referenced
this pull request
Jun 14, 2026
Folds the dispatcher-side register sizing (#359) into the interpreter-side honest-max_registers fix (#312). With codegen's instruction_peak/1 backstop making max_registers a true bound on every statically-fixed destination, the separate per-prototype reg_file_size walker #359 added to work around the codegen undercount is redundant. Remove it: drop the reg_file_size field and the max_register_used/ register_positions walker from Lua.Compiler.Bytecode, and size both engines to max(max_registers, param_count) with no slack. Runtime-dynamic writes (vararg spread, multi-return) still grow on demand (grow_regs/2, ensure_regs_capacity/2). The max_registers invariant test keeps its independent bytecode-level walker as a cross-check against instruction_peak/1 (which walks the pre-encoding instruction stream) and now asserts against max_registers. One register-sizing source of truth across both engines.
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.
Summary
Recovers the deep-recursion performance regression tracked in #324.
fib(30)goes from ~1.32× slower than Luerl to ~1.11× (Apple M-series, drift-controlled by running Luerl identically before/after), with no other workload slower.What the investigation found
The issue (and the rc.1/rc.2 CHANGELOG "Known issues") attributed the regression to the per-call depth bookkeeping added in #283. Bisecting
fib(30)on this machine told a different story:3a4a0c8)df79f8f)Two findings:
+16register-slack buffer toinit_callee_regs/4, which runs on every compiled-closure call. fib(30) is ~2.7M calls, each over-allocating a register tuple (e.g. 10 → 26 slots). That's the +26%.The fix
The
+16slack existed to cover prototypes whose register file can transiently exceedmax_registers(multi-return constructor tails like{x, f()}). But every runtime-dynamic write in the dispatcher already grows the tuple on demand viagrow_regs/2(vararg spread, multi-return result distribution,{:multi, …}expansion,pad_nils). The slack's only real job was masking a latent static undercount in codegen'smax_registers.So:
reg_file_sizeinLua.Compiler.Bytecodeby walking the emitted bytecode for the highest register index any op references — authoritative even where codegen undercounts.grow_regs/2continues to cover runtime-dynamic growth.This recovers the perf for all compiled prototypes (not just fib — register tuples shrink across the board) and fixes the latent undercount the slack had been hiding (see the short-circuit/concat regression case added to the invariant test).
Review-stabilization cycle
An adversarial review caught a critical latent bug in the first version of this change (verified with a reproduction): the
:selfopcode (obj:m()) writes registerbase+1— not one of its syntactic operands — so the walker undercounted and a zero-arg method call crashed with:badarg. Fixed by handling:selfstructurally in both the production walker and the invariant test's walker, plus:obj:m()through the dispatcher), and:selfcorpus entry inmax_registers_invariant_test.exs.A follow-up focused audit (independent peak re-derivation across 88 programs + 15 end-to-end executions) confirmed no other opcode has the same bug class.
Validation
mix test(default +--include slow --include differential --include lua53, i.e. the official Lua 5.3 suite): all green (2368 passed with the heavy suites).:selffix and pass with it.mix format --check-formatted: clean.origin/main(lua/luerl ratio).Follow-ups (out of scope)
max_registersstill undercounts for some expression shapes (now harmless on the dispatcher; the interpreter retains its own+16buffer). Worth fixing at the root in codegen's peak tracking.Closes #324.