packages/engine)Owner: WP-E (EN2 in wave 3). ADRs 0024 (supersedes 0013’s phasing), 0502, 0503-0506, 0004, 0014, 0002, 0501.
EN2 kept the E1 design below and changed when and how much is derived:
FRAME_DERIVE_MS = 2 ms for the whole sync, and asks for another frame if visible work is left. Off-screen chunks are derived in idle callbacks. Changed nodes whose bounds may move widen their chunks’ bounds first (Emitter.nodeBounds: text per line), so culling stays right. Queries derive stale chunks from the doc as before.ChangeSet.transforms (DC2’s TRANSFORM op) are applied on the GPU by transform feedback over the roots’ instance ranges, or over the selected instances. Bounds and LOD slots of fully covered chunks shift with them, and nothing is derived.docs/perf/en2.md. Tools: node tools/bench/remote-apply.ts (remote batch cost), node tools/bench/gpu-check.ts (GPU buffers vs a derivation from the doc, in Chrome).packages/engine/src/ is a derived cache of the session’s DocCore (ADR 0024, design details in ADR 0502). It holds no CPU copy of instances, flags or LOD quads:
scene.ts (RenderScene): per node primStart (Int32) and a selection bit (plus remote drag slots while peers drag); per chunk render/geometry bounds, LOD sub-area, big-instance indexes, live/selected/remote counts, a non-empty LOD group mask and an anchor node.derive.ts (Emitter): walks the doc in pre-order with an ancestor stack (world matrix, inherited opacity/visibility/selection/lock/remote slot) and writes instances into a 64-chunk staging buffer; finalizes each chunk (bounds, LOD groups and impostors, counts) and uploads with one bufferSubData per buffer. Modes: tail layout, fixed-position re-derive (holes and slack become hidden instances), flags only, hit/owners (queries). Text: @linorahq/doc/text forEachLine + bundled Inter atlas; paths: one line instance per segment.sync.ts (DocSync): drained ChangeSets become re-derived chunk ranges or a tail layout from the earliest affected position; deletes leave holes; bulk creates are linear.query.ts: hit test, marquee and selection bounds re-derive candidate chunks into the stage (hit test ~0.1 ms). A marquee keeps a RectQuery for the drag: each move re-derives only the chunks whose clipped rect changed (ADR 0508).renderer.ts, shaders.ts: the POC draw path unchanged (classify, runs, LOD, outline pass, local xf preview, VS_REMOTE variant for remote slots); uploads come from the stage; buffers grow on the GPU (copyBufferSubData). font.ts: atlas cells and advances from the shared metrics, glyphs rasterized from the bundled Inter once loaded. engine.ts (Engine): update(doc) per frame, draw, selection, queries, remote slots, memoryReport(), readInstances() (tests/parity only). A selection change (DocSync.select) re-flags only the chunks of nodes that join or leave it; above an eighth of the instances it re-flags every chunk.Measured (ADR 0502): engine CPU 7.9 MB for the 4,000-page site (12.5 B/node) and 26.5 MB for 5M shapes (5.6 B/node); client total 0.512x and 0.691x of the baseline. Full rebuild 0.65 s (website) / 0.8 s (5M shapes); a normal edit ~1 ms.
The engine is a derived cache of DocCore (ADR 0024, supersedes 0013’s phasing). It owns GPU buffers and per-chunk metadata and holds no CPU copy of primitive data: the doc’s columns are the render source.
packages/engine/src/
chunks.ts per-chunk metadata: render/geometry bounds, LOD groups, big slots, first node handle, dirty bits
derive.ts chunk -> instances into a 40 KB scratch: shapes from columns + world offset, text -> glyphs
(TextView + bundled metrics), paths -> segments (PointsView), flags from visibility + selection
docSync.ts drains doc ChangeSets -> dirty chunks; primStart maintenance; paint-order rebuilds
query.ts hit test, marquee, bounds from doc geometry (walk nodes from a chunk's first handle)
renderer.ts, shaders.ts, font.ts (bundled font atlas), camera.ts, perf.ts
| Per primitive / node | Today | E1 | E2 |
|—|—|—|—|
| CPU primitive mirror, CPU flags, primitive -> object | 48 B/prim | 0 | 0 |
| Object table -> primStart | 12 B/node | 4 B/node | 4 B/node |
| Chunk metadata (Float64 -> Float32 in E2) | 2.4 B/prim | 2.4 B/prim (measured 0.16) | 1.4 B/prim (measured 0.08, ADR 0506) |
| GPU instance + flags + impostors (unchanged) | 47.4 B/prim | 47.4 B/prim | 47.4 B/prim |
doc.drainChanges(); if empty, return (idle frames: one compare).processDirty(view, budget): widen queued nodes’ chunks; flag passes (selection, remote slots); derive the visible dirty chunks within the frame budget; the rest in idle callbacks.Worst case: a paste into more than 8 places, or undo of a large scattered delete, lays out the tail (~0.4 s for 11M instances). Normal edits touch a few chunks (~40 us each); a 10K-node remote batch costs <= 4 ms in its frame.
VS_REMOTE, linked lazily and used only while a slot is live; the default program is the unchanged POC shader); byte 1 of each instance’s flags word selects the slot (1 + slot, 0 = none). The editor assigns a slot when a peer’s presence has drag, updates its affine per presence update (uniforms only) and clears it when the drag ends (the committed ops arrive right after). Chunks with live-slot primitives are neither culled nor drawn as LOD. In E1 the per-chunk flag derivation fills byte 1 from the slot assignment.docs/perf/memory-baseline.json recorded with tools/bench/run.ts --memory on the unchanged code; move-only refactor into packages; remote-xf slots; engine.memoryReport(). Results: docs/perf/e0.md.docs/perf/e1.md.docs/perf/en2.md. The doc part (run-length ids) is DC2’s.GPU buffers, shaders, chunking, LOD and the drag preview are unchanged, so fps and worst-zoom GPU time are unchanged. Derivation is O(dirty chunks) and skipped when idle; drags stay GPU-only locally and remotely. Every change runs npm run bench (the interleaved before/after gate of ADR 0015: median fps within 5%, headless worst-zoom GPU warn above +10%, fail above +20%), npm run bench -- --memory, and node tools/bench/parity.ts for refactors that must not change behaviour; the absolute worst-zoom GPU budget (<= 3.6 ms, headed) is checked at each milestone.