0102. Document core implementation details (E1 layout)
Status: Accepted (2026-11-27). Owner: WP-A. Decision 1 (ids) superseded by 0104 (run-length id map, ~0 B/node for bulk content). Refines 0024 (layout) and 0019 (rules) without changing their decisions.
Context
ADR 0024 fixes the budget: 55 B/node for 5M shapes and 51 B/node for website nodes, plus text. Hitting it, and keeping client and server verdicts identical, needed a few concrete choices the earlier ADRs left open. The measured website scene also differs from the ADR’s estimate: 404K of its 662K nodes are text, not ~270K.
Decision
- Ids (8 B/node).
- A
Uint32 counter column, plus per-client dense Int32 counter → handle tables sized to each client’s live range, with a sparse id → handle hash for clients whose counters are too sparse.
- The handle → client map is a sorted list of handle runs. Snapshot loads and bulk creates produce one run per client switch, so it costs ~0 B/node. Handle reuse by another client splits a run (O(runs)).
- Change feed without a per-node column. ChangeSet dirty bits live in a sparse handle → position map plus a list, sized by the changes since the last drain (1 B/node saved). Only a 1-bit-per-node “created in this window” set is dense.
- Record index. One lazy
Int32 column points to the node’s container record (first/last child, child count), text record or points record. The kinds are disjoint, and the column is absent for scenes with no containers, text or paths (5M shapes). The root’s container record is record 0.
- Text.
- Records are 12 B: offset, byte length, style index.
- Styles (fontSize, lineHeight, weight, align, sizing) are interned and encoded so the default style is index 0.
- Content is in a UTF-8 arena.
- Snapshot loads size arenas exactly.
- This brings the 4,000-page text to 20.0 MB, inside the 21 MB budget.
- Snapshot decode validates like an op block, minus value ranges. Framing, kinds, keys, parents, types and applicability are checked; per-value ranges and text UTF-8 are not, because snapshots are our own output of accepted ops. Text reads decode leniently. CREATE_BULK and RESTORE blocks from ops always get the full rule set.
- Rule details (one implementation in
rules.ts and codec/block.ts, shared by DocCore, ScanBatch and StructIndex):
- The canonical check order lives in
rules.ts: stateless checks first, then structural.
- Framing errors are exactly the inputs on which
shared/codec readOps throws.
- A string value starting with U+FEFF is BAD_VALUE: the wire decoder would strip it, so replicas would diverge.
- F32 values are checked after f32 rounding.
-0 is stored as +0. Opacity is stored at 1/255.
- An unknown prop’s default is the zero value of its type, so absent can be restored by inverses.
- The node-count limit rejects with BAD_VALUE (there is no LIMIT reason in the contract).
- At most one PARENT_INDEX per SET; none inside CREATE props.
- Block id column. Zigzag deltas are wrapped into ±2^52, so any id sequence below 2^53 encodes as safe varuints.
- Website generator.
- A diagonal stroke becomes a path, and connected strokes merge into one polyline.
- A vertical stroke becomes a thin filled rect.
- So the scene needs no rotation column. Everything else is the POC’s page content.
Alternatives
- A dense dirty column (1 B/node): simplest, but it was the difference between passing and failing the website gate.
- A per-node client-slot column (2 B/node): simpler than runs, but busts the 5M budget.
- 20 B text records with inline font props: 23.2 MB for the website text.
- Full value validation on snapshot load: ~70 ms more at 662K nodes, which pushed decode past 300 ms.
Consequences
Measured on the bench (node packages/doc/bench/run.ts), and recorded in document-core.md:
| Scene |
Measured |
Budget |
| 5M shapes |
55.13 B/node |
57.8 |
| 4,000 pages, nodes |
51.20 B/node |
53.6 |
| 4,000 pages, content (text, record index, names, points) |
19.95 MB |
21 MB |
The run-length id map (E2) can replace (1) without touching any other part.