0508. Incremental marquee query (RectQuery)
Status: Accepted (2026-12-01). Changes packages/engine (query.ts, RenderScene.changeVersion) and the editor’s marquee drag. No wire, rule or memory-baseline change.
Context
Every marquee move called queryRect for the whole rect. For a chunk the rect covers, that re-derives the owner of each instance from the doc (ADR 0024: there is no CPU instance copy to read). With 2M shapes under the marquee, that took about 18 ms of a 20 ms move, although only the band the rect swept since the last move could have changed. The frame rule (filterMarquee) then scanned all 2M results again to find frames.
Decision
RectQuery lives for one marquee drag (Engine.rectQuery(), kept in the drag state). Per chunk it keeps the rect clipped to the chunk’s render bounds and the owners found with it.
- An update re-derives only chunks whose clipped rect changed and copies the owners of the others. This is exact, not an approximation: a chunk’s instances lie inside its render bounds, so the same clipped rect meets the same instances. Chunks along an edge that did not move, and chunks inside the rect both times, are reused.
- Any document change invalidates it.
RenderScene.changeVersion is bumped by every change set the sync applies and by every rebuild; a different version or chunk count re-derives every chunk once. Deferred chunks (ADR 0503) are derived from the doc as queryRect does.
- Frames are counted per chunk, so the marquee applies its frame rule only when it meets a frame, and gets the frames from the few chunks holding one (
frameHandles()) instead of scanning every result.
- A node whose instances span chunks is listed once per chunk;
select() drops the duplicates.
Alternatives
- Throttle selection updates during a large marquee (for example 10 Hz). Each update still costs the full query; the selection visibly lags the rectangle.
- A per-node reference count of the chunks a node is found in, to apply selection deltas instead of whole lists. A per-node array for one gesture, more state to keep right, and the selection list consumers still need the whole list.
- Keep owners per chunk at fixed slots (chunk c at c x 1024) to avoid copying unchanged chunks. The result must be one contiguous list for the selection anyway, so it saves one memcpy of the result.
Consequences
- 2M shapes, 120-move drag in the demo (M4 Max, Chromium): 14.5 s before this work, 5.1 s after the flag fixes, 3.3 s with this. Median pointer-move handler 36 ms -> 20 ms -> 10 ms; long tasks 12.9 s -> 2.4 s -> 56 ms.
- During a drag the query holds two Int32Array buffers of the found handles (8 B per found node; 16 MB for 2M) plus a few numbers per chunk. They go with the drag state; nothing stays after pointer-up.
- What is left per move is linear in the selection size but cheap:
select() (about 3 ms for 2M) and the selection box bounds (about 3.5 ms) re-deriving partially selected chunks along the rect’s edges.
- Tested against
queryRect on random rect sequences, with deletes, hidden, locked, moved and new layers and deferred chunks between moves (packages/engine/test/derive.test.ts).