Browse documentation

A const import is read through a register its first read fills

Date: 2026-11-30 Area: engine, modules, bytecode, jit Related: ADR 0218, Architecture, Live import bindings, #2384, #1634

Context

An import is the exporter's own cell in the importer's cell table, so a read is live (ES2026 §9.1.1.5.1 GetBindingValue reads an indirect binding through its target). After #2322 and #2389 a read in a hot loop still cost about 5% of a whole run against reading a local, on #1634's repro: the baseline JIT's opModGet ran 16 instructions where a local's read is a register move. #2384 recorded four options: a register per const import filled on its first read, a value slot in the importer's table, closures that capture the exporter's cell, or accepting the cost.

Only an export const can be cached. §16.2.1.7.3.1 InitializeEnvironment creates its binding with CreateImmutableBinding, so once initialized it never changes. A let, function or class export is a mutable binding, and its reads must stay live. The importer is compiled before its imports are linked, and a module from a bytecode file was compiled on its own, so the compiler cannot tell which imports are const.

Decision

Johannes chose the register per const import (30 November 2026, "Let's do 1").

  • A cache register per import. The module function declares one hidden local per import slot, %modcache <slot>, before its hoisted functions, and starts it as the hole. A nested function captures it as an ordinary upvalue.
  • The exporter marks its const cells. opMakeModCell carries MODCELL_CONST in C when the published binding is an export const, and sets UPVAL_CONST in the cell's header. A re-export hands the importer that same cell.
  • The read goes through the cache. In the module function a read is opModGetC (the destination, the table, the slot and the cache register packed in C); in a nested function it is opModGetUC (the cache upvalue in B, the slot and the table upvalue in C). A cache that holds a value is the read. One that holds the hole reads the cell as opModGet does, throws the temporal-dead-zone ReferenceError for the hole, and fills the cache when the cell is marked const.
  • A mutable binding's read becomes the plain read. The first read that finds a cell not marked const rewrites its own instruction into opModGet (same table and slot), or opModGetU (the table upvalue and slot) in a nested function, so a let, function or class import pays nothing for the cache. Neither the rewrite nor its absence changes a result: the cached instructions still check the cell on every read whose cache is empty. A template linked again, or shared, keeps working either way.
  • Every tier runs them. Execute fills caches and rewrites; FastLoop runs a filled cache and the plain reads and leaves the rest to Execute; the baseline JIT runs a filled cache, and past an empty one reads a mutable cell in place, leaving a const one to the interpreter, which fills the cache; the optimizing tier embeds the baseline's code. The verifier checks the cache register and both upvalues. Code from a bytecode file takes all of them to Execute, as it does opModGet, and a file that marks a mutable binding const only makes reads of its own binding stale.
  • Limits. An import slot, cache register or table upvalue at or past 4096 keeps the uncached read.

Consequences

  • On #1634's repro the read of a const import costs about a local's: see the PR for the instruction counts with the JIT on and off, against the script's local read.
  • The garbage collector traces nothing new: the caches are registers or upvalues and hold JavaScript values or the hole.
  • A template's code can change after it ran once: an instruction that read a mutable import is a plain read from then on. Tools that read a template's instructions see the rewritten form.
  • ClosureCaptureName shows %modcache <slot> for a nested function that reads an import.
  • Namespace imports and JSON modules' values are immutable too but are not marked; they keep the plain read.