Skip to content

fix(compiler): inline plain-module memos beside parent-printed syntax - #1577

Merged
trueadm merged 3 commits into
mainfrom
fix/plain-hook-memo-template-literal
Oct 2, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/plain-hook-memo-template-literal

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • The production inline memo tier for plain .ts/.js hook modules (inlinePlainHookMemos) silently fell back to the surgical path for any module with a template literal. canPrintProgram required an esrap visitor for every node, and esrap prints TemplateElement from the TemplateLiteral visitor, so it has none. SwitchCase, CatchClause, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportAttribute, and TSDeclareMethod (overload signatures) failed the same way. Main admitted only 41% of parseable repo modules.
  • Those node types are now accepted, but only in the parent field whose visitor prints them. TSTemplateLiteralType.quasis stays rejected because esrap 2.3 drops its final quasi.
  • Opening the tier to 99% of modules exposed esrap misprints in node types that do have visitors, and several of them already reach the tier on main. The check now keeps those modules on the surgical path. It runs on the final Program, because hook lowering can remove an authored shape.

Why

Production client builds take this tier by default (bundler.js inlinePlainMemo). A binding module with a default import, a switch, or a template literal lost the closure-free memo lowering without any signal.

esrap misprints that the widened check must not admit (verified against esbuild and tsc):

