Skip to content

fix(compiler): cache a const that only another cached calculation reads - #1625

Merged
trueadm merged 1 commit into
mainfrom
fix/chained-calculation-cache
Oct 3, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/chained-calculation-cache

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Automatic calculation cached a const only when the template read it. In const a = f(x); const b = g(a); <p>{b}</p>, a recomputed every render, so b's cache (keyed on a) missed every render and g ran every render too. The same applied to a const read only by a Strong inline calculation hoisted into __calc. Found in the 2026-10-02 review as the G-1 follow-up.

  • closeCalculationReads (in compile.js, beside rewriteAutoCalculation) seeds from collectRenderReadNames and runs a worklist to a fixed point: every const that rewriteAutoCalculation would cache adds its dependencies. It covers compileFunctionBody and compileReturnJsxFunction. In return-JSX components only statements after the first authored hook take part, because nothing before it is cached.
  • Existing exclusions hold. Hook calls, live member calls, let, self-references, and handler-only values add nothing. nativeReads lowering applies to every rewritten const.
  • Fixes a render crash. A cache evaluates its dependencies at its declaration, so const x = make(() => later); const later = f(y) threw Cannot access 'later' before initialization once the template read x. That was already true on main. Following chains would have spread it: the corpus compile hit it in signals-parallel-use ParallelAuthLocal under Strong. A calculation whose dependency is declared later now stays uncached.
  • Shared helper. calculationDeclarator holds the declarator shape check that rewriteAutoCalculation and the closure share.
  • Docs and changeset. One paragraph on chains in docs/differences-from-react.md, and a patch changeset.

Validation

  • pnpm format:check, not run (scoped pnpm format:files:check on every changed file passes)
  • pnpm typecheck, not run. Scoped pnpm typecheck:files on the changed tests and fixtures reports three errors, all on lines this PR does not touch: the existing .ts/.tsx extension imports and auto-memo-child.tsrx.
  • pnpm test, not run (one owning file and one project at a time, per local load rules)
  • targeted tests:
    • auto-calculation.test.ts: 21/21 in octane and in octane-prod. The new cases compile with prod options. They cover a three-link @{} chain, a return-JSX chain, Strong @{} and return chains through __calc, and the later-const case in direct, chained, and Strong forms. Every new case failed before the fix, or with the guard disabled. The chained and Strong later-const forms each fail alone without the guard.
    • signals-rendering.test.ts: 26/26 in octane-signals, octane-signals-prod, and octane-signals-strong. The new native-reads chain reads a signal in its first link. It failed without the closure, and it goes stale ('1' instead of '5') if the native witness check is removed.
    • signals-parallel-use.test.ts: 6/6 in octane-signals-strong and octane-signals-prod. parallel-use.test.ts: 64/64 in octane.
  • Compiled-output size. Every repo .tsrx (Ripple benchmark sources excluded) was compiled with { mode: 'client', hmr: false, dev: false }, plain and with strong: true, comparing main's compiler with this branch:
    • Compat: 5 outputs changed, +2,427 B (+0.011%), +327 B gzip.
    • Strong: 6 outputs changed, +10,596 B (+0.090%), +1,406 B gzip. About 9 KB of that is the signals-parallel-use test fixture.
    • Each changed output only caches a chain link: getAssetKeys, errorArguments, getInternalNodesBounds, createStyleCache, or local query$. The template-read form already got that shape on main.
    • The CI-gated minimal bundle-budget fixtures, including root-chained-jsx.tsx, compile byte-identical.
    • No timing benches run. Corpus compile time was within noise.

Risk / follow-ups

  • Merging with the unopened Review-G fix (G-1/G-2/G-3/G-12). That fix is still uncommitted elsewhere. Its collectMutatedNames exclusion has to act inside closeCalculationReads, skipping or dropping names the way the later-binding guard does. Until then, the closure widens the pre-existing G-12 hazard: in const list = make(rows); list.push(x); const v = wrap(list), list is now cached. This closure also replaces that fix's G-1 change.
  • More cache hits can expose impure imported functions. A downstream calculation that always missed now hits, so an imported function with side effects runs less often. This is the existing purity contract, exercised more often.
  • octane-signals-strong doesn't reach this path. It passed signals-parallel-use even with the guard disabled. The source-loader tests above and the corpus compile are the evidence for production Strong chains.

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
Compiler optimization changes when derived values are memoized and can alter call frequency for impure helpers; TDZ guard avoids a class of runtime crashes but chain closure may cache more bindings until related follow-up fixes land.

