Skip to content

perf(octane): clear portal marker expandos instead of deleting them - #1900

Merged
trueadm merged 1 commit into
mainfrom
fix/portal-marker-expando-clear
Oct 8, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/portal-marker-expando-clear

Conversation

@trueadm

@trueadm trueadm commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Each portal close allocated a property dictionary for each of the portal's two comment markers. The cause was unregisterPortalEventRange, which removed the markers' event-range fields with delete start.$$portalEventRange / delete end.$$portalEventStart. In Chromium, that delete switches the DOM wrapper to dictionary mode. It does so even when the deleted key is the last one added, so V8's map-rollback fast path doesn't cover these wrappers. This PR assigns undefined instead.

The only reader of these fields is resolvePortalEventOwner. It already compares both with !== undefined, and it re-checks every match against the target's live range set and the marker identity. No reader uses in, hasOwnProperty or Object.keys, and no other package reads them. Event routing is unchanged.

Why

Portal teardown is the next notable cost on benchmarks/portal-swarm after the root-journal work in #1897. The sampling heap profiler put ~137 KB of each open+close cycle (600 portals) in unregisterPortalEventRange.

Shape probe (%HasFastProperties under --allow-natives-syntax, all 1,200 portal markers in the page, 3 cycles):

after open after close
main 1200 / 1200 fast 0 / 1200 fast
this PR 1200 / 1200 fast 1200 / 1200 fast

Changes

  • packages/octane/src/runtime.ts: unregisterPortalEventRange clears both fields instead of deleting them. A comment says why.
  • packages/octane/tests/conformance/event-listener.test.ts, with two fixtures in _fixtures/event-listener.tsrx: three owners share one portal target. Each portal child reveals its button through its own state, so the button is unstamped and is resolved at event time. The test opens and closes the portals in a rotating order, for a compiled child-position portal and a value-position portal. After every step it asserts that each late button routes to its own owner. Once all portals close, a foreign button in the target must route to nothing.
  • Changeset: octane patch.

Measurements

portal-swarm runs in headless Chromium through Playwright.

Allocation per open+close cycle. Unminified production build, sampling heap profiler at 64 B. Semi-space pinned at 256 MB and inlining off, so neither a scavenge nor inlining moves the attribution:

main this PR
cycle total 3,100.6 KB 2,957.9 KB (−142.7 KB, −4.6%)
unregisterPortalEventRange 155 KB 14 KB
childSlot (control) 178 KB 177 KB

Without pinning, childSlot appears to grow 20–36 KB. That growth is GC-cadence noise (pretenuring and memento timing), and it disappears with the semi-space pinned.

Time. Minified production build. Min-of-5 per round, 40 rounds, fresh pages per block in rotated order, with an A/A copy of main as the control:

op run A/A this PR
open_close_cycle 1 1.005 [0.997, 1.019] 0.968 [0.953, 0.982]
open_close_cycle 2 0.995 [0.987, 1.003] 0.985 [0.968, 0.992]
open_all 1 1.008 [0.994, 1.016] 0.999 [0.986, 1.016]
open_all 2 0.992 [0.976, 1.001] 1.000 [0.978, 1.019]

open_all never reaches unregister, so it stays flat as expected. --log-deopt over 40 cycles gives the same deopt set as main, plus one expected "dependent field type changed": the fields' tracked type generalizes once when undefined is first stored.

Bytes. Raw grows 4 B wherever the runtime ships the portal code: the minifier wraps an assignment inside && in parentheses, which delete didn't need. Gzip changes by 0 or −1 B (cli-spa-starter, root-static, hooks-state, hydrate-root, binding-radix vite/esbuild, binding-mantine-hooks-vite, measured against main). run-minimal.mjs --budgets passes every scenario, and run.mjs --budgets octane-tsrx octane-jsx reports all 6 application budgets holding. The ratchet still sees about 70 B of slack in many scenarios. That slack is #1891's budget-first raise for #1875, not this diff.

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on the changed files.

  • pnpm typecheck: not run repo-wide. pnpm typecheck:files passes on the changed runtime, test and fixture.

  • pnpm test: not run locally, because many sessions share the machine. CI covers it.

  • targeted tests, octane + octane-prod projects, on the rebased head: conformance/event-listener, portal-events, portal, portal-value, conformance/fragment-future-children (198 passed). Before the rebase this also included audit-events-portals, portal-into-deopt-host and transition-swap-portal (246 passed).

  • The new test fails on broken variants of unregisterPortalEventRange:

    • dropping the target's whole range set: both placements fail;
    • decrementing the portal-target count on every unregister: child position fails.

    It passes on main, as it should for a behavior-preserving change. It also passes with the two clearing lines removed, or with unregister turned into a no-op. Torn-down markers always leave the target before unregister runs, and the reader re-checks set membership, so no consumer can observe the clearing. It stays as reference hygiene: a retained marker must not pin a closed portal's block.

  • perf-review: node scripts/perf-review-scan.mjs reports no candidates. The change removes a delete from a teardown path (the pattern the performance-audit rules forbid on hot records). Evidence is the shape probe and allocation numbers above.

  • pnpm sync: clean.

Risk / follow-ups

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

Low Risk
Behavior-preserving hot-path optimization in portal event teardown; routing logic is unchanged and new tests lock down shared-target open/close routing.

Overview
Portal teardown no longer uses delete on the two comment-marker expandos in unregisterPortalEventRange; it sets $$portalEventRange and $$portalEventStart to undefined instead. That avoids Chromium/V8 putting DOM wrappers into dictionary mode on every close (fewer allocations on portal-swarm; routing is unchanged because resolvePortalEventOwner already treats cleared fields as absent).

Conformance coverage adds toggled portal fixtures (ToggledChildPortal / ToggledValuePortal) and a shared-target test: three owners open and close portals on one target while late-rendered portal buttons must still route to the correct owner, and closed ranges must not claim stray DOM.

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

unregisterPortalEventRange deleted $$portalEventRange and $$portalEventStart
from the portal's comment markers. Deleting an expando from a DOM wrapper
switches it to dictionary mode, so every portal close allocated a property
dictionary per marker. Assign undefined instead; resolvePortalEventOwner
already reads both fields with `!== undefined` and re-checks membership.

Adds a shared-target event-routing test that opens and closes portals at
child and value position around late, unstamped portal content.

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 8, 2026
@trueadm
trueadm marked this pull request as ready for review October 8, 2026 14:14
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

  • ❌ bundle-size: 8 value(s) exceed their committed budget
  • ❌ bundle-reachability: 21 value(s) exceed their committed budget
  • 🟡 js-framework: 1 timed operation(s) slower beyond ±3%
  • 🔴 bundle-size: 28 value(s) increased within budget
  • 🔴 bundle-reachability: 29 value(s) increased within budget

Compares 90a57d820, the merge commit's first parent with d3cb36262 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 +6.7% +5.5% … +9.0% 🟡 slower
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.775 2.775 ±0.0% −3.5% … +3.6% ⚪ within noise
octane-tsrx replace 6.783 7.050 +0.7% −4.0% … +3.0% ⚪ within noise
octane-tsrx add 2.575 2.508 ±0.0% −3.8% … +1.9% ⚪ within noise
octane-tsrx update 0.131 0.130 −1.8% −2.9% … +2.2% ⚪ within noise
octane-tsrx select 0.006 0.005 −1.4% −3.3% … +0.3% ⚪ within noise
octane-tsrx swap 0.032 0.032 −0.7% −1.7% … +0.3% ⚪ within noise
octane-tsrx remove 0.017 0.017 +4.4% −3.7% … +8.2% ⚪ within noise
octane-tsrx runlots 23.517 23.067 −2.2% −4.0% … +0.6% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +6.7% +5.5% … +9.0% 🟡 slower
octane-tsrx clear 41.767 42.433 +0.2% −1.0% … +1.0% ⚪ within noise
octane-jsx run 3.217 3.183 −2.9% −3.1% … −0.0% ⚪ within noise
octane-jsx replace 5.800 5.750 −2.3% −4.8% … +1.7% ⚪ within noise
octane-jsx add 3.067 3.075 ±0.0% −0.0% … +3.2% ⚪ within noise
octane-jsx update 0.241 0.244 +0.4% −0.7% … +2.9% ⚪ within noise
octane-jsx select 0.127 0.125 −0.6% −2.2% … +0.5% ⚪ within noise
octane-jsx swap 0.209 0.210 −0.3% −0.6% … +1.2% ⚪ within noise
octane-jsx remove 0.162 0.164 +0.6% −0.3% … +3.1% ⚪ within noise
octane-jsx runlots 26.308 26.075 −0.4% −2.4% … +1.1% ⚪ within noise
octane-jsx select_lots 1.402 1.441 +1.2% +0.6% … +2.4% ⚪ within noise
octane-jsx clear 47.450 48.075 +0.5% −1.6% … +4.7% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

❌ 8 value(s) exceed their committed budget. Reduce the growth, or raise the budget in a separate pull request that names the bytes and the reason (CONTRIBUTING.md, "Size budgets").

target metric head budget over
octane-tsrx chat_fw_gzip 36,798 36,791 +7
octane-tsrx chat_js_gzip 39,515 39,506 +9
octane-tsrx bindings_fw_raw 341,198 339,858 +1,340
octane-tsrx bindings_fw_gzip 109,340 109,005 +335
octane-tsrx bindings_fw_brotli 94,937 94,788 +149
octane-tsrx bindings_js_raw 688,836 687,496 +1,340
octane-tsrx bindings_js_gzip 225,006 224,671 +335
octane-tsrx bindings_js_brotli 191,184 191,021 +163
target metric base head Δ
octane-tsrx js_raw 113,131 113,135 +4 (+0.0%) 🔴
octane-tsrx js_gzip 36,879 36,878 −1 (−0.0%) 🟢
octane-tsrx js_brotli 32,590 32,667 +77 (+0.2%) 🔴
octane-tsrx app_gzip 2,335 2,334 −1 (−0.0%) 🟢
octane-tsrx app_brotli 2,032 2,038 +6 (+0.3%) 🔴
octane-tsrx fw_raw 106,185 106,189 +4 (+0.0%) 🔴
octane-tsrx fw_brotli 30,558 30,629 +71 (+0.2%) 🔴
octane-tsrx todo_js_raw 121,538 121,542 +4 (+0.0%) 🔴
octane-tsrx todo_js_gzip 39,674 39,672 −2 (−0.0%) 🟢
octane-tsrx todo_js_brotli 34,923 34,897 −26 (−0.1%) 🟢
octane-tsrx todo_app_gzip 2,221 2,220 −1 (−0.0%) 🟢
octane-tsrx todo_app_brotli 1,886 1,889 +3 (+0.2%) 🔴
octane-tsrx todo_fw_raw 116,920 116,924 +4 (+0.0%) 🔴
octane-tsrx todo_fw_gzip 37,453 37,452 −1 (−0.0%) 🟢
octane-tsrx todo_fw_brotli 33,037 33,008 −29 (−0.1%) 🟢
octane-tsrx chat_js_raw 120,287 120,291 +4 (+0.0%) 🔴
octane-tsrx chat_js_gzip 39,512 39,515 +3 (+0.0%) 🔴
octane-tsrx chat_js_brotli 34,858 34,856 −2 (−0.0%) 🟢
octane-tsrx chat_app_gzip 2,714 2,717 +3 (+0.1%) 🔴
octane-tsrx chat_app_brotli 2,386 2,390 +4 (+0.2%) 🔴
octane-tsrx chat_fw_raw 114,522 114,526 +4 (+0.0%) 🔴
octane-tsrx chat_fw_brotli 32,472 32,466 −6 (−0.0%) 🟢
octane-tsrx weather_js_raw 156,842 156,846 +4 (+0.0%) 🔴
octane-tsrx weather_js_gzip 50,886 50,885 −1 (−0.0%) 🟢
octane-tsrx weather_js_brotli 44,407 44,376 −31 (−0.1%) 🟢
octane-tsrx weather_app_brotli 6,663 6,654 −9 (−0.1%) 🟢
octane-tsrx weather_fw_raw 134,688 134,692 +4 (+0.0%) 🔴
octane-tsrx weather_fw_gzip 43,015 43,014 −1 (−0.0%) 🟢
octane-tsrx weather_fw_brotli 37,744 37,722 −22 (−0.1%) 🟢
octane-tsrx bindings_js_raw 688,832 688,836 +4 (+0.0%) 🔴
octane-tsrx bindings_js_brotli 191,175 191,184 +9 (+0.0%) 🔴
octane-tsrx bindings_app_gzip 115,667 115,666 −1 (−0.0%) 🟢
octane-tsrx bindings_app_brotli 96,214 96,247 +33 (+0.0%) 🔴
octane-tsrx bindings_fw_raw 341,194 341,198 +4 (+0.0%) 🔴
octane-tsrx bindings_fw_gzip 109,339 109,340 +1 (+0.0%) 🔴
octane-tsrx bindings_fw_brotli 94,961 94,937 −24 (−0.0%) 🟢
octane-tsrx full_fw_raw 568,620 568,624 +4 (+0.0%) 🔴
octane-tsrx full_fw_gzip 171,214 171,215 +1 (+0.0%) 🔴
octane-tsrx full_fw_brotli 143,953 144,050 +97 (+0.1%) 🔴
octane-jsx js_raw 187,729 187,733 +4 (+0.0%) 🔴
octane-jsx js_gzip 59,529 59,532 +3 (+0.0%) 🔴
octane-jsx js_brotli 51,727 51,773 +46 (+0.1%) 🔴
octane-jsx app_gzip 2,856 2,857 +1 (+0.0%) 🔴
octane-jsx fw_raw 179,464 179,468 +4 (+0.0%) 🔴
octane-jsx fw_gzip 56,673 56,675 +2 (+0.0%) 🔴
octane-jsx fw_brotli 49,319 49,365 +46 (+0.1%) 🔴
octane-jsx full_fw_raw 568,620 568,624 +4 (+0.0%) 🔴
octane-jsx full_fw_gzip 171,214 171,215 +1 (+0.0%) 🔴
octane-jsx full_fw_brotli 143,953 144,050 +97 (+0.1%) 🔴