Authored esrap prints Effect
declare global { … } declare global global { esbuild error
(ref.current as any) = v / (x as T)++ / [(x as T) = 1] = y parentheses dropped esbuild error
box?.item!.label (box?.item!).label throws where it short-circuited (live on main)
import type {} from './x' import './x' adds a side-effect import (live on main)
static override m() / protected abstract m() modifiers reordered invalid TS
tag<T>\…`, { m() {} }, ([a]: T[]) =>, extends Base+ line-broken{` type arguments or annotations dropped / parenthesized lost TS

Changes

  • packages/octane/src/compiler/plain-hook-memo.js: walkNodes passes (parent, field). A PARENT_PRINTED allowlist keys each child-only type to the fields that print it. misprints(node) covers the table above, and nonNullEndsChain fires only when a parenthesized ! sits under a non-optional link with an optional link below it. The visitor check stays on the authored AST as before; the misprint check runs on the transformed Program right before the single print.
  • Tests:
    • tests/compiler/plain-hook-memo-codegen.test.ts: every parent-printed type takes the inline path (a source map is returned) and keeps its authored TypeScript syntax tree.
    • The same file has one table row per misprint guard, checking authored syntax survives whichever tier runs, plus a neighbour test that ordinary non-null chains stay inline.
    • tests/plain-hook-memo.test.ts: runtime behavior with inline on and off, both for a template-literal/switch/catch/namespace-import module (also pinning the tier) and for props.box?.item!.label short-circuiting.
  • benchmarks/hook-memo/plain-hooks.ts: slot Symbols now take template-literal descriptions, so the existing eligible_hit_* ratio guards cover this regression.
  • docs/decallback-memo.md: what "unsupported printing shape" means.

Audit

  • Child-only types were found by walking the esrap tsx visitor table against 19,664 repo modules plus synthetic TS syntax.
  • Fidelity check: print every admitted .ts/.js module, then compare TypeScript syntax trees (parentheses unwrapped, optional-chain and type-only flags kept). Also require that both esbuild and tsc parse the output.
  • Final result: 15,912 of 16,062 admitted, 0 new esbuild or TS parse failures. The 63 remaining tree diffs are { a: a } → { a } and export { default as default } → export { default }, which are equivalent.
  • .tsx/.jsx/.mjs never reach this tier (plainHelperSource).

Performance audit

  • Target: production-client compile of plain hook modules; per-render memo allocation; shipped bytes.
  • Allocation: benchmarks/hook-memo with the template-literal fixture, main vs this branch, inline variant. Values are per 32 dependency-hit renders; value/identity controls pass in both.
    • plain_hook: function expressions 128 → 64, arrays 512 → 384.
    • plain_explicit_slot: function expressions 192 → 64, arrays 640 → 384.
    • Inline bundle gzip 74,257 → 74,209 B.
  • Admission: every memo-bearing plain module in packages/*/src, examples/*/src, and website/src (589), through createOctaneCompiler().transform.
    • Inline 195 → 320; no module main inlined falls back.
    • The 125 newly inlined modules grow +16,593 B min+gzip (+9.0% for those modules; the de-callbacked lowering is larger). The other 460 are byte-identical.
    • Transform time ≈ +10% for this set: best of 3, 2.90 s → 3.23 s.
  • Bytes: bundle-size / bundle-reachability, main vs this branch: 33 of 35 rows byte-identical; only the apollo-client binding moves (vite gzip +4 B, esbuild gzip −42 B).
  • Note: on main, node benchmarks/bench.mjs --ratios hook-memo breaches 9 guards with or without this change (identical ratios). CI's bench job fails earlier at the Lynx diagnostic step, so it never sees them. Filed separately.

Validation

  • targeted: plain-hook-memo-codegen.test.ts, plain-hook-memo.test.ts, every packages/octane/tests file that uses inlineHookMemo/slotHooks/createOctaneCompiler, and packages/octane/tests/compiler: 137 files, 5,400 tests pass across octane and octane-prod.
  • each new test fails on main or with its guard disabled
  • pnpm sync (no changes); pnpm format:files:check and pnpm typecheck:files on changed files. The codegen test file's TS2531 at the out!.map line also exists on main, and CI typecheck excludes packages/octane/tests.
  • pnpm test / pnpm typecheck / pnpm format:check (full): not completed locally. The machine was overloaded by concurrent sessions: the unsharded run hit an OOM, and the shard runs failed only on 5 s timeouts, where every file re-run in isolation passed. Local full runs were then stopped on request, so CI is the gate.

Risk / follow-ups

  • More modules now reprint through esrap. The misprint inventory is empirical (corpus + targeted probes), so a printer gap outside the corpus can still exist; the authored-syntax guards keep each known gap pinned.
  • The esrap gaps are worth reporting upstream (sveltejs/esrap).
  • Locally load-sensitive timeouts in hmr.test.ts and volar.test.ts reproduce on main and do not touch this tier.

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 production compile routing and esrap reprint eligibility for many hook modules; misprint detection is empirical, though known gaps are test-pinned and unsafe shapes stay on the surgical path.

Overview
Production plain .ts/.js hook modules can take the inline useMemo / useCallback tier even when the file uses syntax that esrap prints from a parent visitor—template literals, switch/try/catch, default or namespace imports, import attributes, and method overload signatures. Previously, missing per-node visitors forced the whole module onto the slower surgical path with no signal.

The compiler now walks the AST with parent/field context, treats those child-only shapes as printable when they sit in the right parent slot, and adds a post-lowering fidelity gate (misprints / printsFaithfully) so modules still fall back unchanged when esrap would rewrite behavior or types—e.g. declare global, empty import type {}, cast assignment targets, or box?.item!.label optional-chain continuations.

Docs, hook-memo benchmarks (template-literal slot symbols), and codegen/runtime tests lock in both the widened admission and the misprint guards.

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

trueadm and others added 2 commits October 2, 2026 16:25
The production inline memo tier for plain .ts/.js hook modules bailed for
any module containing a template literal: canPrintProgram required an esrap
visitor for every node, but esrap prints TemplateElement (and SwitchCase,
CatchClause, import specifiers and attributes, overload signatures) from
the parent's visitor. Accept those types in the parent field that prints
them.

Widening the tier exposed esrap shapes that have a visitor but misprint:
`declare global`, cast assignment targets, a non-null assertion that
continues an optional chain, an empty `import type {}`, member modifier
order, and dropped type arguments or annotations. Keep those modules on
the surgical path, checking the Program as printed.

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 2, 2026
@github-actions
github-actions Bot marked this pull request as draft October 2, 2026 15:27
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-reachability: 1 value(s) increased

Compares 416882fc3 with e0ba2eabb 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.25 4.26 +0.2% ±22% ⚪ within noise
octane-tsrx replace 8.54 8.54 +0.0% ±20% ⚪ within noise
octane-tsrx add 3.79 3.86 +1.8% ±16% ⚪ within noise
octane-tsrx update 0.39 0.56 +43.6% ±61% ⚪ within noise
octane-tsrx select 0.15 0.18 +20.0% ±101% ⚪ within noise
octane-tsrx swap 0.38 0.42 +10.5% ±40% ⚪ within noise
octane-tsrx remove 0.41 0.50 +22.0% ±52% ⚪ within noise
octane-tsrx runlots 34.71 34.95 +0.7% ±16% ⚪ within noise
octane-tsrx select_lots 0.25 0.27 +8.0% ±39% ⚪ within noise
octane-tsrx clear 46.96 48.79 +3.9% ±8% ⚪ within noise
octane-jsx run 4.01 4.34 +8.2% ±12% ⚪ within noise
octane-jsx replace 8.08 9.04 +11.9% ±19% ⚪ within noise
octane-jsx add 3.78 3.72 −1.6% ±13% ⚪ within noise
octane-jsx update 0.62 0.83 +33.9% ±51% ⚪ within noise
octane-jsx select 0.50 0.49 −2.0% ±54% ⚪ within noise
octane-jsx swap 0.76 0.84 +10.5% ±28% ⚪ within noise
octane-jsx remove 0.64 0.69 +7.8% ±31% ⚪ within noise
octane-jsx runlots 32.35 32.44 +0.3% ±10% ⚪ within noise
octane-jsx select_lots 3.26 3.32 +1.8% ±22% ⚪ within noise
octane-jsx clear 50.33 49.36 −1.9% ±8% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
binding-apollo-client-vite raw 356,985 356,969 −16 (−0.0%) 🟢
binding-apollo-client-vite gzip 111,787 111,780 −7 (−0.0%) 🟢
binding-apollo-client-vite brotli 97,552 97,675 +123 (+0.1%) 🔴
binding-apollo-client-esbuild raw 359,963 359,923 −40 (−0.0%) 🟢
binding-apollo-client-esbuild gzip 114,003 113,995 −8 (−0.0%) 🟢
binding-apollo-client-esbuild brotli 99,309 99,251 −58 (−0.1%) 🟢

99 other measured values are unchanged.

@trueadm
trueadm marked this pull request as ready for review October 2, 2026 15:41
@trueadm
trueadm merged commit 3514dc7 into main Oct 2, 2026
49 checks passed
@trueadm
trueadm deleted the fix/plain-hook-memo-template-literal branch October 2, 2026 15:58
trueadm added a commit that referenced this pull request Oct 3, 2026
…owering (#1634)

Production builds of plain .ts/.js hook modules grew with #1546 (a withSlot
boundary per local custom-hook call) and #1577 (the inline memo tier admitted
~99% of modules). This trims both without changing behavior:

- hook-deps.js omits a module-declared hook's call boundary when the hook
  reads no slot (it calls nothing but useContext and other such hooks), or
  when it is the hook's only use, made directly by another local hook that is
  used only by name, outside any cycle of such calls. Signal-reading modules
  and `$` hooks keep every boundary.
- memoSlot receives the hook name only for an authored slot, which a manual
  hook may leave undefined; a compiler-appended slot always resolves.
- A plain-module memo dependency that is a never-reassigned local binding is
  read in place instead of through a temporary. Imports stay copied: they
  are live bindings the factory can change.
- The inline tier's admission and print move into createPlainProgramPrinter,
  and a corpus test reprints every Octane module in packages/*/src through it,
  comparing TypeScript syntax trees and requiring TypeScript and esbuild to
  parse the output, so an esrap gap fails CI instead of shipping to
  production only.

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