Skip to content

perf(three): update retained meshes faster than React Three Fiber again - #1665

Merged
trueadm merged 4 commits into
mainfrom
perf/three-update-path
Oct 3, 2026
Merged

trueadm merged 4 commits into
mainfrom
perf/three-update-path

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Three's update_1k (1,000 in-place mesh position updates) is faster than React Three Fiber again: 0.72×–0.81× R3F across three bench.mjs --ratios three-renderer runs, up from about parity. The guard goes back from 1.05 to 1.0.

Why #1653's leak made updates 25% faster. It wasn't heap layout.

  • Object.freeze gives each frozen batch and command its own hidden class. V8 reaches that class from the unfrozen one only through a weak transition.
  • Batches are short-lived. Once no batch survives the runner's forced gc(), those classes are collected.
  • Every optimized function that reads commands, in core and in the Three driver, then deoptimizes with "embedded weak objects cleared" and re-warms inside the timed run.
  • The leak kept old batches, and therefore those classes, alive.
  • Evidence: adding only the pinned exemplars below to the pre-change code reproduces the leak-era 0.77×.
  • These deopts don't appear as --trace-deopt "bailout" lines. They show as "marking dependent code … for deoptimization".

Changes

  • Pinned batch shapes (universal-core.ts). One frozen exemplar of the batch and of each common command (update, create/recreate, insert/move, remove, destroy) is held by a static field on the universal root class, so the classes survive a full collection. Each literal must keep the property order the commit paths emit; a comment says so. The minified bundle keeps the static.
  • Per-item functions (compactLeafCommand and isCompactLeafRecord in core; isDirectMeshUpdate and applyDirectMeshUpdate in the Three driver). Each update runs these loops once, so they stay in Maglev. Their per-item bodies, called 1,000 times, reach TurboFan.
  • One pass per leaf. Each compact leaf is derived, compared, classified and frozen in one pass. Compact lists exist only without a transport or prop codec, so deriving props can't throw or reach the driver. Each command is frozen as it's emitted, which drops the second freezing walk.
  • Behavior is unchanged: same checks in the same order, same reads of public Three objects (scene.children[index] is still read per command), and updates.classify still runs only after pairing succeeds.

Measurements (local, Chromium 149)

update_1k vs R3F
bench.mjs --ratios three-renderer, before ~0.96× (#1653 reported 0.989–1.000×)
same, after (3 runs, last on current main) 0.72×, 0.80×, 0.81×
interleaved A/B following run.mjs's op order and warmups: base / restructure only / restructure + pinning 1.07–1.10× / 0.87–0.91× / 0.77–0.79×

All eight three-renderer guards pass on current main. The other runtime ops are unchanged within noise.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on every changed file passes
  • pnpm typecheck: not run repo-wide; pnpm typecheck:files on the changed sources and the driver test passes
  • pnpm test: not run in full; CI runs it
  • targeted tests:
    • three project: driver, root, testing, hooks (58 passed)
    • octane project: universal-renderer, universal-host-sdk, universal-event-scope, universal-host-bindings, universal-native, universal-activity (241 passed)
  • node benchmarks/bench.mjs --ratios three-renderer three-bundle-size: PASS
  • pnpm sync is clean; pnpm react-parity:validate is current

New tests. The existing tests broke two update-only guards at once, so removing any single guard went unnoticed.

  • 5 driver tests ("retained root meshes") and 2 compact-list tests each break exactly one guard.
  • Mutation-checked: removing the visibility, order, fromArray, props-shape, id, name-write, key, type or children check, or any of the freezes, fails a test.
  • All new tests also pass on the pre-change code.
  • No public scenario I found reaches the mesh parent, name-writability or record-owner checks with a different outcome. They are kept verbatim.

Risk / follow-ups

  • Bundle size: +221 B gzip. three-bundle-size now measures octane-min 1.032× (ceiling 1.034) and octane-full 1.022× (ceiling 1.024). It passes, but with little headroom.
  • Shape drift: pinning only helps while the exemplars match the command literals. A reordered or new command shape silently loses the benefit; the update_1k guard is what catches it.
  • Scope: the mechanism hits any universal renderer after a major GC, Lynx included, not just this benchmark.

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
Touches the universal commit fast path and Three driver batch application; behavior is intended to be identical but regressions would affect all renderers using frozen host batches, not only the benchmark.

Overview
Restores Three update_1k performance to roughly 0.72–0.81× React Three Fiber (from ~parity) by tuning the universal compact-list commit path and the Three driver’s update-only batch, without changing observable behavior. The benchmark guard maxRatio drops from 1.05 to 1.0.

Universal core pins frozen exemplars of host batches and common commands on UniversalRootImpl so V8 hidden classes and optimized readers survive a full GC after transient batches are collected. Compact keyed leaf updates move per-leaf derive/compare/classify/freeze into compactLeafCommand and isCompactLeafRecord, merging the old two-pass walk into one and freezing each command as it’s emitted via freezeUniversalHostBatchShell.

Three driver splits the update-only fast path into isDirectMeshUpdate and applyDirectMeshUpdate so per-command validation and mesh position/name writes stay in engine-hot functions.

Adds compact-list tests in universal-renderer.test.ts and retained root mesh driver tests that each guard a single check on the fast path. Changeset documents the patch for octane and @octanejs/three.

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

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]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Oct 3, 2026
@github-actions
github-actions Bot marked this pull request as draft October 3, 2026 11:11
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 11:12
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No budget breaches, no added work, and no timing changes beyond ±3%.

