Skip to content

perf(vm): close the fibonacci gap with Luerl — register reclaim + lazy call frames - #360

Merged
davydog187 merged 2 commits into
mainfrom
perf/fibonacci-register-reclaim-call-info
Jun 14, 2026
Merged

davydog187 merged 2 commits into
mainfrom
perf/fibonacci-register-reclaim-call-info

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Summary

Closes the recursive-fibonacci performance gap with Luerl by attacking the two costs the profiler flagged. On fib(30), the time gap shrinks from 1.18x slower → 1.03x (parity within Luerl's own ±3.3% run-to-run noise) and total allocations fall ~26%.

Driven data-first with the profiling loop: baseline → mix profile.tprof (time + memory) → one fix → re-baseline.

What the profile showed

On fib(26) through the dispatcher:

Function Time % Alloc % Notes
:erlang.setelement/3 22.6% 61.6% register tuple copied on every write
Dispatcher.dispatch/8 46.2% 19.2% the interpreter loop
Executor.dispatcher_call_info/3 5.0% 3.7% per-call traceback map
hint_name/1 + hint_namewhat/1 3.4% — name decoding, per call

Changes

1. Defer call-frame name decoding (perf(vm))
Every call eagerly built a 4-key %{source, line, name, namewhat} map and decoded the call-site name tag — but those fields are read only by error traceback formatting and debug.getinfo, both cold. Now stores a flat 3-tuple {source, line, name_hint} and decodes lazily at the cold readers. dispatcher_call_info drops 5.0%→2.0% CPU; the hint_* functions leave the profile; per-frame alloc 7→4 words.

2. Reclaim register window after single-result calls (perf(codegen))
Expr.Call left next_reg above the dead argument registers, so fib(n-1) + fib(n-2) stacked the two calls into disjoint register ranges and reported max_registers = 10. Resetting next_reg to base_reg + 1 after the call lets siblings reuse the freed registers — fib drops to max_registers = 6. Since each register file is a tuple sized to max_registers, this shrinks setelement/copy_regs/make_tuple/init_callee_regs proportionally. This is the dominant win.

Results (full-mode benchmarks/fibonacci.exs, vs Luerl)

Before After
Time (chunk) 1.18x slower 1.03x
Time (eval) 1.14x slower 1.04x
Allocations (fib(26), tprof) 73.6M words 54.4M words (−26%)

Memory remains 1.13x Luerl, dominated by setelement on the now-smaller tuples — further reduction is structural (the register representation itself).

Testing

  • mix test --include slow — 2413 passed, 0 failures (includes the official Lua 5.3 suite)
  • mix test --only differential --only lua53 — all pass
  • Spot-checked nested/multi-return calls and the closures/OOP benchmarks (compute correctly; register reclamation can only shrink tuples, never regress)

Every Lua call pushed a 4-key `%{source, line, name, namewhat}` map onto
`call_stack`, eagerly running `hint_name/1` and `hint_namewhat/1` to
decode the call-site name tag. Those fields are read only by error
traceback formatting and `debug.getinfo` — both cold paths — so the work
is wasted on every successful call.

Store a flat 3-tuple `{source, line, name_hint}` instead and decode the
name lazily at the cold readers via new `Executor.frame_{source,line,
name,namewhat}/1` accessors (which also still accept caller-supplied map
frames, as the public `ErrorFormatter` entry points and tests pass).

Profiling fib(26) on the dispatcher: `dispatcher_call_info/3` drops from
5.0% to 2.0% of CPU and the two `hint_*` functions leave the profile
entirely; per-frame allocation falls from 7 words to 4.
`Expr.Call` left `next_reg` at `base + 1 + arg_count`, but a
single-result call's result lives in `base_reg` and the callee/argument/
vararg temp registers above it are dead. Nothing reclaimed them, so
sibling subexpressions stacked fresh registers on top: `fib(n-1) +
fib(n-2)` compiled the two calls into disjoint register ranges (1-4 and
5-8) and reported `max_registers = 10`.

Reset `next_reg` to `base_reg + 1` after the call (recording the peak
first so the spread still counts; the `instruction_size` backstop scans
the emitted writes regardless). The second call now reuses the freed
registers and `max_registers` for fib drops to 6.

Since every register file is a tuple sized to `max_registers`, this
shrinks `setelement`, `copy_regs`, `make_tuple`, and `init_callee_regs`
proportionally. On fib(30) vs Luerl the time gap closes from 1.18x to
1.03x (parity within noise) and total allocations fall ~26%.
@davydog187
davydog187 merged commit ec33c12 into main Jun 14, 2026
5 checks passed
@davydog187
davydog187 deleted the perf/fibonacci-register-reclaim-call-info branch June 14, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant