Skip to content

perf(compiler): admit host-element spread bags into autoMemo regions - #642

Merged
trueadm merged 1 commit into
mainfrom
perf/automemo-host-spread-bags
Aug 8, 2026
Merged

trueadm merged 1 commit into
mainfrom
perf/automemo-host-spread-bags

Conversation

@trueadm

@trueadm trueadm commented Aug 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • The autoMemo callee proof (containsAutoMemoUnsafeStructure) now admits spread bags on host elements, so bodies like svg-dashboard Topology's <path d={e.d} {...e.attrs}> no longer disqualify the component's call-site region cache or its keyed-list caches. Component-tag spreads keep failing closed, and the spread argument still walks under every other rule (accessors, computed members, ref reads).

Why

  • A host spread compiles to one setSpread binding whose bag is reachable only from dependencies the region guard already witnesses (every free identifier of a region is classified by the existing proofs; unwitnessed names fail closed independently of spreads). A skip on unchanged deps is therefore the no-op a re-entry would have been — the same pure-render immutable-snapshot argument that already admits member-read bindings like class={e.cls}. The statically invisible bag keys hold up case by case:
    • ref in a bag: setSpread diffs refs by identity (unchanged value is a no-op even on re-entry), queues detach on removal/swap with commit ordering, and compiled teardown detaches spread-supplied refs on unmount. A skipped region keeps the same ref attached to the same element — identical to a React.memo bail. (The explicit ref={…} attribute rejection stays as the pinned static-escape philosophy.)
    • Handlers in a bag: delegated dispatch reads the current event slot at event time; there is no per-render re-subscription a skip could miss.
    • Getter-bearing bags: the same contract exposure as already-admitted member reads; getters the walk can see (inline accessor objects) still reject via the existing accessor rule.
  • Component-tag spreads stay rejected because they build the child's props snapshot — callSiteOk excludes them, and isAutoMemoPropsParam's rest admission documents that a getter-bearing props object must never reach a cached region.
  • Measured motivation: with all three svg-dashboard state domains colocated in App (the idiomatic shape), Topology's non-bail accounted for ~130µs of every ui-only commit after perf(compiler): admit destructured props params into autoMemo regions #640.

Changes

  • packages/octane/src/compiler/compile.js: the walk marks spread attributes of !isComponentTag elements into a per-invocation WeakSet during the element's visit (parents walk before their attributes), and the blanket SpreadAttribute/JSXSpreadAttribute rejection exempts marked nodes. hostMountSafe stays spread-free through isHostMountSafeTree's own attribute check; dev/server output is unchanged since every consumer gates on ctx.autoMemo.
  • packages/octane/tests/auto-memo.test.ts: compiler admissions (props-object callee, destructured @for Topology shape, tsx map-lowered rows) and pinned rejections (component-tag spread in a body and at a call site, inline getter bag, computed-member argument, explicit ref beside a bag); plus a runtime cached-region scenario — bag applies on mount, a skip keeps the bag's ref and handler live (and pins mutation-invisibility under unchanged deps), re-entry applies changed keys, clears vanished keys, and swaps the bag ref old → null → new, unmount detaches.
  • benchmarks/baselines/ratios.json: svg-dashboard guard notes updated with the 2026-08-08 observations (no ceiling changes).
  • Changeset (octane, patch).

Validation

  • pnpm format:check (scoped format:files:check over the changed files)
  • pnpm typecheck
  • pnpm test — full suite green on this branch's base
  • targeted tests: packages/octane/tests/auto-memo.test.ts (142 tests, octane + octane-prod projects); spread/ref/keyed neighbor suites. Red-checked both directions: without the widening the new tests fail (no region); with a deliberately neutered dep comparison the DOM re-diff assertions catch the wrong skip.
  • Benchmarks (Playwright/Chromium, 20 iters): inline-App probe (ui state colocated in App) pan_zoom 134.0 ms → 0.6 ms, tooltip_swarm 51.5 → 0.8 ms; committed-shape A/B isolating this change: charts_tick 4.2 → 2.9 ms, tick_sparse 3.7 → 0.8 ms, series_toggle 3.0 → 2.1 ms, always-miss ops flat within noise (drag_nodes +~0.1 ms, ~1.4%). Full 4-flavor bench.mjs svg-dashboard --ratios run passes all guards (re-run on this rebased base with the React-Compiler react column).

Risk / follow-ups

  • No dedicated hydration test for a spread bag inside a cached region: the first render is always a cache miss so the adoption path is unchanged, and the prod-hydrate suites pass, but the specific combination is untested.
  • The svg-dashboard octane fixture keeps its Viewport descriptor-prop shape; flipping it to the idiomatic colocated shape is now viable (the pan_zoom guard notes record the probe numbers) but is a fixture/fairness decision for the bench suite, not this change.
  • Contract note: an in-place bag mutation under unchanged region deps is invisible while the region skips — the same trade every autoMemo tier (and React.memo) already makes; the runtime test pins it deliberately.

Provenance

  • An agent produced this diff (agent-authored)

Note

Medium Risk
Widens compiler memoization eligibility; correctness hinges on region guards and spread re-diff parity, though the new compiler and runtime tests target the risky cases.

Overview
autoMemo no longer treats {...bag} on host elements as an automatic disqualifier. Patterns like <path d={e.d} {...e.attrs}> can keep region and keyed-list caches; component-tag spreads still fail closed, and unsafe spread arguments (getters, computed keys, refs) still do.

In containsAutoMemoUnsafeStructure, host-element visits register their spread attributes in a per-walk WeakSet before children are walked; only spreads not in that set trigger the old reject path.

New auto-memo tests cover admitted shapes (including Topology-style @for rows and TSX map), pinned rejections, and a runtime scenario where an unchanged-deps skip preserves DOM/refs/handlers while re-entry updates and clears spread keys. Benchmark ratio notes for svg-dashboard are refreshed; ceilings are unchanged.

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

A spread bag on a HOST element is one setSpread binding whose bag is
reachable only from deps the region guard already witnesses, so skipping on
unchanged dependencies is the no-op a re-entry would have been. Callee
bodies like svg-dashboard Topology's {...e.attrs} rows now earn the
call-site region and keyed-list caches; component-tag spreads keep failing
closed, and the spread argument still walks under every other rule.

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
@trueadm
trueadm marked this pull request as ready for review August 8, 2026 11:16
@trueadm
trueadm merged commit 62d7f13 into main Aug 8, 2026
53 checks passed
@trueadm
trueadm deleted the perf/automemo-host-spread-bags branch August 8, 2026 11:53
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