Skip to main content

Module insn

Module insn 

Source
Expand description

The instruction stream the interpreter actually runs.

Bytecode is a compact variable-length encoding, and decoding it costs a table lookup, a width switch and a bounds check per instruction. At ten nanoseconds per instruction that was a visible share of the budget, and it is work that never changes its answer: a function’s bytecode is immutable once compiled.

So it is decoded once, into a fixed-width record. Insn is twenty bytes — opcode, three operands, and the byte offset it came from — which is small enough that a loop’s whole body sits in a couple of cache lines, and it turns the interpreter’s pc into an index. The third operand exists for exactly one fusion (Fused::LtLocImmJump needs a slot, a bound and a target); it is zero everywhere else. Everything the old decode computed per instruction is computed here instead:

  • operands are unpacked from their 1/2/4-byte big-endian fields;
  • jump operands are resolved from the encoded relative offset to an index into this same array, so goto becomes an assignment rather than arithmetic;
  • the byte offset is kept, because that is what a stack trace reports and what the line table is keyed by.

This is not a peephole pass and not a superinstruction compiler: the instruction sequence is exactly the reference’s, one record per bytecode instruction, and the 422-case disassembly diff still compares that bytecode byte for byte.

Structs§

Insn
One decoded instruction.

Enums§

Fused
A fused instruction, and how many records it stands for.

Functions§

predecode
Decode code into an instruction array.