Linora

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

  1. 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.
  2. 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.
  3. 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).
  4. 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.
  5. 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).
  6. 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.
  7. 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

Alternatives

Consequences