0503. Deferred, budgeted chunk derivation
Status: Accepted (2026-11-28, EN2). Amends 0502 (when chunks are derived, not how).
Context
ADR 0502 re-derived every dirty chunk in the frame that drained the change. A remote batch of 10K SETs therefore cost 11.3 ms when clustered and 455 ms when spread over the 4,000-page site (WP-E1: ~6,000 text-heavy chunks at ~58 us each), against a target of <= 4 ms of main-thread time (render-engine.md). Most of those chunks are off screen, and nothing drawn depends on them until they come into view.
Decision
- Only what can be seen is derived in the frame.
DocSync.processDirty(view, budget) derives, in order:
- flag-only work everywhere (selection and remote slots; never deferred, drags need it);
- chunks that can be seen this frame: bounds in the padded view, or holding selected instances while a drag preview is live, or remote-slot instances while a slot is live;
- the rest only in idle callbacks (
requestIdleCallback, else a 0 ms timer), off screen.
Engine.update gives the whole sync (drain, marks, widening, visible derivation) FRAME_DERIVE_MS = 2 ms per frame and asks for another frame (onInvalidate) when visible work is left. Idle callbacks use up to IDLE_DERIVE_MS = 4 ms each.
- Culling stays right for deferred chunks: widening. A changed node whose render bounds may move (GEOMETRY, VISIBILITY, TEXT, and STYLE of lines, arrows and paths) is queued. Before deciding what is visible,
processDirty widens the node’s chunks’ render bounds by its new bounds, within the same budget. Emitter.nodeBounds runs the derivation without writing:
- shapes use the same f32 math as
instRenderBounds;
- text is bounded per line, not per glyph;
- with no rotation column, the world offset is the sum of x/y down the ancestor chain;
- a container’s bounds come from a walk of its subtree.
Chunks already in view skip widening, since they are derived now anyway. Derivation then sets exact bounds. While the queue is not empty, culling can lag by a frame (the engine draws again at once).
- Queries tolerate deferred chunks. Hit tests and marquee treat a stale chunk as a candidate whatever its counts say, and derive it from the doc as before. Selection bounds use the derive path for stale chunks instead of their cached geometric bounds.
contentBounds reads the widened (conservative) bounds.
- Deletes and moves mark every chunk their old instances reach into: the start chunk plus following chunks whose anchor (
cFirst) is the node. So no stale part stays on screen while its first chunk is deferred.
- Derivation got cheaper too. The per-instance bounds are inlined in
finalizeChunk, and the LOD grouping loop no longer calls Math.max/Math.min. Owners and lock bits are written only for queries, and the flags word as one u32. GPU output is byte-identical; a full re-derive is 0.63 s -> 0.41 s (website) and 0.71 s -> 0.45 s (5M shapes) in Node.
10K SETs, sequenced apply, medians of 5. Cost of the applying frame (doc apply + engine sync), then later frames and idle work:
| Case |
E1 |
EN2 frame |
later visible frames |
idle (off screen) |
| website, clustered, fill, page view |
12.4 ms |
1.4 ms |
0 |
8 ms |
| website, clustered, x/y, page view |
12.4 ms |
3.0 ms |
1 (1.8 ms) |
8 ms |
| website, spread, fill, page view |
375 ms |
2.6 ms |
0 |
314 ms |
| website, spread, x/y, page view |
364 ms |
3.2 ms |
3 (5.1 ms) |
312 ms |
| website, spread, fit-all view |
~380 ms |
3.3 ms |
~150 (310 ms) |
0 |
| 5M shapes, spread (doc ~2 ms of it) |
- |
3.9-4.5 ms |
0-183 |
0-430 ms |
Full table and the 5M-shape rows: docs/perf/en2.md.
Alternatives
- Per-node instance patching (upload only a changed node’s instances): exact for zoomed-in views, but a spread batch still needs ~10K separate uploads. LOD impostors and chunk bounds would still need the whole chunk, so it saves nothing in the fit-all view. Rejected.
- Widening at mark time (no queue): simpler, but 10K spread geometry SETs then cost ~5 ms in the applying frame. The queue moves that work under the frame budget.
- Treating deferred chunks as visible until derived: correct, but derives everything at once, which is the 455 ms case.
Consequences
- The applying frame stays under 4 ms for 10K remote SETs in every website case and for clustered edits on 5M shapes; spread edits on 5M shapes come to 3.9-4.5 ms, about 2 ms of it the doc’s apply. Total work is not smaller when the user looks at everything: in the fit-all view every chunk is visible, and the work is spread over frames (progressive LOD refresh, ~0.3 s for a spread batch).
- Off-screen work happens in idle time; a background tab defers it further. Nothing is lost:
pendingWork() and the next frames or idle calls finish it.
- Stale chunks may be drawn through a merged culled gap (
MERGE_GAP). That shows old content off screen only, which is clipped.
- Hidden cost: queued widening uses ~4 B per queued node until processed.