Skip to content

perf(compiler): admit destructured props params into autoMemo regions - #640

Merged
trueadm merged 2 commits into
mainfrom
perf/automemo-destructured-props
Aug 8, 2026
Merged

trueadm merged 2 commits into
mainfrom
perf/automemo-destructured-props

Conversation

@trueadm

@trueadm trueadm commented Aug 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Compiler-inferred component memoization (autoMemo, prod-only) now admits components with destructured props parameters: function Child({ rows }) is treated as the same one-props snapshot as function Child(props), so stable-prop call sites of such components gain the whole-region dependency cache and skip the child's update entirely.
  • Call-site eligibility is no longer vetoed body-wide by an unrelated ref.current/computed-member/live-import member read elsewhere in the parent. Those reads now gate per call site and per tainted local, so e.g. a portal-mount overlayRef.current guard no longer disables memoization for every sibling subtree.

Why

  • The svg-dashboard suite (feat(benchmarks): add the svg-dashboard SVG stress suite #635) exposed that with app state colocated idiomatically (all domains in App, children like <Topology topo={topo} /> behind reference-stable snapshots), a ui-only commit re-entered every subtree: ~148µs per commit vs ~1.7µs for the fixture's Viewport-leaf workaround, 960-commit pan_zoom samples at 142ms vs 1.6ms.
  • Root cause was not the region machinery — it never engaged: every child used destructured props (ordinaryPropsParam failed), and the one overlayRef.current read in the body killed autoMemoCallsitesSafe for all call sites.
  • With this change (measured on top of feat(benchmarks): add the svg-dashboard SVG stress suite #635's suite locally), Defs/ChartsStrip/Sparks/IconLayer — including the 150-icon createElement de-opt layer — bail completely on ui commits; the dashboard minus the spread-bearing Topology drops from ~228µs to ~3–6µs per commit in-page, with the harness's Node-replay byte-parity gates green throughout. Topology itself still cannot bail because its rows carry {...e.attrs} spread bags, which the callee proof correctly fails closed on today (follow-up below).

Changes

  • isAutoMemoPropsParam (compile.js): admits an ObjectPattern props param when destructuring it evaluates nothing — fails closed on defaults, computed keys, any current binding at any depth, array patterns (iterator protocol), and non-identifier rest targets.
  • collectAutoMemoLocalHazards (compile.js): per-local taint replacing the body-wide readsDeferredRef/readsImportedMember veto. A local is tainted when a ref or live-import member read reaches its value through its initializer, any assignment right-hand side, or a for-of/for-in feed — transitively, including inside nested function values (useCallback(() => ref.current, [])). A site whose free identifiers touch a tainted local falls back; plain-value conditionally-reassigned locals stay clean witnesses (locals re-initialize every body entry).
  • scanComponentBody no longer collects the now-unused body-wide ref flag.
  • Tests: two compile-shape tests (admission matrix incl. every fail-closed pattern; hazard scoping incl. laundered/transitive/assignment-fed rejection and the clean-reassignment non-regression) — both verified failing against the pre-change compiler. The AutoMemoApp runtime fixture gains a destructured-param twin of Rows with mirrored context-refresh/child-state assertions, running in both the dev and octane-prod vitest projects.
  • Changeset (octane, patch).

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test — 1567 files / 16,965 tests green on this branch (also green twice on the feat(benchmarks): add the svg-dashboard SVG stress suite #635 base during development)
  • targeted tests: auto-memo.test.ts, auto-memo-value-position.test.ts, automemo-render-calls.test.ts (184 tests), packages/octane/tests/compiler (1,061 tests); new tests confirmed red pre-fix
  • pnpm sync — no generated changes
  • svg-dashboard bench (run locally on top of feat(benchmarks): add the svg-dashboard SVG stress suite #635, 4 flavors, exit 0): all ratio guards pass — pan_zoom 0.31x react (guard ≤0.7), drag_nodes 1.64x (≤2.2), style_spread_pulse 1.99x (≤3.0); octane absolute numbers flat-to-better vs same-server pre-change baseline; full harness correctness gates (byte-parity Node replay, namespace census, survivor identity) green with regions skipping.

Risk / follow-ups

  • The pinned conservative semantics are preserved: ref-derived or live-import-derived locals still reject their sites (existing nonlocalCallSites expectations unchanged), and dev/HMR/profile/server builds still emit no regions.
  • Callee bodies containing host-element spread bags ({...attrs}) remain excluded from the proof — the measured remaining gap (svg-dashboard Topology, ~130µs of the inline-shape commit); tracked as a separate investigation.
  • For-list caches (listSafe/itemMemo) accept locals as deps without hazard filtering — pre-existing behavior, deliberately not tightened here.
  • Hydration coverage for destructured-callee regions rides the existing props-form hydration tests; the emitted call-site shape is identical, only eligibility widened.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


Note

Medium Risk
Changes compiler correctness boundaries for production-only memo regions (refs, live imports, destructuring); semantics are fail-closed and heavily tested, but misclassification could skip caches or risk stale subtrees.

Overview
Widens autoMemo (prod compiler-inferred region caching) so more real-world components and call sites actually get memo regions instead of falling back globally.

Destructured props callees: isAutoMemoPropsParam treats a plain ObjectPattern param like (props) — one snapshot at entry — while still rejecting defaults, computed keys, any current binding, array patterns, and multi-param signatures. Callees such as function Child({ rows }) can now be autoMemoSafe and emit dependency-cached regions when props stay reference-stable.

Per-site hazard gating: The body-wide readsDeferredRef veto is removed from scanComponentBody / autoMemoCallsitesSafe. collectAutoMemoLocalHazards taints component locals that absorb ref reads or live-import member reads (including via destructuring defaults, loops, assignments, and transitive locals). Only call sites whose free identifiers touch a tainted local lose the region; unrelated siblings (e.g. a portal ref.current guard elsewhere) keep caching. Plain locals with only value reassignment stay eligible.

Component analysis stores autoMemoLocalHazards on componentInfo and threads it through compile via ctx.currentAutoMemoLocalHazards for per-site depsSafe checks. Tests cover admission/rejection matrices, hazard scoping, and a runtime twin for destructured Rows.

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

Component call sites now receive the compiler-owned whole-region cache when
the callee destructures its props param, and call-site eligibility is no
longer vetoed body-wide by an unrelated ref or live-import member read —
those reads gate per site and per tainted local instead.

Co-Authored-By: Claude Fable 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 Aug 8, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Hazard feeds miss pattern sources
    • Extended feed to check the binding pattern (not just the RHS) for deferred-ref reads and imported-member reads, collect free identifiers from pattern expression positions (defaults, computed keys), handle for-of/in with VariableDeclaration left-hand sides, and process nested VariableDeclaration nodes in the walk — all 138 auto-memo and 1068 compiler tests pass.

Create PR

Or push these changes by commenting:

@cursor push cb504a0523
Preview (cb504a0523)
diff --git a/packages/octane/src/compiler/compile.js b/packages/octane/src/compiler/compile.js
--- a/packages/octane/src/compiler/compile.js
+++ b/packages/octane/src/compiler/compile.js
@@ -4243,17 +4243,26 @@ function collectAutoMemoLocalHazards(stmts, importedNames) {
 		const names = new Set();
 		collectBindings(pattern, names);
 		if (names.size === 0) return;
-		const direct = containsDeferredRefRead(rhs) || containsImportedMemberRead(rhs, importedNames);
-		for (const name of names) {
-			if (hazards.has(name)) continue;
-			if (direct) {
+		const direct =
+			containsDeferredRefRead(rhs) ||
+			containsImportedMemberRead(rhs, importedNames) ||
+			containsDeferredRefRead(pattern) ||
+			containsImportedMemberRead(pattern, importedNames);
+		if (direct) {
+			for (const name of names) {
 				hazards.add(name);
 				sourceFrees.delete(name);
-				continue;
 			}
+			return;
+		}
+		const rhsFree = collectFreeIdentifiers(rhs, []);
+		const patFree = collectFreeIdentifiers(pattern, names);
+		for (const name of names) {
+			if (hazards.has(name)) continue;
 			let set = sourceFrees.get(name);
 			if (set === undefined) sourceFrees.set(name, (set = new Set()));
-			for (const id of collectFreeIdentifiers(rhs, [])) set.add(id);
+			for (const id of rhsFree) set.add(id);
+			for (const id of patFree) set.add(id);
 		}
 	};
 	for (const stmt of stmts) {

@@ -4243,17 +4243,26 @@ function collectAutoMemoLocalHazards(stmts, importedNames) {
 		const names = new Set();
 		collectBindings(pattern, names);
 		if (names.size === 0) return;
-		const direct = containsDeferredRefRead(rhs) || containsImportedMemberRead(rhs, importedNames);
-		for (const name of names) {
-			if (hazards.has(name)) continue;
-			if (direct) {
+		const direct =
+			containsDeferredRefRead(rhs) ||
+			containsImportedMemberRead(rhs, importedNames) ||
+			containsDeferredRefRead(pattern) ||
+			containsImportedMemberRead(pattern, importedNames);
+		if (direct) {
+			for (const name of names) {
 				hazards.add(name);
 				sourceFrees.delete(name);
-				continue;
 			}
+			return;
+		}
+		const rhsFree = collectFreeIdentifiers(rhs, []);
+		const patFree = collectFreeIdentifiers(pattern, names);
+		for (const name of names) {
+			if (hazards.has(name)) continue;
 			let set = sourceFrees.get(name);
 			if (set === undefined) sourceFrees.set(name, (set = new Set()));
-			for (const id of collectFreeIdentifiers(rhs, [])) set.add(id);
+			for (const id of rhsFree) set.add(id);
+			for (const id of patFree) set.add(id);
 		}
 	};
 	for (const stmt of stmts) {
@@ -4276,12 +4285,14 @@ function collectAutoMemoLocalHazards(stmts, importedNames) {
 		seen.add(n);
 		if (n.type === 'AssignmentExpression' && n.left?.type !== 'MemberExpression') {
 			feed(n.left, n.right);
-		} else if (
-			(n.type === 'ForOfStatement' || n.type === 'ForInStatement') &&
-			n.left &&
-			n.left.type !== 'VariableDeclaration'
-		) {
-			feed(n.left, n.right);
+		} else if ((n.type === 'ForOfStatement' || n.type === 'ForInStatement') && n.left) {
+			if (n.left.type === 'VariableDeclaration') {
+				for (const d of n.left.declarations || []) feed(d.id, n.right);
+			} else {
+				feed(n.left, n.right);
+			}
+		} else if (n.type === 'VariableDeclaration') {
+			for (const d of n.declarations || []) feed(d.id, d.init);
 		}
 		for (const key in n) {
 			if (AST_WALK_SKIP_KEYS.has(key)) continue;

@@ -4276,12 +4285,14 @@ function collectAutoMemoLocalHazards(stmts, importedNames) {
 		seen.add(n);
 		if (n.type === 'AssignmentExpression' && n.left?.type !== 'MemberExpression') {
 			feed(n.left, n.right);
-		} else if (
-			(n.type === 'ForOfStatement' || n.type === 'ForInStatement') &&
-			n.left &&
-			n.left.type !== 'VariableDeclaration'
-		) {
-			feed(n.left, n.right);
+		} else if ((n.type === 'ForOfStatement' || n.type === 'ForInStatement') && n.left) {
+			if (n.left.type === 'VariableDeclaration') {
+				for (const d of n.left.declarations || []) feed(d.id, n.right);
+			} else {
+				feed(n.left, n.right);
+			}
+		} else if (n.type === 'VariableDeclaration') {
+			for (const d of n.declarations || []) feed(d.id, d.init);
 		}
 		for (const key in n) {
 			if (AST_WALK_SKIP_KEYS.has(key)) continue;

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 16cfb6c. Configure here.

Comment thread packages/octane/src/compiler/compile.js Resolved
@domgan-oai
domgan-oai marked this pull request as ready for review August 8, 2026 01:45
The per-local hazard classification only inspected right-hand sides of
top-level declarators and bare loop lefts, so a hazard riding the pattern
itself (a `current` or computed binding key, a default reading a ref or live
import) or routed through a nested-block or for-of declaration escaped the
taint and could witness a region dep. Feed every declaration at every depth,
classify the pattern subtree alongside the source, and feed for-of/for-in
bound names from the loop source, restoring the old body-wide veto's full
coverage per local.

Co-Authored-By: Claude Fable 5 <[email protected]>
@trueadm
trueadm merged commit 80a9c7e into main Aug 8, 2026
34 checks passed
@trueadm
trueadm deleted the perf/automemo-destructured-props branch August 8, 2026 09:12
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