Skip to content

Commit f3ee2a8

Browse files
davydog187claude
andcommitted
perf: right-size register tuple allocations
Replace the hard-coded 256-register top-level allocation and the +64 over-allocation on every callee call with values derived from the prototype's max_registers field. - vm.ex: proto.max_registers + 16 (was 256) - executor.ex: max(max_registers, param_count) + 16 (was + 64) The +16 buffer accounts for multi-return expansion slots that the codegen doesn't always track in max_registers. This cuts register tuple waste significantly for recursive workloads like fib(N). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 56d0501 commit f3ee2a8

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

lib/lua/vm.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ defmodule Lua.VM do
1616
"""
1717
@spec execute(Prototype.t(), State.t()) :: {:ok, list(), State.t()}
1818
def execute(%Prototype{} = proto, state \\ State.new()) do
19-
# Create register file - tuple of nils
20-
# For now, just allocate 256 registers (we'll optimize this later)
21-
registers = Tuple.duplicate(nil, 256)
19+
# Create register file sized to the prototype's needs.
20+
# The +16 buffer covers multi-return expansion slots that the codegen doesn't
21+
# always track in max_registers (call results can land beyond the stated max).
22+
registers = Tuple.duplicate(nil, proto.max_registers + 16)
2223

2324
# Execute the prototype instructions
2425
{results, _final_regs, final_state} =

lib/lua/vm/executor.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Lua.VM.Executor do
3232
@spec call_function(term(), list(), State.t()) :: {list(), State.t()}
3333
def call_function({:lua_closure, callee_proto, callee_upvalues}, args, state) do
3434
callee_regs =
35-
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 64)
35+
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 16)
3636

3737
callee_regs =
3838
args
@@ -499,7 +499,7 @@ defmodule Lua.VM.Executor do
499499

500500
# Create new register file for the callee
501501
callee_regs =
502-
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 64)
502+
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 16)
503503

504504
# Copy arguments into callee registers (params are R[0..N-1])
505505
callee_regs =
@@ -1104,7 +1104,7 @@ defmodule Lua.VM.Executor do
11041104
# Helper: call a function value inline (used by generic_for)
11051105
defp call_value({:lua_closure, callee_proto, callee_upvalues}, args, _proto, state) do
11061106
callee_regs =
1107-
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 64)
1107+
Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 16)
11081108

11091109
callee_regs =
11101110
args

0 commit comments

Comments
 (0)