Skip to content

perf: recover transition hook hot-path cost from the audit fixes - #949

Merged
trueadm merged 1 commit into
mainfrom
perf/transition-hook-hot-paths
Sep 3, 2026
Merged

trueadm merged 1 commit into
mainfrom
perf/transition-hook-hot-paths

Conversation

@trueadm

@trueadm trueadm commented Sep 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Add benchmarks/transition-hooks, a deterministic Node-only suite for the transition and state-hook hot paths that fix: address React behavioral audit defects #943 touched: useTransition start → pending render → settle cycles with a replacement value and a functional updater, a suspended hold + release, an urgent functional dispatch and its same-value bailout, a delegated click, and an urgent update beside a queued transition. Each scenario checks its render sequence and DOM, and the observed bundle counts runtime function/array/object/constructor creation events against a work-model target; 35 ratio guards enforce them.
  • Remove the per-transition Set allocations fix: address React behavioral audit defects #943 added for hook pending tracking. A batch stores its first starting hook in a field (a second distinct hook allocates a small list) and each hook counts its pending batches, with a per-batch flag so a re-held batch is counted once. Every fixed behavior is unchanged: nested starts from another hook share the pending window, a hook starting nested transitions inside its own Action is counted once, and a finished batch that a later round re-holds becomes pending again.
  • Give the shared hook-memo creation observer an opt-in extended mode (object literals and non-Array new expressions); hook-memo itself keeps its default counts.
  • Record the attribution, the counts, the timing caveats, and the manual-provider adapter finding in docs/divergence-audit-2026-09-03.md.

Why

The audit recorded a 2.8 → 3.6 µs (+29%) hook-transition cycle from an uncommitted fixture and about 5.7–9.5 ns per manual-provider call. There was no committed benchmark and no guard. This suite makes the transition-cycle cost a deterministic count with semantic controls, so the regression it protects against is visible in CI's ratio run and cannot be argued away as timing noise.

Attribution on 33720d8ef (pre-#943) vs 44d50dbc0 (main) with the new suite: the only per-cycle allocation the audit added on a non-suspending cycle was the Set allocated for actionBatch.hooks (+1 constructor per transition, plus one Set per hook); CPU profiles of 300k cycles put the rest of the delta in the new hook bookkeeping (runTransition/finishTransitionHookBatch Set traffic), the render-time queue replay in useState, and the setter's held-update lookups. The replay, the staged update record, the retained flush list, and the lookups are the corrected semantics and stay; the audit paragraph documents them as such.

Measurements

Same command, same machine, same installed dependencies (node benchmarks/transition-hooks/run.mjs, Node 24.18.0). Runtime creation events per 64 cycles; every other counter and every render count is identical between main and this branch:

Scenario Counter 33720d8ef main (44d50dbc0) this branch
cycle constructors 192 256 192
updater constructors 192 256 192
held constructors 896 1,025 961
held arrays / objects 4,544 / 1,472 4,480 / 1,344 4,480 / 1,344
urgent constructors n/a 448 384

node benchmarks/bench.mjs --ratios transition-hooks: this branch passes all 35 guards; the main runtime (OCTANE_TRANSITION_ROOT pointed at a pristine 44d50dbc0 checkout) breaches cycle_runtime_constructors (1.33x), updater_runtime_constructors (1.33x), held_runtime_constructors (1.07x), and urgent_runtime_constructors (1.17x). The remaining held allocation over the baseline is the hook-holder registration a suspended transition creates when it holds; it stays on that cold path.

Timing (secondary, no guard). Three alternating rounds of run.mjs per runtime at a steady load average near 5.5 (another session's test jobs were running throughout), median of per-round medians in µs per cycle, baseline / main / this branch:

Scenario 33720d8ef main this branch
cycle 1.55 1.83 1.52
updater 1.31 1.44 1.36
held 6.07 6.66 6.25
dispatch 0.75 0.75 0.71
bail 0.24 0.26 0.24
click 4.81 5.06 5.08
urgent n/a 2.47 2.38

The same rounds taken while the machine's load average was above 20 doubled every figure, so the audit's absolute 2.8 → 3.6 µs is not comparable with this fixture and the PR claims only what the count guards see; the direction of the timing deltas matches the constructor counts.

Manual-provider adapter (isolated, fifteen alternating million-call samples after six warmups): prior module-initialization closure provider 6.85 ns per bound call, current hoisted declaration adapter 9.93 ns, two call-site-specialized variants that pass arguments.length plus named parameters instead of arguments 9.75–9.93 ns, direct call 6.01 ns. The remaining cost is the second call frame a hoisted declaration needs to reach the shared driver, not arguments forwarding, so the adapter is unchanged and the audit keeps documenting that cost.

Changes

  • benchmarks/transition-hooks/{cases.tsrx,entry.mjs,run.mjs,README.md}: new suite; benchmarks/bench.mjs manifest entry; benchmarks/README.md table row; 35 guards in benchmarks/baselines/ratios.json.
  • benchmarks/hook-memo/instrument.mjs: opt-in objects counting (objectLiterals, constructors); README note.
  • packages/octane/src/runtime.ts: TransitionActionBatch.hook/hooks/hooksPending, TransitionHookSlot.pendingBatches, addTransitionHookToBatch, publishTransitionHookBatch; finishTransitionHookBatch/holdTransitionHookBatch use them; the per-hook Set and its unmount cleanup are gone (an in-flight batch releases its hook reference when the Action settles; publish already ignores a disposed block).
  • packages/octane/tests/transition-hook-pending.test.ts (+ fixture): pending ownership across overlapping and nested Actions and an unmounted starting hook. The nested-second-hook case fails when a newcomer to an already-counted batch is not counted; the others pin the contract.
  • packages/octane-mcp-server/src/index.js + README: the hand-maintained suite list gains transition-hooks (its manifest-sync test enforces this).
  • .changeset/quick-transition-hooks.md (octane patch); docs/divergence-audit-2026-09-03.md row and follow-up section.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test (2,599 files, 28,723 tests; the orchestrator needed NODE_OPTIONS=--max-old-space-size=8192 to survive the shared machine's memory pressure, after a default-heap run died in the runner process before reporting)
  • targeted tests: audit-hook-behavior, transitions, transition-held-audit, transition-held-urgent-resuspend, transition-urgent-equality, transition-nested-action, transition-action-order, transition-descendant-suspend, transition-timeout, transition-replacements, conformance/update-reconciliation, conformance/eager-bailout, scheduler-commit, external-store-behavior (236 tests), plus the new transition-hook-pending (4) — all pass on this branch.
  • node --test benchmarks/hook-memo/instrument.test.mjs; node benchmarks/bench.mjs --ratios transition-hooks (35/35 pass; main runtime breaches 4).
  • pnpm sync produced no generated changes.

Risk / follow-ups

  • Behavior is unchanged by design; the semantic controls in the suite and the 236 pinned transition tests are the evidence. A disposed hook now stays referenced by an in-flight batch until that Action settles (previously the unmount cleanup removed it); the window is bounded by the Action.
  • The held path still allocates one holder registration Set per suspended hold (cold path); readQueuedState stays O(1) on the common path and was not changed.
  • Timing guards were deliberately not added: the cheapest cycles are inside run-to-run noise on a shared machine.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


Note

Medium Risk
Changes core useTransition pending bookkeeping in runtime.ts with broad transition test coverage and new regression guards; behavior is intended to be parity-preserving but touches scheduler-adjacent hot paths.

Overview
Replaces per-transition Set allocations in useTransition pending tracking with a batch field for the first starting hook, an optional array only when a second distinct hook joins, and per-hook pendingBatches counts—restoring the pre-audit three-constructor budget per cycle while preserving audit semantics (nested hooks, single count for repeated starts in one Action, re-held batches go pending again).

Adds a transition-hooks Node benchmark with seven scenarios (cycle, functional updater, suspended hold/release, urgent dispatch, same-value bailout, delegated click, urgent beside queued transition), render/DOM controls, extended hook-memo allocation instrumentation, 35 CI ratio guards, and audit doc updates.

Adds transition-hook-pending tests for overlapping Actions, nested second-hook pending, and unmount-before-settle; removes per-hook Set unmount cleanup in favor of batch lifetime holding the hook reference.

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


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

Add the deterministic benchmarks/transition-hooks suite (render and creation-event
counts with semantic controls, 35 ratio guards) covering useTransition cycles,
functional updaters, a suspended hold + release, urgent dispatch and bailout,
delegated clicks, and an urgent update beside a queued transition.

Track useTransition pending ownership without a Set per transition: a batch keeps
its first starting hook in a field and each hook counts its pending batches, so a
cycle allocates the pre-audit three collections again while every corrected
behavior stays pinned. Extend the hook-memo observer with opt-in object and
constructor counting, and record the attribution, counts, timing caveats, and the
manual-provider adapter measurement in the audit document.

Co-Authored-By: Claude Fable 5.1 <[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 Sep 3, 2026
@trueadm
trueadm marked this pull request as ready for review September 3, 2026 21:17
@trueadm
trueadm merged commit eff0271 into main Sep 3, 2026
49 checks passed
@trueadm
trueadm deleted the perf/transition-hook-hot-paths branch September 3, 2026 21:28
@domgan-oai domgan-oai mentioned this pull request Sep 3, 2026
1 task done
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