Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Performance

- **Deep recursion regression recovered (#324).** Call-dense, work-light
workloads (naive `fib(30)` most visibly) were ~25%+ slower than rc.0.
The rc.1/rc.2 changelog attributed this to the call-depth bookkeeping
added in #283, but bisection showed that bookkeeping is now negligible;
the dominant regressor was the blanket `+16` register-slack buffer #347
reserved on every prototype's register tuple, paid on every call frame
by the dispatcher's `init_callee_regs/4`. The dispatcher now sizes each
register file to an exact `reg_file_size` derived from the emitted
bytecode (authoritative even where codegen's `max_registers` undercounts
the peak); runtime-dynamic expansion still grows the tuple on demand.
`fib(30)` improves from ~1.32× slower than Luerl to ~1.11× (Apple M-series,
drift-controlled), with no other workload slower. Fixes the latent
register undercount the slack had been masking.
- **Register tuples are sized to an honest peak, with no slack buffer, on
both VM engines (#312, #324).** Both the interpreter (`call_function/3`,
the `:call` opcode, `call_value/5`) and the dispatcher
(`init_callee_regs/4`) used to over-allocate every call frame's register
tuple — the interpreter with a `+16` buffer, the dispatcher with the `+16`
slack #347 added. On call-dense, work-light code (naive `fib(30)` most
visibly, ~25%+ slower than rc.0) that per-frame over-allocation dominated.
Both buffers existed to mask a latent codegen bug: `max_registers` could
undercount the true register peak for some deeply-nested expression
shapes. Codegen's new `instruction_peak/1` backstop makes `max_registers`
honest (it counts every statically-fixed destination the emitted stream
writes), so both engines now size to exactly `max(max_registers,
param_count)` and grow on demand only for runtime-dynamic writes (vararg
spread, multi-return distribution). The interpreter path is ~26% faster
on `fib(25)` (closing the dispatcher–interpreter gap to ~1.02×), and
`fib(30)` on the dispatcher improves from ~1.32× slower than Luerl to
~1.11× (Apple M-series, drift-controlled), with no other workload slower.

### Fixed

Expand Down
171 changes: 1 addition & 170 deletions lib/lua/compiler/bytecode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,182 +124,13 @@ defmodule Lua.Compiler.Bytecode do

case encode_list(proto.instructions, [], 0) do
{:ok, encoded} ->
bytecode = List.to_tuple(encoded)

%{
proto_with_children
| bytecode: bytecode,
reg_file_size: reg_file_size(bytecode, proto.param_count)
}
%{proto_with_children | bytecode: List.to_tuple(encoded)}

:fallback ->
proto_with_children
end
end

# The exact register-file size the dispatcher must allocate to run
# `bytecode`: one past the highest register index any encoded op reads or
# writes, floored at `param_count` so the argument-copy range always fits.
#
# This is derived from the emitted bytecode rather than trusting
# `proto.max_registers`, which codegen can undercount for some expression
# shapes (it lost the peak of a few transient-register chains). The
# dispatcher used to paper over that with a blanket `+16` slack on every
# register tuple — costly on call-dense code that allocates a fresh tuple
# per frame (issue #324). Sizing exactly removes that per-call overhead
# while staying correct: runtime-dynamic expansion (vararg spread,
# multi-return results) is not counted here because the dispatcher grows
# the tuple on demand at those sites (`grow_regs/2`).
defp reg_file_size(bytecode, param_count) do
max(max_register_used(bytecode) + 1, param_count)
end

# Highest register index referenced anywhere in `bytecode`, or -1 if none.
# Recurses into the nested instruction tuples carried by branch/loop
# opcodes so the bound holds at every level.
defp max_register_used(bytecode) when is_tuple(bytecode) do
bytecode
|> Tuple.to_list()
|> Enum.reduce(-1, fn instr, acc -> max(acc, max_in_instr(instr)) end)
end

defp max_in_instr(instr) do
case register_positions(:erlang.element(1, instr)) do
:load_nil ->
# {tag, dest, count}: writes dest..dest+count-1.
:erlang.element(2, instr) + :erlang.element(3, instr) - 1

:vararg ->
# {tag, base, count}: writes base..base+count-1 when count>0; the
# count==0 spread grows on demand in the dispatcher, so bound by base.
base = :erlang.element(2, instr)
count = :erlang.element(3, instr)
if count == 0, do: base, else: base + count - 1

:return_multi ->
# {tag, base, count}: reads base..base+count-1.
:erlang.element(2, instr) + :erlang.element(3, instr) - 1