Overview
Extends automatic const caching to full dependency chains so intermediate derived values stay stable when only the last link is read in the template (including Strong hoisted inline calculations). The compiler now runs closeCalculationReads after collectRenderReadNames, walking eligible cached calculations to a fixed point before rewriteAutoCalculation runs in @{} bodies and post-hook return <jsx> components.

Guards against temporal-dead-zone crashes: calculations whose dependency tracking would read a const declared later are removed from the cached set and left uncached (directly and when reached via chain closure).

Docs note chain behavior; tests cover multi-link chains, return-JSX/Strong paths, forward-read cases, and signal-invalidated chains (NativeChainedReader).

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

Automatic calculation cached a `const` only when the template read it, so
in `const a = f(x); const b = g(a); <p>{b}</p>` `a` recomputed every
render and `b`'s cache, keyed on `a`, missed every render too. The same
happened to a const read only by a Strong inline calculation hoisted into
`__calc`.

closeCalculationReads closes the template's reads through every
calculation rewriteAutoCalculation would cache, with a worklist run to a
fixed point, in compileFunctionBody and compileReturnJsxFunction. Hook
calls, live receivers, `let`, self-references, and handler-only values
still add nothing.

A cache evaluates its dependencies at its declaration, so a calculation
whose callback reads a later binding threw in the temporal dead zone. Main
already crashed when the template read one; following chains would have
spread it. Such a calculation now stays uncached.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 3, 2026
@github-actions
github-actions Bot marked this pull request as draft October 3, 2026 07:29
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares 11a9da935 with 9ec949beb 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 3.37 3.44 +2.1% ±22% ⚪ within noise
octane-tsrx replace 6.73 7.02 +4.3% ±25% ⚪ within noise
octane-tsrx add 3.42 3.28 −4.1% ±28% ⚪ within noise
octane-tsrx update 0.33 0.34 +3.0% ±44% ⚪ within noise
octane-tsrx select 0.11 0.13 +18.2% ±118% ⚪ within noise
octane-tsrx swap 0.34 0.30 −11.8% ±19% ⚪ within noise
octane-tsrx remove 0.34 0.30 −11.8% ±35% ⚪ within noise
octane-tsrx runlots 28.28 28.17 −0.4% ±14% ⚪ within noise
octane-tsrx select_lots 0.22 0.24 +9.1% ±60% ⚪ within noise
octane-tsrx clear 39.93 40.06 +0.3% ±8% ⚪ within noise
octane-jsx run 3.47 3.60 +3.7% ±11% ⚪ within noise
octane-jsx replace 6.53 7.39 +13.2% ±18% ⚪ within noise
octane-jsx add 3.08 3.16 +2.6% ±10% ⚪ within noise
octane-jsx update 0.57 0.49 −14.0% ±33% ⚪ within noise
octane-jsx select 0.37 0.36 −2.7% ±44% ⚪ within noise
octane-jsx swap 0.65 0.57 −12.3% ±20% ⚪ within noise
octane-jsx remove 0.59 0.59 +0.0% ±23% ⚪ within noise
octane-jsx runlots 29.27 28.10 −4.0% ±9% ⚪ within noise
octane-jsx select_lots 2.50 2.55 +2.0% ±19% ⚪ within noise
octane-jsx clear 41.32 42.95 +3.9% ±22% ⚪ 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 marked this pull request as ready for review October 3, 2026 08:27
@trueadm
trueadm merged commit de31710 into main Oct 3, 2026
83 of 86 checks passed
@trueadm
trueadm deleted the fix/chained-calculation-cache branch October 3, 2026 09:58
trueadm added a commit that referenced this pull request Oct 3, 2026
…#1647)

The dev-SSR hydration e2e picked its port with getFreePort(), which closed
the probe socket before the .vite cache removal and the vite spawn. On PR
#1625 another process took the port in that window and the strictPort dev
server died with "Port 41193 is already in use".

- reserveFreePort() now holds the port on 127.0.0.1 and ::1. The servers
  listen on `localhost`, which Node can resolve to ::1, and a hold on
  127.0.0.1 alone does not stop a ::1 listener from taking the same port.
- The dev server clears the optimize-deps cache first, then reserves, and
  releases immediately before the spawn via startServerOnFreePort().
- startServerOnFreePort() retries, on a fresh reservation and at most three
  times, only when the child exits before listening because vite reported
  that exact port in use. The pnpm and vite startup between release and
  bind is a window no reservation can cover.
- getFreePort() is removed; it had no other caller.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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 fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant