Skip to content

fix(three): stop retaining every committed host batch on application roots - #1653

Merged
trueadm merged 1 commit into
mainfrom
fix/three-root-commit-history
Oct 3, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/three-root-commit-history

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Every @octanejs/three root created by createRoot, Canvas, or the @octanejs/three/testing harness leaked its commit history.

  • Each accepted host batch was appended to container.commits and never released.
  • ThreeHostEnvironment.recordCommits defaults on, and root.ts never turned it off.
  • A long-lived scene therefore retained every replaced props object, listener value, and destroy record for its whole lifetime.
  • The Three bench's heap grew about 170 KB per 1,000-node remount. Heap snapshots traced the growth to driverState.commits.

Application roots now set recordCommits: false. A container created directly with createThreeContainer keeps the documented recording default, which the low-level driver tests use.

The update_1k guard ratchets to 1.05. The leak had been flattering it.

  • Keeping the previous mount batch alive across the runner's forced gc() made the next 1,000-mesh update about 25% faster: 0.77× R3F with the leak, 0.989–1.000× without.
  • The effect needs the batch itself to survive. Push-then-pop recovers nothing; keeping only the last batch recovers most of it.
  • I ruled out deopts (traces are identical), allocation-site pretenuring (--no-allocation-site-pretenuring leaves the gap), and heap size (a 300k-object ballast makes it slower, not faster).
  • The note asks for update-path allocations to be cut and the guard restored to 1.0.

Validation

  • pnpm format:files:check, pnpm sync clean, pnpm react-parity:validate (hooks.test.ts hash refreshed)
  • pnpm typecheck:files on the changed sources
  • pnpm test (not run in full locally; CI runs it)
  • targeted: three project testing.test.ts and hooks.test.ts.
    • The new regression test "keeps no accepted host batch history on a configured root" fails before the fix: 7 retained batches after 5 updates.
    • hooks.test.ts asserted commits.length > 0 as a proxy for a live root; it now asserts the instance's root scene.

Risk / follow-ups

  • instance.root.commits is now empty on application roots. Only tests read it, and R3F's instance.root has no equivalent history.
  • Follow-up: cut Octane's update-path allocations (batch freezing, compact leaf transaction), then restore update_1k to 1.0.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes core Three root host behavior and public diagnostic commits on application roots; benchmark guard is temporarily relaxed until update allocations are reduced.

Overview
Fixes a memory leak on long-lived @octanejs/three scenes: roots from createRoot, Canvas, and the testing harness now set recordCommits: false, so accepted host batches are no longer appended to container.commits for the root’s lifetime. That stops retaining every replaced props object, listener value, and destroy record. createThreeContainer still records batches by default unless the environment disables it; docs on ThreeHostEnvironment.recordCommits spell out the split.

Tests no longer treat non-empty commits as proof of a live root (e.g. hooks assert instance.root.scene instead). A new testing regression asserts commits stays empty after several updates. The update_1k benchmark ceiling moves 1.0 → 1.05 with a note that the old leak made updates look faster under forced gc(); follow-up is to trim update-path allocations and restore 1.0.

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

…roots

Every root created by createRoot, Canvas, or the testing harness appended each
accepted host batch to container.commits and never released it, because
recordCommits defaulted on and root.ts never turned it off. A long-lived scene
kept every replaced props object, listener value, and destroy record. The Three
bench retained about 170 KB per 1,000-node remount. Application roots now set
recordCommits: false; a container created directly keeps the documented
default.

The update_1k guard ratchets to 1.05. The leak flattered it: keeping the
previous mount batch alive across the runner's forced gc() made the next
update about 25% faster (0.77x R3F). Without that history it measures
0.989x-1.000x.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 3, 2026
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 09:24
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares dc3e18023 with 40b32a6ae on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 3.36 3.41 +1.5% ±12% ⚪ within noise
octane-tsrx replace 6.93 7.31 +5.5% ±16% ⚪ within noise
octane-tsrx add 3.19 3.37 +5.6% ±11% ⚪ within noise
octane-tsrx update 0.31 0.41 +32.3% ±46% ⚪ within noise
octane-tsrx select 0.16 0.14 −12.5% ±74% ⚪ within noise
octane-tsrx swap 0.31 0.35 +12.9% ±53% ⚪ within noise
octane-tsrx remove 0.34 0.30 −11.8% ±42% ⚪ within noise
octane-tsrx runlots 29.21 29.04 −0.6% ±20% ⚪ within noise
octane-tsrx select_lots 0.26 0.22 −15.4% ±61% ⚪ within noise
octane-tsrx clear 40.83 39.25 −3.9% ±29% ⚪ within noise
octane-jsx run 4.30 3.49 −18.8% ±36% ⚪ within noise
octane-jsx replace 6.39 6.98 +9.2% ±10% ⚪ within noise
octane-jsx add 3.14 3.11 −1.0% ±11% ⚪ within noise
octane-jsx update 0.46 0.48 +4.3% ±38% ⚪ within noise
octane-jsx select 0.30 0.31 +3.3% ±42% ⚪ within noise
octane-jsx swap 0.55 0.55 −0.0% ±24% ⚪ within noise
octane-jsx remove 0.57 0.56 −1.8% ±39% ⚪ within noise
octane-jsx runlots 28.70 27.04 −5.8% ±11% ⚪ within noise
octane-jsx select_lots 2.47 2.39 −3.2% ±24% ⚪ within noise
octane-jsx clear 41.18 41.15 −0.1% ±9% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit a0df409 into main Oct 3, 2026
74 of 78 checks passed
@trueadm
trueadm deleted the fix/three-root-commit-history branch October 3, 2026 09:59
trueadm added a commit that referenced this pull request Oct 3, 2026
…in (#1665)

Fixing the commits leak (#1653) took update_1k from 0.77x to parity with
React Three Fiber. The leak had been keeping old host batches alive, and with
them the hidden classes Object.freeze gives batches and commands. V8 reaches
those classes only through weak transitions. Once no batch survived the
runner's forced gc(), they were collected, and every optimized reader of a
command deoptimized ("embedded weak objects cleared") and re-warmed inside
the timed run.

- Pin one frozen exemplar of the batch and of each common command on the
  universal root class, so their classes survive a full collection.
- Move the per-leaf bodies of the compact-leaf update path and the Three
  update-only driver into functions called per item. Their loops run once per
  commit and stay in Maglev; the per-item functions reach TurboFan.
- Derive, compare, and freeze each compact leaf in one pass, and freeze each
  command as it is emitted.
- Add tests that break exactly one update-only or compact-pairing guard each.
- Restore the update_1k guard to 1.0.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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 fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant