Browse documentation

Map and Set iterators follow forwarding records, so compaction never waits for them

Date: 2026-11-28 Area: engine, built-ins Related: ADR 0077, #2190

Context

A Map or Set keeps its entries in one list in insertion order, and a delete leaves a hole there. ES2026 §24.1.5.1 CreateMapIterator and §24.2.6.1 CreateSetIterator define an iterator as an index into that list (the [[MapData]] / [[SetData]] List), which §24.1.3.3 Map.prototype.delete marks empty and §24.1.3.1 Map.prototype.clear empties in place. The note on §24.1.3.3 lets an implementation remove entries physically, as long as no iterator can tell.

The engine compacted a list once its holes outnumbered its live entries, but only while no iterator was open: the map counted its open iterators, and an iterator gave its count back only when it ran to its end. This is the gate ADR 0077 chose, and it named the gap it left: Map and Set iterators have no return, so an iterator left part-way (m.keys().next(), for...of with break, const [[k, v]] = m, isSubsetOf returning false) kept the count up for the map's lifetime. Such a map never compacted again. An LRU cache that evicts with m.delete(m.keys().next().value) then scanned every earlier hole on each eviction and kept every one alive: 80,000 inserts with 100 kept took 990 ms against 25 ms when evicting by key, and the live heap grew by 8 MB per 160,000 inserts.

Four ways to let compaction go on were weighed:

  • Release the count when an abandoned iterator dies. The nursery is a copying collector that never visits a dead cell, and compaction would then depend on when a collection happens to run.
  • Skip a run of holes at the front of the list with an offset the iterators share. It fixes eviction of the oldest entry, but not holes left in the middle.
  • Keep a translation table on the map for each compaction. The map cannot tell when the last iterator that needs a table is gone, so the tables pile up.
  • Replace the list and leave a forwarding record in the old one, which each iterator follows the next time it steps. V8's OrderedHashTable does this.

Decision

  • An iterator holds its list. A Map or Set iterator keeps the entry list it is in (Aux2) and its index there (Native), beside the map (Aux1) and the map's clear count (Pad). Creating one, or stepping into a list, marks that list MEL_ITERATED in its Hdr.Extra.
  • A marked list is replaced, not rewritten. When it compacts or grows, the live entries go to a new list, and the old one becomes a MEL_FORWARD record: Data[0] is the list that took its place, and the rest are the numbers of the entries it dropped, ascending (none when it grew). The new list is marked too, since the old list's iterators will come to it. A list no iterator has reached still compacts in place.
  • An iterator follows the records lazily. On its next step it walks the records to the map's list and moves its index back two for each dropped entry before it, found by binary search (MapListFollow in Goccia.VM). A clear still empties the list in place and bumps the clear count; an iterator that sees a new count starts at the beginning of the map's list.
  • Only forEach holds compaction off. The map's Native now counts only the forEach walks under way, which always end and give the count back.

Consequences

  • An iterator left part-way never stops the map compacting. The LRU loop above takes 34 ms for 80,000 inserts, and its live heap stays flat.
  • An iterator that is still reachable keeps alive the records from its list onward, each no larger than the list it replaced, and no entries. Records an unreachable iterator held are collected with it.
  • Once a map has been iterated, its compactions allocate a new list, and its growth writes a record into the old one. Both are proportional to the list, as the copy they come with already is.
  • Iteration semantics are unchanged: a JavaScript model of the specification's list, run beside the engine through deletes, sets, clears and iterators stepped often and seldom, agrees on every step, and Node agrees with the model.