Repository navigation
perf(octane): clear portal marker expandos instead of deleting them - #1900
Conversation
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]>
Benchmark report
Compares js-framework: 1k/10k row operations
All 20 timed operations (19 without a verdict)
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").
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").
46 other measured values are unchanged. |
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 withdelete 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 assignsundefinedinstead.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 usesin,hasOwnPropertyorObject.keys, and no other package reads them. Event routing is unchanged.Why
Portal teardown is the next notable cost on
benchmarks/portal-swarmafter the root-journal work in #1897. The sampling heap profiler put ~137 KB of each open+close cycle (600 portals) inunregisterPortalEventRange.Shape probe (
%HasFastPropertiesunder--allow-natives-syntax, all 1,200 portal markers in the page, 3 cycles):Changes
packages/octane/src/runtime.ts:unregisterPortalEventRangeclears 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.octanepatch.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:
unregisterPortalEventRangechildSlot(control)Without pinning,
childSlotappears 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:
open_allnever reaches unregister, so it stays flat as expected.--log-deoptover 40 cycles gives the same deopt set as main, plus one expected "dependent field type changed": the fields' tracked type generalizes once whenundefinedis first stored.Bytes. Raw grows 4 B wherever the runtime ships the portal code: the minifier wraps an assignment inside
&&in parentheses, whichdeletedidn'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 --budgetspasses every scenario, andrun.mjs --budgets octane-tsrx octane-jsxreports 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:checkpasses on the changed files.pnpm typecheck: not run repo-wide.pnpm typecheck:filespasses 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-prodprojects, on the rebased head:conformance/event-listener,portal-events,portal,portal-value,conformance/fragment-future-children(198 passed). Before the rebase this also includedaudit-events-portals,portal-into-deopt-hostandtransition-swap-portal(246 passed).The new test fails on broken variants of
unregisterPortalEventRange: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.mjsreports no candidates. The change removes adeletefrom 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
bundle size,benchmark reportand theCI provenancegate (which only checksBUNDLE_SIZE_RESULT) fail on main too. Main at 90a57d8 fails the same ratchet:cli-spa-starter: ratchet raw budget 102235 to 102211. That comes from ci: budget native producer pacing separately #1891's budget-first raise for fix: coalesce and pace ready signal producers #1875. This PR's run reads102215, which is the diff's +4 raw on top. The benchmark report's over-budget rows (bindings_fw_raw+1,340,chat_fw_gzip+7,suspense-transitionand thebinding-*reachability rows) are already over on the base.chat_fw_gzipreads 36,798 on both base and head. After merging main tip abe9b42 (fix: coalesce and pace ready signal producers #1875 + test: cover buffered backlog responsiveness in Chromium #1877), main's own runtime still exceedschat,bindingsandhydrate-rootbudgets locally (bindings/frameworkraw 341,240 > 339,858;hydrate-rootgzip 44,219 > 44,196). Those overages come from fix(octane): commit the pending cue apart from its component's transition (#1864) #1888, whose raise ci(bundle-size): budget combined transition cues and native producers #1887 is open and conflicting. This branch adds +4 raw and 0 to −2 gzip on those rows. Next step: once ci(bundle-size): budget combined transition cues and native producers #1887 lands, merge main and re-run CI. No budget edits belong in this PR. The js-frameworkselect_lots"slower" verdict (+0.4 µs on a 6 µs op, all 64 work counters unchanged) cannot come from this diff, because js-framework never opens a portal.undefineduntil they are collected. Code that enumerates a marker's keys would see them, and Octane has none.registerPortalEventRangeandunregisterPortalEventRange. That is the function context V8 allocates on entry, because theDEFERRED_LAYOUT_DRIVER?.stageAction(() => f(target, portal))closure captures the parameters. 13stageActionsites share this pattern, and moving the staging into helpers costs bytes, so it is left alone.unregisterPortalEventRange, not its body.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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
deleteon the two comment-marker expandos inunregisterPortalEventRange; it sets$$portalEventRangeand$$portalEventStarttoundefinedinstead. That avoids Chromium/V8 putting DOM wrappers into dictionary mode on every close (fewer allocations onportal-swarm; routing is unchanged becauseresolvePortalEventOwneralready 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.