Skip to content

perf(compiler): skip parsing modules the Vite plugin only passes through - #1852

Merged
trueadm merged 1 commit into
mainfrom
perf/nitro-passthrough-parse
Oct 7, 2026
Merged

trueadm merged 1 commit into
mainfrom
perf/nitro-passthrough-parse

Conversation

@trueadm

@trueadm trueadm commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Nitro (TanStack Start) rebundles the SSR build's own chunks from node_modules/.nitro/vite/services/ssr/assets/*.js, and Octane's Vite plugin runs over each one again. On main, each big chunk was parsed twice by @tsrx/core parseModule, whose comment attachment is quadratic. Both parses were wasted:

  1. Preflight before any ownership check. vite.js preflightFor parsed every module in full, then derived facts that only compiled modules, hook-slotted modules, or virtual barrels can read.
  2. Wrong ownership for build caches. _isInstalledOctaneSource walked from website/node_modules/.nitro/…/docs-*.js up past node_modules to website/package.json, which declares octane. The plain-module text gate /from\s*['"]octane['"]/ matched code samples inside the chunk's strings, so slotHooks parsed the 5 MB chunk again and returned it unchanged.

This PR removes both parses in Octane itself. The @tsrx/core parser patch in #1399 is a patchedDependencies entry, and patches don't ship in the published tarball, so without this change Octane + TanStack Start + Nitro apps keep paying the cost. #1399's patch is not touched.

Changes

  • Ownership boundary (bundler.js _nearestOctanePackageRule). The nearest-manifest walk now stops at a node_modules directory. A file that reaches one without its own manifest belongs to no package. Node's package scope lookup (getPackageScopeConfig) draws the same boundary. It covers .vite, .nitro, and .cache build caches, and loose files in node_modules. Real installed packages are unaffected because their own manifest sits below the boundary, and so are linked or symlinked packages, which resolve to real paths.

  • Parse-free preflight scope (bundler.js _preflightScope / _mayHookSlot). The compiler classifies a module without parsing it:

    • compile: a full-compile source. Preflight collects every fact, as today.
    • slots: a plain .ts/.js module that passes transform's parse-free slot gates. Preflight collects void imports only.
    • none: everything else.

    transform's plain-module branch now calls the same _mayHookSlot, so the two can't drift.

  • Preflight parses only when a fact needs the AST (vite.js). These facts can't arise for a module the compiler won't compile:

    Fact Who reads it Non-compiled module
    CSS-module requests full compile only (already gated on _isFullCompileSource) none
    descriptor-export receipt full compile only (preparedDescriptorChildrenExports) null
    descriptor imports JSX-tag keys are read by full compile only; re-export keys survive the existing !existsSync filter only for a module absent from disk parsed only when virtual, so plain .js/.ts virtual barrels still publish DESCRIPTOR_CHILDREN_EXPORTS_META
    void imports full compile and slotHooks parsed only for slots
    server import requests every server module (assertNoLiveClientOnlyImports) still collected, see next item
  • Server import requests without a parse (findServerImportRequests + runtime-requests.js). For .js/.mjs/.cjs, the requests come from es-module-lexer, which Octane already uses for runtime-request targeting. When the lexer rejects a module, for example JSX in .js, the parse path runs as before. .ts keeps the parse because of type-only imports.

Measurements

All measurements are local on an Apple M5 Max (load average around 6–7, other sessions running), with @tsrx/core 0.4.0 as on main.

Website production build (pnpm --filter website build, fresh worktree, main 83eaaf4 vs this branch):

Phase main this PR
client 15.44 s 10.71 s
ssr 9.45 s 7.78 s
nitro ("building nitro environment") 2 m 50 s 0.49 s

Semantic control: all 967 emitted files in the client, SSR, and Nitro phases have identical hashed names and sizes in both build logs. The SSR chunks under node_modules/.nitro/vite/services/ssr are byte-identical (diff -rq).

Plugin transform on the 304 real Nitro-phase chunks. Same harness, server environment, website config with threeRenderers, main's src extracted with git archive:

Chunk main this PR
docs (4.9 MB) 92,881 ms 36 ms
benchmarks (1.5 MB) 78,954 ms 8 ms
playground (2.3 MB) 2,242 ms 14 ms
routes (0.7 MB) 402 ms 6 ms
all 304 174,743 ms 89 ms

Every chunk's result is a null pass-through in both trees. Before this PR, the docs time split as 44.7 s parseModule in preflight plus 49.0 s in compiler.transform (slotHooks). Benchmarks was 78 s of preflight parse.

Lexer vs. parser differential. es-module-lexer static requests (d === -1) were compared with findStaticRuntimeImportRequests:

  • 300 parseable SSR chunks: 0 mismatches.
  • 39,689 parseable .js/.mjs files from node_modules/.pnpm: 0 under-reports. The 4 over-reports are Flow import type lines in @emotion/*/src, which the client-only live-use check already skips.

Validation

  • targeted tests, --project octane:
    • compiler/bundler-compiler.test.ts and compiler/compiler-vite-options.test.ts: 108 passed
    • vite-void-import-cycles, external-hook-slot, manual-state-getters, vite-text-types, and compiler/renderer-boundary-analysis: 61 passed
  • new tests fail when the change is broken:
    • does not attribute a node_modules build cache to the package that installed it fails with the boundary disabled (client: expected 'slots' to be 'none').
    • checks a rebundled server chunk's imports, not the code samples in its strings fails if the lexer path returns no requests, and fails if it is replaced by a regex scan that reads strings (it then resolves ./Leak.object.tsrx and octane). It passes on main, because it pins behavior this change preserves.
    • Skipping void imports for slots fails 4 vite-void-import-cycles real-build tests.
    • Skipping descriptor imports for virtual modules fails carries descriptor-children export metadata through the Vite module graph.
  • prettier --write on the changed files (no changes); pnpm sync (no generated changes); react-parity/check.mjs --validate-only (current)
  • pnpm format:check, pnpm typecheck, pnpm test: left to CI, per the local test-load rule. typecheck:files on the touched test files reports only its known tsgo selection artifacts on existing lines, none on changed lines.

Risk / follow-ups

  • Fail-open becomes fail-closed in one corner. Take a server .js module that @tsrx/core cannot parse but the lexer can read, such as the website's playground chunk. On main it got no server import requests, so the client-only check was skipped silently. Now its static imports are classified. If one of them resolved to a client-only module, the live-use check would report a parse error instead of passing silently. No website module hits this.
  • A project-owned .ts/.js file with Octane code samples in strings still passes the regex gate and gets parsed by slotHooks. This PR only fixes ownership for build caches. Making that gate lexical is separate work, because it decides which modules reach slotting.
  • findServerImportRequests can over-report Flow import type requests, at the cost of one extra resolve each. The live-use check ignores type imports.

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
Touches compiler ownership boundaries, preflight gating, and server client-only import discovery; behavior changes for edge cases (e.g. lexer-readable but parser-failing .js modules) though outputs are intended to stay hash-identical.

Overview
Speeds up Octane’s Vite/Nitro path by not fully parsing modules that only pass through, and by fixing wrong package ownership for rebundled SSR chunks under node_modules/.nitro (and similar caches).

Package scope stops at a bare node_modules directory when walking for package.json, so cache chunks no longer inherit the app’s Octane manifest and trigger hook-slotting from string-matched samples inside chunk strings.

Preflight classifies each module as compile, slots, or none via _preflightScope / _mayHookSlot (shared with transform); it parses with parseModule only when an AST-backed fact is needed. Pass-through modules still collect server static imports via lexStaticImportRequests (es-module-lexer) for plain .js/.mjs/.cjs, so imports in strings/comments are ignored.

Tests cover cache ownership and Nitro chunk client-only resolution behavior.

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

Nitro rebundles the SSR build's chunks from node_modules/.nitro through
Octane's Vite plugin. The plugin's preflight parsed every module in full
before checking ownership. The compiler then attributed the chunks to the
application package.json by walking up past node_modules. Code samples in
the chunks' strings also matched the hook-import text gate, so slotHooks
parsed them a second time and returned them unchanged.

- Stop the nearest-manifest walk at a node_modules directory, as Node's
  package scope lookup does. Build caches no longer inherit the app manifest.
- Preflight parses only when compilation, hook slotting, or a virtual
  barrel's descriptor re-exports can read a fact. The decision comes from
  a parse-free compiler scope that shares transform's slot gates.
- Read a plain JavaScript module's server import requests with the module
  lexer instead of a full parse.

Website production build, local: Nitro phase 2m 50s -> 0.5s. All 967
emitted files keep their hashes.

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

cloudflare-workers-and-pages Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 1773cd8 Commit Preview URL

Branch Preview URL
Oct 07 2026, 05:31 PM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 1773cd8 Commit Preview URL

Branch Preview URL
Oct 07 2026, 05:33 PM

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🟡 js-framework: 1 timed operation(s) slower beyond ±3%

Compares 83eaaf4ae, the merge commit's first parent with bfb338058 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

target operation base ms head ms head/base 95% CI
octane-tsrx replace 7.150 7.575 +7.0% +3.5% … +9.8% 🟡 slower
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.942 2.800 ±0.0% −3.1% … +1.6% ⚪ within noise
octane-tsrx replace 7.150 7.575 +7.0% +3.5% … +9.8% 🟡 slower
octane-tsrx add 2.575 2.600 −0.0% −4.2% … +3.5% ⚪ within noise
octane-tsrx update 0.126 0.127 −0.4% −2.0% … +4.9% ⚪ within noise
octane-tsrx select 0.006 0.006 −1.0% −1.8% … +1.0% ⚪ within noise
octane-tsrx swap 0.031 0.032 +1.3% −0.3% … +4.3% ⚪ within noise
octane-tsrx remove 0.016 0.016 −0.0% −2.9% … +4.2% ⚪ within noise
octane-tsrx runlots 26.708 29.183 +1.3% −1.9% … +4.0% ⚪ within noise
octane-tsrx select_lots 0.007 0.007 −0.3% −3.5% … +2.7% ⚪ within noise
octane-tsrx clear 50.633 55.842 +4.4% ±0.0% … +10.3% ⚪ within noise
octane-jsx run 3.967 4.100 −2.5% −10.5% … +2.5% ⚪ within noise
octane-jsx replace 9.508 10.058 +1.7% −6.8% … +15.5% ⚪ within noise
octane-jsx add 3.508 3.350 −2.8% −5.6% … +0.0% ⚪ within noise
octane-jsx update 0.270 0.265 −0.3% −3.7% … +1.6% ⚪ within noise
octane-jsx select 0.145 0.150 +2.4% −1.5% … +5.1% ⚪ within noise
octane-jsx swap 0.241 0.233 −1.2% −3.5% … −0.0% ⚪ within noise
octane-jsx remove 0.182 0.176 +0.0% −2.4% … +2.4% ⚪ within noise
octane-jsx runlots 29.267 29.058 +1.3% −3.1% … +2.9% ⚪ within noise
octane-jsx select_lots 1.617 1.639 +0.3% −2.4% … +4.7% ⚪ within noise
octane-jsx clear 50.975 50.425 −0.1% −5.0% … +2.4% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 108 measured values.

@trueadm
trueadm merged commit 7e6bda5 into main Oct 7, 2026
50 of 53 checks passed
@trueadm
trueadm deleted the perf/nitro-passthrough-parse branch October 7, 2026 18:02
trueadm added a commit that referenced this pull request Oct 7, 2026
Conflicts: app-budgets.json and minimal-budgets.json, where #1854 and
#1855 lowered the rows this branch had tightened. Take main's values,
then write this branch's measurements (`--write-budgets`) on the merged
tree, which only lowers: bindings app gzip and total raw, one app
brotli, and three minimal gzip rows. hydrate-frame stays at main's
364,882 raw. It measures 364,886 here and on main alike, and main's own
CI fails the same check, so the raise belongs in a budget-only change on
main, not in this PR.

#1852 stops the Vite plugin parsing modules it only passes through, such
as the SSR chunks Nitro re-bundles. The @tsrx/core patch still bounds
every module the plugin does compile, so its guard now transforms a
540 kB project module that uses a hook instead of a Nitro chunk: about
2 s patched, 20 s unpatched, against a 6 s bound. The patch note says
so.

Reformat main's new hydrate-frame fixture; its output is unchanged once
position-derived ids are masked. main's changed test files pass on this
branch (bundler-compiler, compiler-vite-options, behavior-root,
binding-handoff, style-transaction-rollback).

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 perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant