LibJS: Skip initializing constant slots in ExecutionContext

Every function call allocates an ExecutionContext with a trailing array
of Values for registers, locals, constants, and arguments. Previously,
the constructor would initialize all slots to js_special_empty_value(),
but constant slots were then immediately overwritten by the interpreter
copying in values from the Executable before execution began.

To eliminate this redundant initialization, we rearrange the layout from
[registers | constants | locals] to [registers | locals | constants].
This groups registers and locals together at the front, allowing us to
initialize only those slots while leaving constant slots uninitialized
until they're populated with their actual values.

This reduces the per-call initialization cost from O(registers + locals
+ constants) to O(registers + locals).

Also tightens up the types involved (size_t -> u32) and adds VERIFYs to
guard against overflow when computing the combined slot counts, and to
ensure the total fits within the 29-bit operand index field.
This commit is contained in:
Andreas Kling 2026-01-18 23:17:10 +01:00 committed by Andreas Kling
parent 5b6fea6c57
commit 4d92c4d71a
Notes: github-actions[bot] 2026-02-06 11:27:20 +00:00
25 changed files with 147 additions and 102 deletions

View file

@ -127,13 +127,14 @@ UnrealizedSourceRange Executable::source_range_at(size_t offset) const
Operand Executable::original_operand_from_raw(u32 raw) const
{
// NB: Layout is [registers | locals | constants | arguments]
if (raw < number_of_registers)
return Operand { Operand::Type::Register, raw };
if (raw < local_index_base)
return Operand { Operand::Type::Constant, raw - static_cast<u32>(number_of_registers) };
if (raw < registers_and_locals_count)
return Operand { Operand::Type::Local, raw - local_index_base };
if (raw < argument_index_base)
return Operand { Operand::Type::Local, raw - static_cast<u32>(local_index_base) };
return Operand { Operand::Type::Argument, raw - static_cast<u32>(argument_index_base) };
return Operand { Operand::Type::Constant, raw - registers_and_locals_count };
return Operand { Operand::Type::Argument, raw - argument_index_base };
}
}