Linora

0104. Run-length id map (memory E2, doc part)

Status: Accepted (2026-11-28, DC2). Supersedes decision 1 of 0102 (ids, 8 B/node). Implements the E2 id step of 0024.

Context

The E1 id map stored a Uint32 counter per handle plus a per-client dense counter -> handle table: 8 B per node, the largest part of the doc left to cut (ADR 0024 projects ~0 B for generated content). Gates: memoryReport() bytes/node must drop or hold for both bench scenes; decode/encode, ScanBatch and StructIndex must not regress; canonical snapshots stay byte-stable.

Decision

packages/doc/src/idmap.ts; same interface, in memory only, so snapshots and the wire are unchanged.

  1. Client per handle: unchanged (sorted handle runs per client switch, ~0 B/node).
  2. Counter per handle in chunks of 1,024 handles.
    • An affine chunk stores one base: counter = base + offset.
    • The first counter off that line (handle reuse, interleaved creates, counter gaps) turns the chunk explicit: a 4 KB Uint32 block.
    • Bulk creates and snapshot loads of generated content are affine.
  3. id -> handle:
    • Per client, sorted non-overlapping affine segments (counter range -> handle range), extended in O(1) on appends.
    • Ids placed at explicit positions use the E1 dense/sparse table.
    • A lookup tries the explicit table first (only when it holds entries), then the client’s last-extended segment, then a binary search.
    • After any handle reuse, a segment hit is verified against the handle’s current id, so a freed and reused handle never aliases a deleted id. The doc already treats free handles as absent.
  4. Decode duplicate check: insertUnique reports a repeated id through segment overlap, without a lookup per node.

Measured (node packages/doc/bench/run.ts, shared M4 Pro; codec times are medians of 5)

| | E1 | E2 | |—|—:|—:| | idMap, both scenes | 8.00 B/node | 0.01 B/node | | 5M shapes after snapshot load | 55.13 B/node | 47.13 B/node | | 5M shapes after generation (1.25x slack) | 62.57 B/node | 53.59 B/node | | 4,000 pages, nodes (content counted separately) | 51.20 B/node | 43.21 B/node | | decodeSnapshot 5M / 4,000 pages | 1,216 / 236 ms | 1,078 / 212 ms | | encodeSnapshot 5M / 4,000 pages | 1,597 / 251 ms | 1,577 / 244 ms | | ScanBatch / StructIndex.check | 47 / 111 ns | 48 / 96 ns (no id map involved) | | apply 100K SETs (2 lookups each) | 7.4 ms | 8.0 ms |

The lookup costs ~3 ns more on the SET path. That is the only regression, and it is far inside the 50 ms target; the memory win was judged worth it.

Alternatives

Consequences