:numeric_for ->
# {tag, base, loop_var, body}: reads base..base+2, writes loop_var.
base = :erlang.element(2, instr)
Enum.max([base + 2, :erlang.element(3, instr), max_register_used(:erlang.element(4, instr))])

:while_loop ->
# {tag, test_reg, cond_body, loop_body}.
Enum.max([
:erlang.element(2, instr),
max_register_used(:erlang.element(3, instr)),
max_register_used(:erlang.element(4, instr))
])

:repeat_loop ->
# {tag, test_reg, loop_body, cond_body}.
Enum.max([
:erlang.element(2, instr),
max_register_used(:erlang.element(3, instr)),
max_register_used(:erlang.element(4, instr))
])

:generic_for ->
# {tag, base, var_regs_tuple, body, line}.
base = :erlang.element(2, instr)
var_max = Enum.reduce(Tuple.to_list(:erlang.element(3, instr)), -1, &max/2)
Enum.max([base + 2, var_max, max_register_used(:erlang.element(4, instr))])

:test ->
# {tag, reg, then_body, else_body}.
Enum.max([
:erlang.element(2, instr),
max_register_used(:erlang.element(3, instr)),
max_register_used(:erlang.element(4, instr))
])

:self ->
# {tag, base, obj_reg, method, hint}: reads obj_reg, writes the
# resolved method into base and the receiver into base+1 (base+1
# dominates base). The base+1 write is not a syntactic operand, so it
# must be added explicitly — a zero-arg `obj:m()` call has no later op
# to cover it.
max(:erlang.element(2, instr) + 1, :erlang.element(3, instr))

positions when is_list(positions) ->
Enum.reduce(positions, -1, fn pos, acc -> max(acc, :erlang.element(pos + 1, instr)) end)
end
end

# Register operand positions per opcode, where position 0 is the opcode
# tag. Reads and writes fold together — any out-of-bounds index would
# crash the dispatcher's `:erlang.element/2` or `:erlang.setelement/3`.
# Opcodes whose register footprint needs structural handling (ranges,
# nested bodies) return an atom dispatched above. Mirror this when adding
# an encodable opcode; `max_registers_invariant_test.exs` pins the
# contract across the compilable surface.
defp register_positions(op) do
case op do
@op_load_constant -> [1]
@op_load_boolean -> [1]
@op_load_nil -> :load_nil
@op_move -> [1, 2]
@op_load_env -> [1]
@op_get_upvalue -> [1]
@op_get_global -> [1]
@op_get_field -> [1, 2]
@op_add -> [1, 2, 3]
@op_subtract -> [1, 2, 3]
@op_multiply -> [1, 2, 3]
@op_divide -> [1, 2, 3]
@op_floor_divide -> [1, 2, 3]
@op_modulo -> [1, 2, 3]
@op_power -> [1, 2, 3]
@op_negate -> [1, 2]
@op_bitwise_and -> [1, 2, 3]
@op_bitwise_or -> [1, 2, 3]
@op_bitwise_xor -> [1, 2, 3]
@op_shift_left -> [1, 2, 3]
@op_shift_right -> [1, 2, 3]
@op_bitwise_not -> [1, 2]
@op_less_than -> [1, 2, 3]
@op_less_equal -> [1, 2, 3]
@op_greater_than -> [1, 2, 3]
@op_greater_equal -> [1, 2, 3]
@op_equal -> [1, 2, 3]
@op_not_equal -> [1, 2, 3]
@op_not -> [1, 2]
@op_test -> :test
@op_call_zero -> [1]
@op_call_one -> [1]
@op_call_multi -> [1]
@op_return_one -> [1]
@op_return_zero -> []
@op_return_collect -> [1]
@op_return_multi -> :return_multi
@op_return_proto_varargs -> []
@op_new_table -> [1]
@op_get_table -> [1, 2, 3]
@op_set_table -> [1, 2, 3]
@op_set_field -> [1, 3]
@op_set_list -> [1, 2]
@op_set_list_multi -> [1, 2]
@op_length -> [1, 2]
@op_numeric_for -> :numeric_for
@op_closure -> [1]
@op_set_upvalue -> [2]
@op_get_open_upvalue -> [1, 2]
@op_set_open_upvalue -> [1, 2]
@op_vararg -> :vararg
@op_self -> :self
@op_concatenate -> [1, 2, 3]
@op_break -> []
@op_while_loop -> :while_loop
@op_repeat_loop -> :repeat_loop
@op_generic_for -> :generic_for
# Carries a register *watermark* (pre-block next_register), not an
# operand it touches; the watermark can be one past the last live slot.
@op_close_upvalues -> []
end
end