Compares 12c32ece2, the merge commit's first parent with cc49d2e4b on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

target operation base ms head ms head/base 95% CI
octane-tsrx select_lots 0.006 0.006 −8.6% −9.1% … −7.6% 🟢 faster
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.167 3.233 −1.4% −3.2% … +1.6% ⚪ within noise
octane-tsrx replace 7.217 6.933 −2.8% −6.5% … +3.8% ⚪ within noise
octane-tsrx add 2.725 2.758 −0.0% −3.6% … +3.4% ⚪ within noise
octane-tsrx update 0.135 0.134 −0.0% −1.9% … +0.9% ⚪ within noise
octane-tsrx select 0.006 0.006 −0.5% −1.6% … +1.1% ⚪ within noise
octane-tsrx swap 0.038 0.037 −0.7% −1.4% … +0.7% ⚪ within noise
octane-tsrx remove 0.019 0.019 −0.3% −1.5% … +1.8% ⚪ within noise
octane-tsrx runlots 27.700 26.433 −4.1% −5.5% … +0.4% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 −8.6% −9.1% … −7.6% 🟢 faster
octane-tsrx clear 47.575 49.500 +3.0% −0.6% … +7.6% ⚪ within noise
octane-jsx run 3.742 3.767 ±0.0% −2.7% … +3.7% ⚪ within noise
octane-jsx replace 7.292 7.717 +2.0% −2.1% … +7.6% ⚪ within noise
octane-jsx add 3.442 3.417 −0.0% −2.8% … ±0.0% ⚪ within noise
octane-jsx update 0.265 0.267 +1.1% −0.6% … +2.1% ⚪ within noise
octane-jsx select 0.151 0.140 −0.5% −1.8% … +0.3% ⚪ within noise
octane-jsx swap 0.245 0.243 −0.8% −2.2% … ±0.0% ⚪ within noise
octane-jsx remove 0.187 0.187 +0.7% −0.0% … +1.4% ⚪ within noise
octane-jsx runlots 31.908 31.508 +0.5% −2.2% … +2.1% ⚪ within noise
octane-jsx select_lots 1.557 1.553 −1.1% −2.2% … +0.5% ⚪ within noise
octane-jsx clear 53.575 54.850 +0.7% −2.1% … +3.8% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx bindings_js_gzip 240,407 240,396 −11 (−0.0%) 🟢
octane-tsrx bindings_js_brotli 204,738 204,641 −97 (−0.0%) 🟢
octane-tsrx bindings_app_gzip 121,728 121,717 −11 (−0.0%) 🟢
octane-tsrx bindings_app_brotli 101,836 101,739 −97 (−0.1%) 🟢

50 other measured values are unchanged.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@vercel
vercel Bot temporarily deployed to Preview – octane-website October 3, 2026 13:26 Inactive
@vercel

vercel Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated
octane-website Skipped Skipped Oct 3, 2026 1:26pm UTC
octane-website-mcp Skipped Skipped Oct 3, 2026 1:26pm UTC

Request Review

@vercel
vercel Bot temporarily deployed to Preview – octane-website-mcp October 3, 2026 13:26 Inactive
@trueadm
trueadm merged commit 425a32a into main Oct 3, 2026
30 checks passed
@trueadm
trueadm deleted the perf/three-update-path branch October 3, 2026 19:29
trueadm added a commit that referenced this pull request Oct 3, 2026
Main gained #1685, which raised 51 budgets for the useLayoutSnapshot
commit-phase priority fix (#1686), and #1681/#1665. Re-measured on main
425a32a: each of #1663, #1664 and #1666 merged alone, and all three
plus #1686 merged together. Each budget is the larger of main's value and
the largest measured value plus 32 (raw/gzip) or 256 (brotli), so the
three perf PRs and #1686 can land in any order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Oct 3, 2026
…nd warm-plan perf PRs (#1687)

Three open runtime perf pull requests fail the PR bench byte gate on main
12c32ec, and landing any two of them stacks past the 32-byte headroom:

- #1664 (de-opt survivor fast path): +809 raw / +290 gzip
  in the bindings app framework chunk, which #1676 did not raise because
  #1639 added that budget key first; +20 raw in every compiled-only app.
- #1666 (warm-plan length guard): +17 raw in every client
  bundle, two `!==` guards.
- #1663 (scoped-value read-once): +2 raw / +30-70 gzip in
  descriptor-renderer bundles, a compression shift from the restructured
  childSlot reads.

Main had already spent part of the headroom: bindings framework gzip
measured exactly its budget and deferred-hydration raw had 8 bytes left.

Each budget is set to the largest value measured across each PR merged
alone with main and all of them merged together (plus #1665, which moves
none of these values), plus 32 for raw and gzip and 256 for brotli. Only
values one of those trees exceeds change.

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 perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant