0502. The engine as a derived cache of the doc: paint-order positions, holes and tail layout
Status: Accepted (2026-11-27, WP-E1). Implements 0024 and records its measured numbers.
Context
ADR 0024 decided that the doc’s columns are the only CPU copy of node data and that the engine derives GPU instances per 1,024-instance chunk, keeping only primStart per node. It left open how instance positions follow edits without per-node counts, and how hit testing, LOD and selection work without the CPU instance mirror. The GPU buffers, shaders, chunking and LOD must stay as they are (fps and worst-zoom GPU parity).
Decision
- Positions, not counts. Per node the engine stores only
primStart (Int32). A node’s own instances are [primStart, primStart of the next laid-out node in pre-order). Its count is derivable at any time from the doc (shapes 1, frame 1, group/page 0, text = non-space code points, path = points - 1), so nothing else is stored per node.
- Positions move only in a tail layout.
DocSync (sync.ts) turns each drained ChangeSet into the cheapest correct work:
- deleted node: its instances become hidden holes; its chunks re-derive in place, with no shift;
- geometry/style/visibility change: re-derive the node’s range, or its subtree’s for containers;
- text/points change that still fits its slot: re-derive; unused instances become hidden slack;
- a create, a move (PARENT_INDEX), or growth past a slot: tail layout from the earliest affected position (Emitter.layout). It assigns positions compactly from an unaffected anchor node to the end of the doc and re-derives every chunk from there. Appends at the end of the paint order (drawing, pasting, generators on top) lay out only the tail.
Unlaid nodes memoize their insertion position in
primStart as -(pos + 2) during a sync, so a bulk create of 662K nodes is linear.
- Per chunk, only what drawing and queries need: render and geometry bounds, LOD sub-area, big-instance indexes, live/selected/remote counts, a 64-bit mask of non-empty LOD groups, and an anchor (
cFirst, the first node reaching into the chunk). LOD group bounds and impostor instances are computed in the staging buffer at derivation time and uploaded, not kept. This also removes the renderer’s CPU copy of the LOD buffer (3.75 B per primitive in the baseline).
- Staging and uploads: the Emitter derives into a fixed 64-chunk staging buffer (3.4 MB) and uploads finished chunks with one
bufferSubData per buffer. GPU buffers grow with copyBufferSubData, so nothing is re-derived on growth.
- Flags are derived: selection, visibility and remote drag slots (ADR 0501) inherit from ancestors during derivation. A selection change rewrites flags only (a flags pass needs no text layout). A selected container flags its whole subtree (preview and outlines).
- Queries re-derive candidate chunks (hit test, marquee, selection bounds of partly selected chunks) into the staging buffer with owner handles. Fully covered marquee chunks use an owners-only pass.
- Text: glyphs come from
TextView bytes with @linorahq/doc/text forEachLine (the shared metrics, so engine and measurer break lines identically) and an SDF atlas of the bundled Inter (ADR 0014). The atlas layout comes from the metrics, so glyph placement does not depend on font loading.
Measured (M4 Pro, tools/bench/run.ts --memory, harness page, against docs/perf/memory-baseline.json)
| Scene | Baseline (E0 code) | E1 | Ratio | Engine CPU | Doc (memoryReport) | GPU |
|—|—:|—:|—:|—:|—:|—:|
| 4,000 pages: 662,190 nodes, 11,028,450 instances | 1,204.9 MB | 617.2 MB | 0.512x | 7.9 MB (12.5 B/node) | 56.4 MB (89 B/node incl. 18.5 MB text) | 500 MB |
| 5M shapes | 879.1 MB | 607.5 MB | 0.691x | 26.5 MB (5.6 B/node) | 298 MB (62.6 B/node) | 227 MB |
JS heap objects: 45K (website) and 29K (shapes), below the 100K limit and flat in node count.
Against the ADR 0024 projection
- Website: E1 617 MB vs ~605 MB projected. GPU and doc match the projection; the extra comes from the canvas backbuffers (+49 MB on the full-window harness page), which the projection left out.
- Shapes: 608 MB vs ~545 MB projected. The projection’s “today” side was ~330 MB low (capacity slack, LOD staging, canvas, per-node editor arrays:
docs/perf/memory-baseline.md), so the ratio is what matters: 0.69x against the measured baseline. The remaining gap is mostly the canvas backbuffers (49 MB) and the doc’s growth slack. The doc is 62.6 B/node (ADR 61.5) because its arrays grow by 1.25x on edits.
- E2 projection (run-length ids, doc idMap 8.8 -> ~0 B/node; chunk metadata is already 0.16 B/primitive, since LOD groups are no longer kept): shapes ~564 MB (0.64x), website ~611 MB (0.51x).
Alternatives
- Per-node instance counts (another 4 B/node): rejected; counts are cheap to recompute from the doc.
- Shift instances on every delete: rejected; holes keep deletes O(chunk), and a later tail layout compacts them.
- Keep LOD group bounds per chunk (2.25 B/primitive) to rebuild impostors without re-deriving: rejected; impostors change only when the chunk is re-derived anyway.
Consequences
- Normal edits cost ~1 ms (re-derive a chunk). A tail layout of the whole website costs ~0.65 s and a full rebuild of 5M shapes ~0.8 s; these are rare (reorder of a huge frame, undo of a large delete) and coalesced per frame.
- A move or resize of more than 200K top-level nodes is refused with a message: each node needs a SET op, and the protocol has no bulk transform op yet. The preview still works.
- Text renders in Inter (ADR 0014) instead of the system font.