# ── Encoding ────────────────────────────────────────────────────────────
#
# Walks an instruction list, accumulating opcode tuples in reverse. On
Expand Down
102 changes: 100 additions & 2 deletions lib/lua/compiler/codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ defmodule Lua.Compiler.Codegen do
upvalue_names: ["_ENV"],
param_count: 0,
is_vararg: func_scope.is_vararg,
max_registers: Enum.max([func_scope.max_register, ctx.next_reg, Map.get(ctx, :peak_reg, 0)]),
max_registers:
Enum.max([
func_scope.max_register,
ctx.next_reg,
Map.get(ctx, :peak_reg, 0),
instruction_peak(instructions)
]),
source: source,
lines: lines
}
Expand Down Expand Up @@ -87,6 +93,92 @@ defmodule Lua.Compiler.Codegen do
Map.put(ctx, :peak_reg, max(Map.get(ctx, :peak_reg, 0), ctx.next_reg))
end

# Honest peak-register guarantee for `max_registers`.
#
# `peak_reg`/`next_reg` tracking can undercount in deeply nested
# constructs (e.g. a concat chain inside a list-field of a table
# constructor nested in several `for` bodies). Undercounting is benign
# only as long as a fixed register buffer absorbs the slack; the
# executor and dispatcher both run buffer-free, so `max_registers` must
# cover every statically-fixed destination register actually emitted.
#
# This walks the emitted instruction stream (including nested loop /
# short-circuit bodies) and returns the number of register slots needed
# — i.e. the highest fixed destination index plus one. Dynamic-range
# writers (`:call`, `:vararg`) are grown lazily at runtime via
# `ensure_regs_capacity/2`, so only their fixed `base` contributes here.
#
# Exposed (`@doc false`) so the backstop's completeness can be pinned
# directly: every statically-fixed destination opcode codegen emits must
# be counted here, independent of whether `peak_reg` happens to cover it.
@doc false
def instruction_peak(instructions) do
Enum.reduce(instructions, 0, fn instr, acc ->
max(acc, instruction_size(instr))
end)
end

# Returns the register-slot count an instruction proves is needed (its
# highest written register index + 1), recursing into nested bodies.
defp instruction_size({:load_nil, dest, count}), do: dest + count
defp instruction_size({:vararg, base, count}) when is_integer(count) and count > 0, do: base + count
defp instruction_size({:vararg, base, _}), do: base + 1
defp instruction_size({:self, base, _obj, _name, _hint}), do: base + 2
defp instruction_size({:call, base, _ac, _rc, _hint}), do: base + 1
defp instruction_size({:source_line, _line, _src}), do: 0
defp instruction_size({:close_upvalues, _threshold}), do: 0
defp instruction_size({:label, _name}), do: 0
defp instruction_size({:set_list, _table, start, count, _offset}) when is_integer(count), do: start + count
defp instruction_size({:set_list, _table, start, {:multi, init}, _offset}), do: start + init
defp instruction_size({:numeric_for, base, _loop_var, body}), do: max(base + 3, instruction_peak(body))
defp instruction_size({:generic_for, base, _var_regs, body}), do: max(base + 3, instruction_peak(body))

defp instruction_size({:while_loop, cond_body, _reg, body}),
do: max(instruction_peak(cond_body), instruction_peak(body))

defp instruction_size({:repeat_loop, body, cond_body, _reg}),
do: max(instruction_peak(body), instruction_peak(cond_body))

defp instruction_size({:test, _reg, then_body, else_body}),
do: max(instruction_peak(then_body), instruction_peak(else_body))

defp instruction_size({:test_and, dest, _src, body}), do: max(dest + 1, instruction_peak(body))
defp instruction_size({:test_or, dest, _src, body}), do: max(dest + 1, instruction_peak(body))

# Stores, control flow, and debug annotations establish no fresh
# destination register. Stores write through an existing table / upvalue
# (the register they read was already counted by whatever wrote it);
# returns, breaks, and gotos write no register; labels and source lines
# are annotations. Reads of an existing register never raise the peak, so
# all of these contribute nothing.
defp instruction_size(:break), do: 0
defp instruction_size({:goto, _label}), do: 0
defp instruction_size({:return, _base, _count}), do: 0
defp instruction_size({:return_vararg}), do: 0
defp instruction_size({:set_table, _table, _key, _value, _hint}), do: 0
defp instruction_size({:set_field, _table, _name, _value, _hint}), do: 0
defp instruction_size({:set_upvalue, _index, _source}), do: 0
defp instruction_size({:set_open_upvalue, _reg, _source}), do: 0

