Skip to content

perf(compiler): cache Strong inline render expressions like named consts - #1611

Merged
trueadm merged 2 commits into
mainfrom
perf/strong-inline-render-calculations
Oct 2, 2026
Merged

trueadm merged 2 commits into
mainfrom
perf/strong-inline-render-calculations

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Closes #1610.

A Strong module asserts that every render-time call is a pure projection of witnessed inputs, so these are the same program:

<output>{total.toFixed(2)}</output>

const formatted = total.toFixed(2);
<output>{formatted}</output>

Only the declaration reached rewriteAutoCalculation, so the inline form recomputed on every entered render while the extracted form was cached. React Compiler caches both.

In production client builds, an eligible expression the template evaluates on every render is now moved into a compiler-named const appended to setup. That covers host child holes, host attributes, and component props. The existing declaration path then admits or declines it under the same rule, which this change factors out as autoCalculationOf. For the issue's repro, the inline output now matches the extracted output's cache region. It also skips the extracted form's pending-output guard, because the moved calls were never setup calls.

What stays where it is written

Position Why
@if / @for / @switch / @try arms Conditional evaluation.
Component children The child decides whether and when they render.
Built-in boundaries and anything imported from octane They own when their props and children evaluate.
Expressions that contain JSX (.map(… => <li/>), cond && <X/>) They keep their list and descriptor lowering.
Host on* handlers, ref, key, children They keep their wiring.
A render tree that assigns, updates, or deletes anything It keeps its whole evaluation order.
Native-read (signals) modules, dev, HMR, profile, server strongMemo is off, or holes carry direct signal bindings.

React-style return components get the same calculations only for a top-level return <jsx> reached after an authored hook call. This is the existing render-scope proof the declaration path already uses there, so a hookless return component stays an ordinary deferred function.

Warm plans

A parallel use() warm plan is printed outside the body that declares a hoisted name. collectWarmChild therefore uses the authored prop expression for a hoisted prop. Without this, a param-only prop such as version={next(props.version)} turns into a component local. The child then drops out of the first-attempt warm walk, and its request waterfalls.

Validation

  • pnpm format:check (only pnpm format:files:check on the changed files, which passes)
  • pnpm typecheck (only scoped pnpm typecheck:files on the changed files; it reports nothing in the added lines, only existing fixture-import and implicit-any lines)
  • pnpm test (left to CI)
  • targeted tests:
    • auto-calculation.test.ts: 16 passed (octane and octane-prod)
    • parallel-use.test.ts: passed (octane and octane-prod)
    • for.test.ts under octane-prod: 113 passed. Its Strong fixtures now get hoisted props, including the server-render-then-hydrate cases.
    • compiler/strong-*.test.ts (8 files): 1,874 passed
    • Under octane-prod: event-bundle-captures, event-signal-owner, hydrate-compiler, hydration/range-boundary, hydration/direct-signal-bindings, all passed
    • compiler/for-item-purity, bundler-compiler, compiler-vite-options, native-reads: passed

New tests compile their Strong sources with production options, so both projects exercise the cache. I checked that each fails on a realistic broken version:

Test Breaks when
Inline hole and component-prop identity stays stable across unrelated renders and changes with inputs Hoisting is disabled (identity id1 becomes id3).
Expressions in an @if arm and in component children evaluate only when they render The pass hoists from arms or from component children (TypeError reading name of null).
A returned inline calculation stays stable after an authored hook The return-path hoist is disabled.
A hookless Strong return component reads nothing when called directly Hoisting ignores the render-scope proof.
Strong siblings with inline-calculation props start together in the first attempt The warm-plan fix is removed (second:1 waterfalls).

Compatibility-mode output for the issue's inline repro is byte-identical before and after.

Cost

I made no runtime speed claim and ran no benchmark locally. Each hoisted expression adds one memo cell and guard. That is about 45–50 gzip bytes after minification: the new five-calculation test module grows from 1,539 to 1,776 B gzip. The existing for-strong.tsrx fixture grows by 39 B gzip. for-strong.tsx and event-signal-owner-strong.tsrx are unchanged. This is the trade the declaration path already makes. template-call-memo should not move: its top-level calls use a global callee (String(tick)), which stays live, and its rows are @for arms.

Risk / follow-ups

  • In Strong, a moved expression now evaluates at the end of setup instead of during template evaluation. Under the Strong contract that is unobservable. A module that violates the contract, for example by reading a live store through a call, now sees the same staleness the extracted const already had.
  • @if, @for, and @switch heads are not hoisted yet. For example, @for (const r of rows.filter(…)) would gain a stable iterable. It needs its own check against keyed-list lowering.
  • Member-expression and dynamic component tags (<ui.Button>, <{Tag}>) are skipped conservatively.

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