11 other measured values are unchanged.

Public-import reachability bundles (bytes)

❌ 21 value(s) exceed their committed budget. Reduce the growth, or raise the budget in a separate pull request that names the bytes and the reason (CONTRIBUTING.md, "Size budgets").

target metric head budget over
suspense-transition raw 141,554 140,222 +1,332
suspense-transition gzip 44,728 44,401 +327
suspense-transition brotli 39,230 39,132 +98
binding-base-ui-vite raw 199,752 198,414 +1,338
binding-base-ui-vite gzip 62,771 62,427 +344
binding-base-ui-vite brotli 54,544 54,383 +161
binding-base-ui-esbuild raw 215,272 213,931 +1,341
binding-base-ui-esbuild gzip 69,720 69,370 +350
binding-base-ui-esbuild brotli 60,428 60,247 +181
binding-radix-vite raw 201,783 200,448 +1,335
binding-radix-vite gzip 63,341 63,043 +298
binding-radix-vite brotli 55,066 54,923 +143
binding-radix-esbuild raw 217,096 215,755 +1,341
binding-radix-esbuild gzip 70,263 69,893 +370
binding-radix-esbuild brotli 60,921 60,813 +108
binding-apollo-client-vite raw 342,203 340,859 +1,344
binding-apollo-client-vite gzip 105,054 104,741 +313
binding-apollo-client-vite brotli 91,630 91,544 +86
binding-apollo-client-esbuild raw 365,415 364,070 +1,345
binding-apollo-client-esbuild gzip 116,033 115,728 +305
binding-apollo-client-esbuild brotli 101,272 101,084 +188
target metric base head Δ
cli-spa-starter raw 102,179 102,183 +4 (+0.0%) 🔴
cli-spa-starter brotli 29,877 29,879 +2 (+0.0%) 🔴
root-static-specialized raw 72,693 72,697 +4 (+0.0%) 🔴
root-static-specialized brotli 21,295 21,300 +5 (+0.0%) 🔴
root-chained-jsx raw 74,571 74,575 +4 (+0.0%) 🔴
root-chained-jsx gzip 24,322 24,320 −2 (−0.0%) 🟢
root-static raw 165,545 165,549 +4 (+0.0%) 🔴
root-static brotli 45,520 45,469 −51 (−0.1%) 🟢
root-static-local raw 72,692 72,696 +4 (+0.0%) 🔴
root-static-local brotli 21,290 21,293 +3 (+0.0%) 🔴
root-callback-local raw 74,597 74,601 +4 (+0.0%) 🔴
root-callback-local gzip 24,332 24,331 −1 (−0.0%) 🟢
root-callback-local brotli 21,750 21,744 −6 (−0.0%) 🟢
hooks-state raw 84,414 84,418 +4 (+0.0%) 🔴
hooks-state gzip 27,786 27,784 −2 (−0.0%) 🟢
hooks-state brotli 24,725 24,769 +44 (+0.2%) 🔴
prop-attributes raw 84,668 84,672 +4 (+0.0%) 🔴
prop-attributes gzip 27,782 27,781 −1 (−0.0%) 🟢
prop-attributes brotli 24,753 24,752 −1 (−0.0%) 🟢
context raw 96,621 96,625 +4 (+0.0%) 🔴
context gzip 31,475 31,474 −1 (−0.0%) 🟢
context brotli 27,955 27,927 −28 (−0.1%) 🟢
hydrate-root raw 138,068 138,072 +4 (+0.0%) 🔴
hydrate-root brotli 38,631 38,629 −2 (−0.0%) 🟢
deferred-hydration raw 221,365 221,369 +4 (+0.0%) 🔴
deferred-hydration gzip 69,679 69,676 −3 (−0.0%) 🟢
deferred-hydration brotli 60,118 60,089 −29 (−0.0%) 🟢
hydrate-frame raw 364,520 364,524 +4 (+0.0%) 🔴
hydrate-frame gzip 112,947 112,948 +1 (+0.0%) 🔴
hydrate-frame brotli 96,534 96,667 +133 (+0.1%) 🔴
suspense-transition raw 141,550 141,554 +4 (+0.0%) 🔴
suspense-transition gzip 44,727 44,728 +1 (+0.0%) 🔴
suspense-transition brotli 39,252 39,230 −22 (−0.1%) 🟢
component-owned-effects raw 77,056 77,060 +4 (+0.0%) 🔴
component-owned-effects gzip 25,152 25,153 +1 (+0.0%) 🔴
component-owned-effects brotli 22,483 22,510 +27 (+0.1%) 🔴
binding-hooks raw 82,913 82,917 +4 (+0.0%) 🔴
binding-hooks gzip 27,417 27,416 −1 (−0.0%) 🟢
binding-hooks brotli 24,498 24,476 −22 (−0.1%) 🟢
binding-base-ui-vite raw 199,748 199,752 +4 (+0.0%) 🔴
binding-base-ui-vite gzip 62,770 62,771 +1 (+0.0%) 🔴
binding-base-ui-vite brotli 54,452 54,544 +92 (+0.2%) 🔴
binding-base-ui-esbuild raw 215,268 215,272 +4 (+0.0%) 🔴
binding-base-ui-esbuild brotli 60,516 60,428 −88 (−0.1%) 🟢
binding-radix-vite raw 201,779 201,783 +4 (+0.0%) 🔴
binding-radix-vite brotli 54,997 55,066 +69 (+0.1%) 🔴
binding-radix-esbuild raw 217,092 217,096 +4 (+0.0%) 🔴
binding-radix-esbuild gzip 70,262 70,263 +1 (+0.0%) 🔴
binding-radix-esbuild brotli 61,037 60,921 −116 (−0.2%) 🟢
binding-mantine-hooks-vite raw 86,471 86,475 +4 (+0.0%) 🔴
binding-mantine-hooks-vite gzip 28,391 28,390 −1 (−0.0%) 🟢
binding-mantine-hooks-vite brotli 25,276 25,274 −2 (−0.0%) 🟢
binding-mantine-hooks-esbuild raw 90,840 90,844 +4 (+0.0%) 🔴
binding-mantine-hooks-esbuild gzip 30,510 30,508 −2 (−0.0%) 🟢
binding-mantine-hooks-esbuild brotli 27,152 27,199 +47 (+0.2%) 🔴
binding-apollo-client-vite raw 342,199 342,203 +4 (+0.0%) 🔴
binding-apollo-client-vite brotli 91,622 91,630 +8 (+0.0%) 🔴
binding-apollo-client-esbuild raw 365,411 365,415 +4 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 101,228 101,272 +44 (+0.0%) 🔴
binding-usehooks-ts-vite raw 87,237 87,241 +4 (+0.0%) 🔴
binding-usehooks-ts-vite gzip 28,594 28,592 −2 (−0.0%) 🟢
binding-usehooks-ts-vite brotli 25,536 25,537 +1 (+0.0%) 🔴
binding-usehooks-ts-esbuild raw 91,608 91,612 +4 (+0.0%) 🔴
binding-usehooks-ts-esbuild gzip 30,750 30,748 −2 (−0.0%) 🟢
binding-usehooks-ts-esbuild brotli 27,384 27,412 +28 (+0.1%) 🔴

46 other measured values are unchanged.

@trueadm
trueadm merged commit 6df3359 into main Oct 8, 2026
51 of 54 checks passed
@trueadm
trueadm deleted the fix/portal-marker-expando-clear branch October 8, 2026 14:38
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