# Everything that reaches here is an ordinary value-producing opcode —
# `{tag, dest, ...}` whose destination is operand 1. That is the rule for
# every load / move / arithmetic / comparison / bitwise / table-read /
# closure opcode, so it is the default rather than an enumerated allowlist:
# a new opcode of that shape needs no change here. The clauses above carve
# out the only exceptions — opcodes that write a range or a fixed offset
# off a base, recurse into nested bodies, or write no register at all.
#
# This default cannot reintroduce the #312 undercount: every value-less
# opcode whose operand 1 is a register index is listed above (a missing
# one would only ever *over*-count by a slot — benign slack, never a
# crash), and any instruction without an integer operand 1 falls through
# to zero.
defp instruction_size(instr) when is_tuple(instr) and tuple_size(instr) > 1 and is_integer(elem(instr, 1)) do
elem(instr, 1) + 1
end

defp instruction_size(_instr), do: 0

defp emit_source_line(%{meta: %{start: %{line: line}}}, ctx) when is_integer(line) do
[Instruction.source_line(line, ctx.source)]
end
Expand Down Expand Up @@ -1573,7 +1665,13 @@ defmodule Lua.Compiler.Codegen do
upvalue_names: Enum.map(func_scope.upvalue_descriptors, &elem(&1, 2)),
param_count: func_scope.param_count,
is_vararg: func_scope.is_vararg,
max_registers: Enum.max([func_scope.max_register, body_ctx.next_reg, Map.get(body_ctx, :peak_reg, 0)]),
max_registers:
Enum.max([
func_scope.max_register,
body_ctx.next_reg,
Map.get(body_ctx, :peak_reg, 0),
instruction_peak(body_instructions)
]),
source: ctx.source,
lines: lines
}
Expand Down
1 change: 0 additions & 1 deletion lib/lua/compiler/instruction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ defmodule Lua.Compiler.Instruction do
def generic_for(base, var_count, body), do: {:generic_for, base, var_count, body}

def break_instr, do: :break
def scope(register_count, body), do: {:scope, register_count, body}

# Functions
def closure(dest, proto_index), do: {:closure, dest, proto_index}
Expand Down
23 changes: 9 additions & 14 deletions lib/lua/compiler/prototype.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ defmodule Lua.Compiler.Prototype do
max_registers: non_neg_integer(),
source: binary(),
lines: {non_neg_integer(), non_neg_integer()},
bytecode: tuple() | nil,
reg_file_size: non_neg_integer()
bytecode: tuple() | nil
}

# `bytecode` is an optional dense encoding produced by `Lua.Compiler.Bytecode`.
Expand All @@ -33,16 +32,13 @@ defmodule Lua.Compiler.Prototype do
# `instructions` list (used by error reporting, debugging, and any future
# tooling) survives untouched.
#
# `reg_file_size` is the exact number of registers the dispatcher must
# allocate to run `bytecode` — the highest register index any encoded op
# reads or writes, plus one (and never below `param_count`). It is derived
# from the emitted bytecode in `Lua.Compiler.Bytecode.compile/1`, which is
# authoritative even where codegen's `max_registers` undercounts the peak;
# runtime-dynamic expansion (vararg spread, multi-return) grows the tuple
# on demand in the dispatcher. Sizing to this exact value rather than a
# blanket slack buffer keeps call-dense code (deep recursion) off a
# per-call register over-allocation (issue #324). Zero for prototypes that
# fell back to the interpreter, which sizes its own register file.
# Both engines size their register file to `max(max_registers, param_count)`
# with no slack buffer. `max_registers` is the honest register peak —
# codegen's `instruction_peak/1` backstop counts every statically-fixed
# destination the emitted stream writes — and runtime-dynamic expansion
# (vararg spread, multi-return) grows the tuple on demand. Sizing exactly
# keeps call-dense code (deep recursion) off a per-call over-allocation
# (issues #312, #324).
defstruct instructions: [],
prototypes: [],
upvalue_descriptors: [],
Expand All @@ -53,8 +49,7 @@ defmodule Lua.Compiler.Prototype do
source: <<"-no-source-">>,
lines: {0, 0},
varargs: [],
bytecode: nil,
reg_file_size: 0
bytecode: nil

@doc """
Creates a new prototype with the given options.
Expand Down
Loading
Loading