Medium Risk
Changes Strong production client lowering and memo timing for inline expressions; incorrect hoisting could alter evaluation order or break warm-plan prefetching, though guards and new tests target those edges.

Overview
Strong production client builds now treat eligible inline template expressions (host text holes, attributes, and component props) like an authored const in setup: the compiler hoists them to compiler-named declarations and runs them through the same autoCalculationOf / rewriteAutoCalculation memo path, aligning with React Compiler for cases such as {total.toFixed(2)} and rows={project(rows)}.

Hoisting is skipped where evaluation must stay conditional or special-cased—directive arms, component children, built-in/octane boundaries, JSX-bearing expressions, handlers/refs/keys, render trees with side-effect writes, returned-output bodies, native-read modules, dev/server, and hookless return <jsx> components (caching only after an authored hook establishes render scope).

Parallel use() warm plans still evaluate hoisted prop expressions from the authored source via ctx.hoistedRenderCalculations, so sibling prefetches are not lost when props become setup locals.

Docs and a patch changeset describe the behavior; auto-calculation.test.ts and parallel-use.test.ts add production-compiled Strong coverage.

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

trueadm and others added 2 commits October 2, 2026 22:06
A Strong module asserts that render-time calls are pure projections of
witnessed inputs, so `<output>{total.toFixed(2)}</output>` and
`const formatted = total.toFixed(2); <output>{formatted}</output>` are the
same program. Only the declaration reached rewriteAutoCalculation, so the
inline form recomputed on every entered render while the extracted form
was cached (#1610).

In production client builds, an eligible expression that the template
evaluates on every render (a host child hole, a host attribute, or a
component prop) now moves to a compiler-named const appended to setup.
The existing declaration path then admits or declines it under the same
rule, which is factored out as autoCalculationOf. Directive arms,
component children, built-in boundaries, expressions holding JSX, event
handlers, refs, keys, and render trees that write keep their evaluation
where it is written. A return-JSX component gets the same calculations
before a top-level `return <jsx>` only after an authored hook, so a
hookless one stays an ordinary deferred function.

A parallel use() warm plan runs outside the body that declares the
hoisted name, so it now reads the authored prop expression. Otherwise a
param-only prop such as `version={next(props.version)}` would make the
child look local-dependent and drop it from the first-attempt warm walk.

Closes #1610

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 21:08
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 6bb20ac94 with 739dfafb7 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 4.01 3.65 −9.0% ±14% ⚪ within noise
octane-tsrx replace 9.66 9.05 −6.3% ±18% ⚪ within noise
octane-tsrx add 4.24 3.88 −8.5% ±31% ⚪ within noise
octane-tsrx update 0.68 0.64 −5.9% ±38% ⚪ within noise
octane-tsrx select 0.28 0.28 +0.0% ±42% ⚪ within noise
octane-tsrx swap 0.48 0.49 +2.1% ±28% ⚪ within noise
octane-tsrx remove 0.50 0.49 −2.0% ±37% ⚪ within noise
octane-tsrx runlots 36.09 37.04 +2.6% ±20% ⚪ within noise
octane-tsrx select_lots 0.30 0.29 −3.3% ±70% ⚪ within noise
octane-tsrx clear 50.09 56.46 +12.7% ±31% ⚪ within noise
octane-jsx run 4.19 4.38 +4.5% ±21% ⚪ within noise
octane-jsx replace 10.36 11.53 +11.3% ±30% ⚪ within noise
octane-jsx add 3.62 4.48 +23.8% ±33% ⚪ within noise
octane-jsx update 0.81 1.00 +23.5% ±64% ⚪ within noise
octane-jsx select 0.51 0.52 +2.0% ±47% ⚪ within noise
octane-jsx swap 0.85 1.03 +21.2% ±31% ⚪ within noise
octane-jsx remove 0.85 0.97 +14.1% ±53% ⚪ within noise
octane-jsx runlots 34.48 33.98 −1.5% ±12% ⚪ within noise
octane-jsx select_lots 2.73 2.78 +1.8% ±23% ⚪ within noise
octane-jsx clear 50.30 50.02 −0.6% ±30% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit cda985b into main Oct 2, 2026
52 checks passed
@trueadm
trueadm deleted the perf/strong-inline-render-calculations branch October 2, 2026 21:26
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.

Strong auto-calculation skips inline render expressions that memoize when extracted

1 participant