Skip to content

perf(runtime): wholesale unmount DOM detach + gated de-opt ref walk - #670

Merged
trueadm merged 1 commit into
mainfrom
perf/wholesale-unmount-detach
Aug 9, 2026
Merged

trueadm merged 1 commit into
mainfrom
perf/wholesale-unmount-detach

Conversation

@trueadm

@trueadm trueadm commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Unmount teardown now removes a deleted subtree's DOM once, at the outermost detached block, instead of every descendant block detaching its own marker range node-by-node. Portals are unaffected — they already force self-detach from their foreign targets.
  • The de-opt ref-detach walk (detachDeoptTreeRefs) is now gated on refs actually existing: stamping a descriptor ref sets a monotone flag on the owner block and its parentBlock chain (plus a module-level "ever stamped" flag), and teardown/hide walks skip the per-DOM-node descriptor scan when clear.

Why

The new spa-navigation suite (#614) showed teardown is where Octane loses on full-page navigations. Profiling the production fixtures:

  • A 1024-leaf route teardown issued 6,140 removeChild calls (tsrx) / 8,186 (jsx); React does 1, Solid does 1 replaceChild. Raw removeChild alone was 21% of the tsrx navigation profile.
  • detachDeoptTreeRefs was 20.4% of the octane-jsx profile (~1ms per teardown) in a tree containing zero refs — it walks every DOM node of each block's deoptNode subtree (nested blocks re-walk the same DOM, so it is O(nodes x depth)).

Both fixes also move deletion semantics closer to React: every deletion cleanup now observes the entire deleted subtree still attached (React's commitDeletionEffects runs all destroys before detaching the single host). Previously a later sibling's cleanup saw an earlier sibling's DOM already removed.

Changes

  • unmountBlockInner: compute removesOwnDom (this call will remove the block's own range or clear the root container) and recurse with detachDom=false in that case, so only the outermost detaching block touches the DOM. The existing detachDom=false contract (batchClearItems, clearChildContent, parked blocks) is unchanged, as is the forced self-detach for portalSlotSlot and hidden-try content.
  • Block.deoptRefs (new monomorphic boolean field) + noteDeoptRef called from applyDeoptProp's ref branch (covers fresh apply, reuse/diff re-attach, and hydration adoption). Gates the deoptNode walks in unmountBlockInner and batchClearItems; a module-level DEOPT_REFS_STAMPED flag short-circuits detachDeoptTreeRefs itself for every other call site (clearChildContent sweeps, suspense hide/reveal collection, hydration recovery).

Benchmarks (same machine, same day, 20 iterations, medians in ms)

node benchmarks/bench.mjs spa-navigation — baseline was main earlier the same day; candidate ran twice (a concurrent-load run and a clean run; clean run shown, sd 0.05–0.10 on the sub-ms ops):

op tsrx before tsrx after jsx before jsx after react solid
nav_teardown 1.20 0.60 3.70 0.90 0.80 0.60
nav_deep 2.20 1.80 7.00 4.50 1.50 1.90
nav_mount 1.70 1.70 3.90 4.10 1.30 1.80
nav_deep_6x 14.80 11.20 47.40 29.80 10.20 13.50

nav_teardown halves and now ties Solid (was 2.24x); nav_deep edges past Solid and nav_deep_6x (the mobile signal) is now clearly ahead of it. nav_mount is unchanged — expected, the mount path is untouched, which is itself evidence the deltas are the teardown mechanism and not noise. The deterministic work counters (work.mjs) are byte-identical to baseline — renderBlock/unmountBlock/unmountScope counts unchanged — so the semantic work is proven equal; only DOM calls and the ref walk were removed. All semantic gates (shell/layout survival by node identity) and work gates pass. list-clear and effectful-list (the other teardown-heavy suites) ran without anomaly; their clear paths already used the batched contract this change extends.

Validation

  • pnpm test — 1,610 files / 17,249 tests pass (includes the octane-prod project)
  • pnpm typecheck
  • pnpm format:files:check on changed files
  • targeted: new unmount-detach.test.ts (deletion cleanups observe the whole subtree attached — failed before the fix: second cleanup sees first=false), new deopt-suspense-ref.test.ts (hide/reveal detach/re-attach across the gated walk), deopt-host-ref.test.ts extended with an update-added-ref case — 32/36 of these fail if the gate is deliberately broken (verified), plus portal, ref-cleanup/dispose, suspense-ref-regressions, effect-ordering-deletion suites.

Risk / follow-ups

  • Observable divergence-toward-React: deletion cleanups now see sibling DOM still attached; MutationObserver users see one subtree removal instead of per-node removals. Both match React.
  • A cleanup that removes/moves its own block's markers mid-teardown now forfeits the wholesale removal for that range (guarded on the live parent) — same containment contract as before, noted in a comment.
  • deoptRefs is monotone; a region that once had a ref keeps paying the (previous) walk cost. Finer invalidation was not worth the bookkeeping.
  • Follow-ups from the same audit (not in this PR): static multi-hole marker elision (mount-side; 4,092 comments per 1024-leaf mount), and the .tsx dialect lowering gaps (multi-return bodies de-opt; single-return components keep the generic descriptor ABI).

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


Note

Medium Risk
Changes core unmount/teardown in the runtime: DOM removal order and what deletion cleanups can observe now match React, which can affect MutationObserver users and any code that assumed per-block incremental detach.

Overview
Teardown now strips a deleted subtree's DOM in one pass at the outermost block that owns the marker range (removesOwnDom in unmountBlockInner), instead of every descendant block calling removeChild on its own slice. Descendant unmounts run with detachDom=false in that case so layout/deletion cleanups still see sibling DOM connected until the parent removes the range — matching React's commitDeletionEffects (destroys before a single host detach). Portals keep self-detaching from foreign targets.

De-opt ref teardown is gated: stamping a descriptor ref sets monotone Block.deoptRefs up the parentBlock chain and a module DEOPT_REFS_STAMPED flag; detachDeoptTreeRefs and related walks in unmountBlockInner / batchClearItems skip the per-DOM-node scan when a subtree never had refs.

Adds regression tests for deletion cleanup observation (unmount-detach), suspense hide/reveal on de-opt refs (deopt-suspense-ref), and refs added on a later update (deopt-host-ref).

Reviewed by Cursor Bugbot for commit 3197f02. Bugbot is set up for automated code reviews on this repo. Configure here.

A deleted subtree's DOM is now removed once at the outermost detached
block instead of per-descendant marker range (6,140 removeChild -> ~3 on
a 1024-leaf route teardown), and detachDeoptTreeRefs is skipped for
subtrees that never stamped a descriptor ref. Deletion cleanups now
observe the whole deleted subtree still attached, matching React's
commitDeletionEffects order. spa-navigation nav_teardown 1.20 -> 0.60ms
(ties solid), nav_deep_6x 14.80 -> 11.20ms; work counters unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Aug 9, 2026
@trueadm
trueadm marked this pull request as ready for review August 9, 2026 21:28
@trueadm
trueadm merged commit 217a0b5 into main Aug 9, 2026
52 checks passed
@trueadm
trueadm deleted the perf/wholesale-unmount-detach branch August 9, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant