Repository navigation
vm: cut per-call and per-iteration overhead in the dispatcher - #401
Conversation
Mechanical micro-fixes on the per-call and per-iteration path. None of
them changes semantics; each removes work the default configuration was
paying for a result it already knew.
* `close_upv/2`, `tick/2`, `ckdepth/1`: inlined local shims so the
empty-`open_upvalues`, `max_instructions: :infinity` and
`max_call_depth: :infinity` cases resolve in one function-head match
instead of a cross-module call into `Executor`/`State`.
* Call frames build the `{source, line, name_hint}` tuple inline
rather than through `Executor.dispatcher_call_info/3`.
* `setup_vararg_proto/4` is guarded by the callee's `is_vararg` at the
call site, so a non-vararg call pays one field read.
* `Map.get/2` -> `:maps.get/3` on the upvalue-cell and open-upvalue
reads, dropping the `Map.get/2` -> `Map.get/3` hop.
* `@op_set_field` gets the inline no-metatable fast path that
`@op_get_field` already had; tables carrying a metatable still
bridge so the `__newindex` chain stays the interpreter's.
* `Table.from_data/1` short-circuits the empty map, so a `{}`
constructor no longer pays `Enum.sort` plus a fold.
* `Table.insert_hash/3` gains a no-dead-keys clause, skipping the
resurrection probe on tables that have never had a key removed.
* The compiled-mode native-call position bridge uses the process
dictionary BIFs directly and restores explicitly instead of through
a `try/after`; `Executor.execute/5`'s `after` remains the outer net.
Measured (MIX_ENV=prod, min-of-9 x 100 reps, alternating A/B over four
rounds on a machine with concurrent load; best round per configuration):
fib(15) 479.9us -> 459.2us -4.3%
closures 100x10 506.7us -> 493.2us -2.7%
oop 2000 4590.1us -> 4468.6us -2.6%
table build 5000 3799.3us -> 3633.3us -4.4%
set_field 20000 5961.0us -> 5588.4us -6.3%
Every in-mode Lua call used to rebuild the 17-field `%State{}` twice —
once to push `call_stack`/`call_depth` and reset `open_upvalues`, once to
undo all three on the way out — for roughly 42 words of garbage per call
on a path that otherwise allocates almost nothing.
These three fields are control state, not heap state, so they follow the
discipline `instruction_count` already used: they become parameters of
the dispatch loop, living in X-registers and allocating nothing. Pushing
a frame is now a cons onto the parameter plus an increment; popping it is
`tl/1` plus a decrement, with the caller's open upvalues coming back off
the frame tuple that already carried them.
`%State{}` keeps the fields, because the interpreter still stores them
there and because everything outside the loop reads them from the struct:
`debug.getinfo`, `error(msg, level)`, traceback formatting,
`State.check_call_depth!/1`, `State.tick!/2`. The loop therefore writes
them back — `sync/3` — wherever execution leaves it: an `Executor`
bridge, a native callback, its own raise sites. `sync/3` returns the
struct untouched when it already agrees, so a loop body that bridges
every iteration without calling anything (`t[k] = v`, an `__index` read)
pays for the write once rather than per iteration.
`close_upv/2` now filters the parameter directly, which retires
`Executor.dispatcher_close_open_upvalues_at_or_above/2`;
`Executor.dispatcher_call_info/3` went unused in the previous commit and
goes with it, its frame-shape rationale moving to the `call_frame/0`
typedoc.
Debug output is unchanged by construction: the same
`{source, line, name_hint}` frames are pushed in the same order, just
onto a parameter. Verified by diffing `Lua.format_exception/1` output
across thirteen failing programs (nested runtime errors, `error()` at
levels 1 and 2, stdlib bad-argument raises, `__index` arithmetic errors,
`debug.traceback`, `debug.getinfo`, error-after-caught-error) plus the
exact trip points of `:max_instructions` (7 budgets) and
`:max_call_depth` (11 depths) — byte-identical before and after.
Measured (MIX_ENV=prod, min-of-9 x 100 reps, alternating A/B over three
rounds on a machine with concurrent load; best round per configuration):
fib(15) 472.8us -> 365.5us -22.7% (1.29x)
closures 100x10 504.0us -> 448.5us -11.0%
oop 2000 4598.3us -> 4386.8us -4.6%
table build 5000 3819.1us -> 3792.7us -0.7%
set_field 20000 5633.8us -> 5659.4us +0.5%
The dispatcher now carries its open upvalues as a loop parameter rather
than in `%State{}`, so a nested evaluation is isolated from them by
construction instead of by a save/restore. Pin that from the compiled
side: the interpreter-entered variants above only exercise a main chunk.
🤖 Automated review — Opus + Sonnet consolidated findingsTwo independent AI reviewers (Opus, Sonnet) reviewed this PR. Both converged on the same MEDIUM. The 🟠 MEDIUM — position-restore is no longer exception-safe in the compiled-mode native-call bridge (found independently by both reviewers)
The PR replaces the Concrete scenario: The two structurally identical interpreter-side native-call bridges in the same file ( 🟡 LOW
Follow-up (pre-existing, out of scope)
A fix for the MEDIUM and the LOW items will be pushed to this branch shortly. |
The dispatcher's native-call bridge published {line, source} into the
position slot and restored it with a sequential `case` that only runs on
a normal return. A raise inside the wrapped stdlib call skipped the
restore, and not every entry into this bridge sits under `execute/5`'s
`after restore_position/1` net: `Lua.call_function/3` on a compiled
closure is rescue-only, as is `Dispatcher.do_execute_top/4`. The stale
position then survived the call and attributed a later, unrelated
evaluation's line-less bridge error (indexing nil inside a compiled
body) to the first chunk's line.
Restores `try/after` around the call, keeping the BIF-level process
dictionary access the sibling interpreter bridges' shape allows.
Also:
* correct the `:compiled_closure` comment — the dispatcher isolates
open upvalues via a dispatch-loop parameter, not save/restore
* cover the inlined `:set_field` arms from compiled code: `__newindex`
with both function and table handlers, and the nil-delete case
* assert the require/open-upvalue dispatcher test's subject actually
bytecode-compiles so it can't degrade into an interpreter test
Claude-Session: https://claude.ai/code/session_01BYHGFLoHBJAsUrTzjVp5nC
|
Pushed 694a579 addressing the review findings. The MEDIUM one is real: the dispatcher's native-call bridge lost its |
Cuts per-call and per-iteration overhead in the bytecode dispatcher. Runtime only — the bytecode format, the encoder, and codegen are untouched, so the codegen work (peephole, static-arity calls, new opcodes) lands independently on top of this.
Two commits, each independently green on the full suite.
1.
vm: inline hot-path guards and fast paths in the dispatcherMechanical micro-fixes. Nothing changes semantics; each removes work the default configuration paid for a result it already knew.
close_upv/2,tick/2,ckdepth/1— inlined local shims so the empty-open_upvalues,max_instructions: :infinityandmax_call_depth: :infinitycases resolve in one function-head match instead of a cross-module call intoExecutor/State. The empty-open-upvalues guard alone was two cross-module calls per loop iteration.{source, line, name_hint}inline instead of throughExecutor.dispatcher_call_info/3.setup_vararg_proto/4is guarded by the callee'sis_varargat the call site, so a non-vararg call pays one field read. (The vararg path still copies the whole%Prototype{}.)Map.get/2→:maps.get/3on the upvalue-cell and open-upvalue reads, killing theMap.get/2→Map.get/3hop.@op_set_fieldgets the inline no-metatable fast path@op_get_fieldalready had. Tables carrying a metatable still bridge, so the__newindexchain stays exactly the interpreter's.Table.from_data/1short-circuits the empty map — every{}constructor was payingEnum.sortplus a fold over nothing.Table.insert_hash/3gains a no-dead-keys clause, skipping the resurrection probe on tables that have never had a key removed.try/after.Executor.execute/5'safter restore_position/1remains the outer net.2.
vm: thread call stack, depth, and open upvalues as loop parametersEvery in-mode Lua call rebuilt the 17-field
%State{}twice — once to pushcall_stack/call_depthand resetopen_upvalues, once to undo all three — for ~42 words of garbage per call on a path that otherwise allocates almost nothing.These are control state, not heap state, so they now follow the discipline
instruction_countalready used: parameters of the dispatch loop, living in X-registers, allocating nothing. Pushing a frame is a cons plus an increment; popping istl/1plus a decrement, with the caller's open upvalues coming back off the frame tuple that already carried them.%State{}keeps the fields — the interpreter still stores them there, and everything outside the loop reads them from the struct (debug.getinfo,error(msg, level), traceback formatting,State.check_call_depth!/1,State.tick!/2). So the loop writes them back (sync/3) wherever execution leaves it: anExecutorbridge, a native callback, its own raise sites.sync/3returns the struct untouched when it already agrees, so a loop body that bridges every iteration without calling anything (t[k] = v, an__indexread) pays the write once instead of per iteration.This differs from the sketch in the brief, which proposed dropping
state.call_stackentirely and materializing the traceback lazily from the dispatcher'sframeslist on the error path. Threading it eagerly was chosen instead: the frame entry is 4 words plus a cons against the 42 the struct rebuild cost, so nearly all of the win is kept, while lazy materialization would rebuild an O(depth) list at every bridge crossing — a regression for bridge-heavy code at depth (OOPself.xon a table with a metatable bridges on every read) — and would have needed the traceback reconstructed from call opcodes rather than pushed by them.close_upv/2now filters the parameter directly, retiringExecutor.dispatcher_close_open_upvalues_at_or_above/2;Executor.dispatcher_call_info/3went unused in commit 1 and goes with it, its frame-shape rationale moving to thecall_frame/0typedoc.Debug fidelity
Unchanged by construction — the same
{source, line, name_hint}frames in the same order, pushed onto a parameter rather than a struct field. Verified by diffingLua.format_exception/1output across thirteen failing programs (nested runtime errors,error()at levels 1 and 2, stdlib bad-argument raises,__indexarithmetic errors,debug.tracebackfrom depth,debug.getinfocurrentline/name, error-after-caught-error, concat errors, deep recursion tracebacks) plus the exact trip points of:max_instructionsacross 7 budgets and:max_call_depthacross 11 depths. Byte-identical before and after, including the 200-frame... 190 more frames ...traceback elision andpcall-caught"stack overflow".Numbers
MIX_ENV=prod, min-of-9 × 100 reps per measurement, alternating A/B by swapping the source files in place and recompiling between runs. The machine had concurrent load: within-configuration spread across rounds was up to 5%, so these are the best round for each configuration, and the alternation is what makes the comparison meaningful rather than any single number.Workload bodies are wrapped in a Lua function, because
Lua.VM.execute/3runs the main chunk on the interpreter and only enters the dispatcher through a call.Per commit:
fib(15)End to end,
mainvs the branch tip, measured in one alternating run:fib(15)3.
vm: pin dispatcher-side open-upvalue isolation across requireOne regression test. The existing
requireopen-upvalue tests only exercise a main chunk, which runs on the interpreter; the invariant they pin (a nested evaluation cannot see the caller's register-keyed upvalue cells) now holds on the dispatcher side by construction rather than by a save/restore, so it gets a test entered from compiled code.Validation
mix format --check-formattedclean; no compiler warnings.mix test --include slow --include lua53— 2639 passed, 16 skipped (includes the official Lua 5.3 suite and the error-message/traceback tests) after each commit.mix test --include differential— 2618 passed.mix docs --warnings-as-errorsclean;mix dialyzerclean (0 errors).