0504. Chunk insertion instead of tail layouts
Status: Accepted (2026-11-28, EN2). Amends 0502 decision 2 (positions move only in a tail layout).
Context
ADR 0502 laid out the tail of the paint order from the earliest affected position for every create, move (PARENT_INDEX) or text/path growth past its slot. Appends at the end are cheap. Anything in the middle is not: on the 4,000-page site, drawing into the first page, typing into a text near the start, a layers-panel reorder or a drop into a frame each cost ~0.65 s (a re-derive of up to 11M instances). The editor now does these as everyday hand-editing actions (ED1: text tool, frames that adopt layers, canvas and layers-panel drops of up to 1,000 layers, frame resize).
Decision
- A move leaves a hole and is inserted again. A moved subtree gets
primStart = -1, and its old range is marked for re-derivation (hidden instances, as for deletes). It is then placed like a created subtree.
- Insertions per change set. The roots of unlaid runs are found first; a run is consecutive unlaid siblings under a laid-out parent, such as a paste or a drop of many layers into one frame. Each run, and each node that outgrew its slot, is placed in the gap between the laid-out node before it in pre-order and the next one. When the gap is too small,
openChunks(q, deficit) opens whole chunks at the chunk of position q:
- every
primStart >= q shifts by k * 1024. That is one linear pass over the per-node array, about 1 ms for 700K nodes. Nodes before the insertion point that sit exactly at q (the root, groups, empty texts) are restored by a walk back in pre-order;
- chunk metadata from q’s chunk on moves k chunks up (
RenderScene.shiftChunks). Big-instance indexes are chunk-relative, so they move as they are;
- instances, flags and LOD slots move on the GPU (
GpuSink.moveChunks): copyBufferSubData through an 8 MB temporary buffer, back to front, since a buffer cannot copy onto itself. The opened chunks are hidden until derived;
- only q’s chunk and the opened chunks are re-derived; with ADR 0503, visible ones now and the rest later.
- Slack. The opened room is
deficit + max(32, n/2) instances, rounded up to whole chunks (so at least 1,024). Later keystrokes and drops at the same place re-derive in place with no shift. Typing 300 characters into a text at the start of a 100K-instance doc opened chunks once and never laid out the tail (test).
- Fallbacks. More than 8 insertions in one change set (scattered pastes, generators, a drop into many places) take one tail layout from the earliest position; it also compacts holes. A tail shorter than 64 chunks is laid out too (appends at the end, small docs). Generators and bulk creates keep the memoized linear walk (ADR 0502).
Alternatives
- Shift instances one by one (no whole chunks): every later chunk would need re-derivation for its bounds and LOD groups, so this is the tail layout again.
- An indirection table (paint position -> instance slot): would avoid moves entirely, but the vertex shader would need an extra fetch per instance (a GPU cost on every frame), and LOD runs depend on contiguous chunks. Rejected.
- Append new nodes at the end and fix the paint order later: wrong paint order until a layout. Rejected.
Consequences
- A mid-order create, a move, or the first keystroke past a slot costs about 1 ms of CPU plus a GPU copy of the tail (~0.9 GB of copy traffic for the 11M-instance site: a few ms of GPU time, once). It used to cost ~0.65 s of CPU.
- Holes accumulate: every opening adds up to ~1,024 hidden instances (44 KB of GPU memory). A later tail layout or a page reload compacts them.
RenderScene.holes counts them; automatic compaction in idle time is a follow-up.
- The GPU check (
node tools/bench/gpu-check.ts) compares the GPU buffers with a derivation from the doc after an insertion, a growth and a move, in Chrome.