Linora

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

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

Measured (Node, node tools/bench/remote-apply.ts, real doc + derivation, fake GPU sink)

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

Consequences