0107. DELETE inverse: RESTORE blocks per parent
Status: Accepted (2026-12-01). Supersedes point 3 of 0101 for DELETE’s inverse. Changes packages/doc only: no wire, rule or snapshot change (PROTOCOL_VERSION and DOC_RULES_VERSION unchanged).
Context
ADR 0101 made DELETE’s inverse one RESTORE per deleted root, in reverse delete order. Each root’s block was encoded on its own, and the encoder allocated a position map the size of the whole document per block. Deleting 2M selected shapes in the demo therefore allocated and zeroed about 2M x 8 MB: one Delete froze the page for 30 s (28 s in the encoder and the garbage collector).
Undo was quadratic as well whenever other layers sat below the deleted ones under the same parent, as the demo’s Welcome page does. RESTORE links each root under its parent by key, searching from the parent’s last child. Roots restored in reverse order land between those layers and every root restored before them, so each search walked all of them: undoing a delete of 100,000 shapes took 7.6 s, and 2M would take about an hour.
Decision
- One RESTORE block per run of roots under the same parent, at most about 10,000 nodes (
RESTORE_BLOCK_NODES). A block that encodes larger than MAX_MESSAGE_BYTES / 4 is halved until it fits, so text and points do not push a single op past the 4 MiB message limit. The per-batch node cap (MAX_BATCH_EXTRAS, 1M) is far above what one message carries.
- A listed node under another listed node travels in that node’s block and gets no block of its own. Before this was implicit: deleting the ancestor first made the descendant’s id missing.
- Roots in sibling order. Within a parent, roots are written in sibling order: id order when it matches, as it does for layers drawn or generated on top, otherwise sorted by key. Blocks are applied in that order, and
insertBlock links each root starting from the previous root, so a block of roots between existing siblings is linked in O(n + gaps).
- The encoder keeps a path stack instead of a handle-sized position map for parent references: in pre-order a node’s parent is on the path from its root. The bytes are identical.
RESTORE itself is unchanged: the block format already allowed many roots under one parent, and the server checks every node of a block (ADR 0101 point 2).
Alternatives
- Keep one block per root and only fix the encoder’s allocation. That removes the 30 s freeze, but it still sends one op per deleted node on undo, and the order still makes undo quadratic.
- One block for the whole delete. 2M nodes do not fit one message (about 80 MB against 4 MiB).
- Make large deletes not undoable, like generation and Clear canvas. Delete is an ordinary, undoable edit; the cost was in the encoding, not in keeping the undo.
Consequences
- Deleting 2M shapes in the demo: 30.1 s to 1.6 s (M4 Max, Chromium). In the document core alone (Node, 2M shapes above 58 other layers): delete 1.8 s, undo 1.2 s; before, undo took 0.5 s for 25,000 shapes, 2.0 s for 50,000 and 7.6 s for 100,000.
- A DELETE’s inverse is now a few ops per parent instead of one per root. Callers that applied or sent the inverse as a list (the undo stack, the sync client’s reverts) are unaffected: they treat it as a list of ops.
- Ids against sibling order (layers reordered after creation) cost a sort when a large delete is encoded.