Linora

0024. Single-copy client memory: the doc’s columns are the render source

Status: Accepted (2026-11-27). Supersedes 0003 (storage layout and memory numbers) and 0013 (engine phasing).

Amendment note (2026-11-27, WP-E1): implemented as designed. Measured E1 client totals are 0.512x (4,000 pages) and 0.691x (5M shapes) of the measured baseline. The “Today” columns below underestimate the real baseline (capacity slack, the renderer’s CPU LOD copy, canvas backbuffers: docs/perf/memory-baseline.md). Measured parts and the updated E2 projection: 0502.

Context

Memory is the user’s top priority. The previous plan kept the engine’s CPU primitive mirror next to a new ~80 B/node document: 5M shapes +130% at first integration and +58% after optimization; website +10%. New hard requirement for both benchmark scenes (4,000 website pages = 682K nodes / 11M primitives; 5M random shapes): total client memory <= +10% vs today at first integration (E1), <= today after optimization (E2).

Decision

  1. One CPU copy of node data: the doc’s columns. The engine keeps no CPU primitive array, no CPU flags array and no primitive -> object map. For each dirty 1,024-instance chunk it derives GPU instances from the doc into a reusable 40 KB scratch buffer:
    • shapes: world transform = parent chain + local geometry;
    • text: glyphs from TextView + bundled metrics;
    • paths: segments from PointsView. It then computes chunk bounds and LOD impostors from the scratch and uploads with bufferSubData. The GPU instance buffer stays the only full-scene instance copy, in VRAM, unchanged in format and draw path.
  2. Flags derived: the selected/hidden per-instance flags are generated per chunk from node visibility and the editor’s selection bitset when they change.
  3. Engine per node: only primStart (Int32). Text/path instance counts live in their records. Per chunk: bounds, LOD groups and the first node handle, from which hit testing and marquee walk nodes in pre-order using doc geometry and cached container world offsets.
  4. Compact doc layout (contract: NodeColumns in shared/src/doc-api.ts):
    • dense hot columns: parent/nextSibling/prevSibling (Int32), x/y/w/h (Float32), kindFlags (1 B: 5-bit kind + 3 flags);
    • first/last child only in a container table;
    • sibling keys packed inline in 8 B (up to 10 base-62 chars, 6 bits each, order-preserving; longer keys in an arena);
    • lazy columns (absent until some node differs from the default): rotation, opacity (u8, 1/255 precision);
    • interned columns: fill (u16 index into a color table); stroke style = color + width (lazy, u16); corner radius (lazy, u16). Indices widen to u32 past 65,535 distinct values;
    • text in a UTF-8 arena + small records; points in a Float32 arena; names interned;
    • sparse props in typed-array open-addressing tables; no JS object or Map entry per node.
  5. Ids: E1 uses a u32 counter column + per-client counter -> handle tables (8 B/node). E2 replaces both with a run-length id map: bulk creates are contiguous id/handle runs, with an override table for reused handles, so ~0 B/node for generated content.
  6. E2 also stores chunk metadata in Float32, rounded outward (2.4 -> 1.4 B/primitive).

Byte budget, 5M random shapes (bytes per node = per primitive)

| Component | Today | E1 | E2 | |—|—|—|—| | Engine CPU primitive mirror (10 x u32) | 40 | 0 | 0 | | Engine CPU flags / primitive -> object map | 4 + 4 | 0 | 0 | | Engine object table (today start/len/kind/alive/sel/mark; target primStart) | 12 | 4 | 4 | | Chunk metadata (bounds, LOD groups, first node) | 2.4 | 2.4 | 1.4 | | Doc id + id -> handle index | - | 8 | ~0 | | Doc tree (parent, next, prev) | - | 12 | 12 | | Doc sibling key (inline) | - | 8 | 8 | | Doc kindFlags | - | 1 | 1 | | Doc x, y, w, h | - | 16 | 16 | | Doc rotation (lazy; allocated because 15% are rotated) | - | 4 | 4 | | Doc fill / stroke style / corner radius (interned u16) | - | 6 | 6 | | Doc opacity (lazy; all default -> none) | - | 0 | 0 | | Editor selection bitset | (in object table) | 0.125 | 0.125 | | CPU | 62.4 | 61.5 | 52.5 | | GPU: instance 40 + flags 4 + impostors 3.4 (unchanged) | 47.4 | 47.4 | 47.4 | | Total per node | 109.8 | 108.9 (-1%) | 99.9 (-9%) | | Scene total | ~549 MB | ~545 MB | ~500 MB |

Byte budget, 4,000 website pages (682K nodes, 11M primitives, ~270K text nodes, ~12M text chars)

| Component | Today | E1 | E2 | |—|—|—|—| | Engine CPU primitive mirror + flags + primitive map (48 B x 11M) | 528 MB | 0 | 0 | | Engine object table (12 B -> 4 B per node) | 8.2 MB | 2.7 MB | 2.7 MB | | Chunk metadata (2.4 -> 1.4 B/primitive) | 26.4 MB | 26.4 MB | 15.4 MB | | Names Map (truncated text + frame names) | ~22 MB | - | - | | Doc nodes (E1 51 B/node: ids 8, tree 12, key 8, kindFlags 1, xywh 16, interned paint 6; E2 43 B) | - | 34.8 MB | 29.3 MB | | Text: record index (lazy 4 B/node) + records (~20 B x 270K) + UTF-8 arena (full content) | - | 20.1 MB | 20.1 MB | | Names (interned; frames only) | - | 0.2 MB | 0.2 MB | | CPU (per node) | ~585 MB (858 B) | ~84 MB (123 B) | ~68 MB (99 B) | | GPU (47.4 B/primitive, unchanged) | ~521 MB | ~521 MB | ~521 MB | | Total | ~1,106 MB | ~605 MB (-45%) | ~589 MB (-47%) |

Budgets exclude growth slack. Today’s arrays grow by 1.6x, so its measured baseline is higher. Target code sizes exactly on snapshot load and grows by <= 1.25x on edits.

Measurement (acceptance)

Performance impact (render path unchanged)

Alternatives

Consequences