Skip to content

fix(octane): fold profiling guards out of the vite dev runtime - #1898

Merged
trueadm merged 2 commits into
mainfrom
fix/vite-dev-profile-guards
Oct 8, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/vite-dev-profile-guards

Conversation

@trueadm

@trueadm trueadm commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • vite dev with profiling off no longer serves Octane's runtime with profiling guards or the profiler. The Vite plugin folds __OCTANE_PROFILE_ENABLED__ out of Octane's own runtime modules at transform time and drops the helpers and imports only profiling used, so profiling.ts and devtools-hook.ts are never fetched or evaluated.
  • Apps that get octane pre-bundled by Vite's dependency optimizer (compiler plugin only, octane from npm) get the same result: the optimizer now receives the constant through optimizeDeps.rolldownOptions.transform.define.
  • Profiling-on dev servers (profile: true, or devtools: true resolving to 'auto' under vite dev) and all production builds are unchanged.

Follow-up to #1886 / #1894. The gap predates #1894 and affects every existing guard.

Why

Vite's define is static replacement only in vite build. In vite dev, the vite:define plugin skips client-consumer modules and vite/dist/client/env.mjs installs each entry as a global (globalThis.__OCTANE_PROFILE_ENABLED__ = false). Measured on main before this change, the dev-served runtime.ts had 138 reads of the define across its guards (createBlock, renderBlockInner, renderBranchSlot, unmountBlockInner, drainQueue, setState paths, …), each a global read plus typeof, and it statically imported profiling.ts and devtools-hook.ts (about 41 KB of source). The dependency optimizer also ignores define, so a pre-bundled octane carried the same 136 reads and the whole profiler.

Changes

  • packages/octane/src/compiler/profile-guards.js (new): folds one module given its ESTree (from Vite's this.parse, oxc).
    • Replaces a guard that folds to a constant with that literal, keeps the live arm of an if, conditional, or &&/|| chain, and folds whole operand chains, since a && typeof X !== 'undefined' && X nests as (a && typeof …) && X.
    • Keeps operands that run before a guard and may have effects. if (!block.disposed && guard) becomes {(!block.disposed);}, which is what a production build emits.
    • Removes top-level functions and imports that only the folded code used. This is limited to the octane package, whose sideEffects contract is what lets production bundlers drop the same imports.
    • It is a narrow text edit like slot-hooks.js and runtime-requests.js. Removed ranges keep their whitespace, so line numbers stay exact without a source map. A kept expression is parenthesized only where its first token matters: a return/throw/yield argument, or a statement or arrow body start.
    • It bails (serves the module as authored) when the reserved name is a binding, write, or shorthand property, and when edits would overlap.
  • packages/octane/src/compiler/vite.js:
    • In serve with profiling off, runs the fold on modules under this package's src/ or dist/ that mention the define, before the existing compiler pipeline. Modules the compiler leaves alone still return the folded code.
    • Memoizes the last fold per module: Vitest runs one dev server per project in a single process.
    • Adds the optimizer define, carrying a deprecated user esbuildOptions.define over because Vite copies it only when rolldownOptions.transform.define is unset.
    • PROFILE_DEFINE now comes from profile-guards.js.
  • .rulesync/rules/core-engineering.md (+ generated copies): lists profile-guards.js as a third narrow text-edit exception. Reprinting the 51k-line runtime through esrap on every dev start was the rejected alternative: @tsrx/core parses runtime.ts in about 975 ms, against about 95 ms for oxc.
  • website/src/content/docs/profiling.mdx: the cost section now says the Vite dev server drops the branches, and that Rspack's unminified dev output keeps them as dead code.

Validation

  • pnpm format:files:check on every changed file
  • pnpm typecheck:files on the changed source and test files
  • pnpm typecheck (not run locally; CI)
  • pnpm test (not run locally per the shared-machine load rule; CI)
  • targeted tests:
    • packages/octane/tests/vite-dev-profile-guards.test.ts (new, 3 tests). Starts a real Vite dev server on a temp app.
      • Off: the browser environment's runtime.ts and universal-core.ts have no define and import neither profiling module. The app runs in a runnable client-consumer environment (click → 1), the module graph never loads profiling.ts or devtools-hook.ts, and no __OCTANE_PROFILER__ or __OCTANE_DEVTOOLS__ global is installed.
      • On: imports are kept, profiler records component-render events and component.render/commit.root counters, and both globals are installed.
      • Pre-bundled octane: no define read and no profiler with profiling off; profiling on is the control.
      • Against main's vite.js, the off and pre-bundle tests fail and the on test passes. Removing only the import pruning also fails the off test.
    • packages/octane/tests/compiler/profile-guards.test.ts (new, 7 tests). Runs each module as authored, with the false global vite dev installs, and as folded, and compares results. It covers ASI after return, an object literal at arrow-body and statement heads, effectful prefixes, ||/else-if/nested conditionals, pruning, TS declarations, and shadowing. Removing head-position parens fails 2 tests; treating member reads as pure fails 1.
    • Neighbors: hooks, effect-timing, suspense-preserves-dom, state-getter, devtools-hook, profiling, devtools-prod-strip, and profiling-disabled (octane project, now running the folded runtime: 196 + 2 passing). profiling-runtime and devtools-runtime (octane-profile, 25 passing). vite-plugin-octane devtools-option and profile-bundle (4 passing).
  • Semantic equivalence. esbuild bundles of src/index.ts and src/universal.ts (define false, minifySyntax + minifyWhitespace, tree-shaking, identifiers kept) are byte-identical with the authored and the folded runtime.ts/universal-core.ts, under both NODE_ENV values, on the rebased head (feat(octane): count engine work in profile builds #1894 + fix(octane): commit the pending cue apart from its component's transition (#1864) #1888). The fold also handles the published dist JS shape: an oxc-transpiled runtime.ts folds to 0 reads with no profiling import, reparses cleanly, and keeps its line count.
  • Production unchanged. The strip audit builds a real fixture app with Vite three ways: minified 532,043 B, unminified 1,388,286 B, and the profile control 1,659,807 B. All three outputs are byte-identical to main's vite.js.
  • perf-review: see below.

Perf review: fix/vite-dev-profile-guards (origin/main..HEAD)

Scan: node scripts/perf-review-scan.mjs and node scripts/perf-review-scan.mjs packages/octane/src/compiler/ → 0 candidates.
Hot paths touched: none in shipped runtime source; runtime.ts and universal-core.ts are untouched. The served dev runtime loses its profiling work:

  • runtime.ts: 138 define reads → 0, 1,755,919 → 1,742,266 B;
  • 2 optional-module imports → 0 (about 41 KB of profiler source not fetched or evaluated);
  • pre-bundled octane: 1,452,574 → 1,414,180 B with no profiler.
# Severity Location Dimension Finding Required evidence Provided?
1 note packages/octane/src/compiler/vite.js foldServedProfileGuards compiler cost One oxc parse and fold per runtime module per dev process: about 95 ms parse + 20 ms fold for runtime.ts, about 25 + 5 ms for universal-core.ts. Memoized by exact source, one entry per module. Vite's own TS transform of runtime.ts is about 30 ms. timing of the parse/fold yes (local, Node 24, M-series)
2 note dev-served runtime runtime CPU No timing claim is made. The change removes per-call global reads and the profiler's module evaluation from dev; the evidence is the read and import counts above, not a benchmark. n/a n/a

Not checked: browser timing of dev interactions; Windows path handling (IDs are normalized to /, but there is no Windows run).

Risk / follow-ups

  • bundle size / bundle size checks and benchmark report fail on main itself at 90a57d8. The overages (+~1.3 KB raw on 29 rows, including esbuild-built bundles that never load vite.js) are identical on base and head ("No changes across 60/111 measured values"), so they are inherited, not caused here.
  • The first CI run also failed compiler-ast-emit-audit, which rejects hand-built AST node objects: the fold used a stand-in { type: 'LogicalExpression' } parent. Fixed in d37aa8c.
  • Every non-profile Vitest project now runs the folded runtime, because Vitest is a Vite dev server. That is good coverage for the fold, and it is also the main risk: a fold defect would surface across the suite. Equivalence with esbuild's production DCE is the guard.
  • Rspack/Rsbuild dev (reported, not changed). DefinePlugin already replaces the define statically in dev, so no guard reads the define at runtime. Unminified dev output still keeps 79 if (false) blocks and 4 constant "boolean" !== "undefined" expressions, and bundles profiling.ts; devtools-hook.ts is already dropped. V8 skips if (false) bodies when generating bytecode, so the residual cost is dead bytes plus one module evaluation. Folding it there would need a fast parser in @octanejs/rspack-plugin, or about 1 s of @tsrx/core parsing per cold start, so it is not cheap. Left as a possible follow-up.
  • With resolve.preserveSymlinks: true and a symlinked octane, module IDs are not under the real package root, so the fold does not apply and those apps keep the old dev behavior.

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
Dev-only AST text transforms on the full runtime affect every Vitest/Vite dev session; incorrect folding would change semantics, though production is untouched and equivalence is heavily tested.

Overview
Vite dev with profiling off no longer serves Octane’s runtime with live __OCTANE_PROFILE_ENABLED__ checks or optional profiler modules. Vite leaves define as runtime globals in dev, so guards stayed on hot paths and profiling.ts / devtools-hook.ts still loaded.

The Vite plugin now runs a new profile-guards.js pass (documented as a third narrow text-edit exception alongside slot-hooks.js and runtime-requests.js) on Octane src/ and dist/ modules during vite serve when profiling is disabled. It folds guards to constants, keeps effectful prefixes, prunes profiling-only imports/helpers, and preserves line numbers without a source map.

optimizeDeps.rolldownOptions.transform.define gets the same constant so pre-bundled octane drops profiling code too. Profiling-on dev and production builds are unchanged. Docs note Vite dev matches production-style stripping; Rspack dev still keeps dead branches.

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

Vite's dev server installs `define` entries as runtime globals instead of
replacing them, so with profiling off the served runtime kept every
`__OCTANE_PROFILE_ENABLED__` guard as a global read on hot paths and still
loaded profiling.ts and devtools-hook.ts. The Vite plugin now folds those
guards out of Octane's own runtime modules in `vite dev`, removes the helpers
and imports only profiling used, and gives the dependency optimizer the same
constant so a pre-bundled octane drops them too. Production builds and
profiling-on dev servers are unchanged.

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 8, 2026
@trueadm
trueadm marked this pull request as ready for review October 8, 2026 14:08
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • ❌ bundle-size: 9 value(s) exceed their committed budget
  • ❌ bundle-reachability: 26 value(s) exceed their committed budget

Compares abe9b42a5, the merge commit's first parent (main has moved 2 commit(s) past the pull request's recorded base 90a57d820) with 9a8518038 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

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.708 3.817 ±0.0% −8.3% … +5.7% ⚪ within noise
octane-tsrx replace 11.125 10.217 +4.0% −14.8% … +13.2% ⚪ within noise
octane-tsrx add 3.217 3.633 +1.5% −4.2% … +14.6% ⚪ within noise
octane-tsrx update 0.156 0.160 +2.9% −5.0% … +14.0% ⚪ within noise
octane-tsrx select 0.007 0.007 −5.0% −10.1% … +5.7% ⚪ within noise
octane-tsrx swap 0.038 0.037 −0.7% −4.1% … +2.1% ⚪ within noise
octane-tsrx remove 0.021 0.020 −5.7% −17.1% … +6.8% ⚪ within noise
octane-tsrx runlots 25.300 25.600 +1.2% −3.3% … +4.9% ⚪ within noise
octane-tsrx select_lots 0.007 0.007 −0.5% −2.2% … +2.4% ⚪ within noise
octane-tsrx clear 72.617 75.458 +0.6% −10.6% … +6.4% ⚪ within noise
octane-jsx run 4.400 4.342 ±0.0% −5.1% … +5.1% ⚪ within noise
octane-jsx replace 10.250 11.683 +9.5% −2.1% … +24.0% ⚪ within noise
octane-jsx add 4.692 4.167 −7.1% −12.3% … +2.8% ⚪ within noise
octane-jsx update 0.334 0.333 +2.9% −10.9% … +14.0% ⚪ within noise
octane-jsx select 0.174 0.170 −3.1% −8.3% … +1.9% ⚪ within noise
octane-jsx swap 0.251 0.258 +1.0% −1.8% … +3.9% ⚪ within noise
octane-jsx remove 0.207 0.200 −1.7% −4.8% … +3.5% ⚪ within noise
octane-jsx runlots 31.950 32.008 −0.3% −3.2% … +5.2% ⚪ within noise
octane-jsx select_lots 1.828 1.764 −1.0% −6.3% … −0.4% ⚪ within noise
octane-jsx clear 58.958 59.092 +2.1% −1.3% … +5.2% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

❌ 9 value(s) exceed their committed budget. Reduce the growth, or raise the budget in a separate pull request that names the bytes and the reason (CONTRIBUTING.md, "Size budgets").

target metric head budget over
octane-tsrx chat_fw_gzip 36,807 36,791 +16
octane-tsrx chat_js_gzip 39,522 39,506 +16
octane-tsrx weather_js_brotli 44,467 44,466 +1
octane-tsrx bindings_fw_raw 341,240 339,858 +1,382
octane-tsrx bindings_fw_gzip 109,367 109,005 +362
octane-tsrx bindings_fw_brotli 94,998 94,788 +210
octane-tsrx bindings_js_raw 688,878 687,496 +1,382
octane-tsrx bindings_js_gzip 225,032 224,671 +361
octane-tsrx bindings_js_brotli 191,233 190,989 +244

No changes across 60 measured values.

Public-import reachability bundles (bytes)

❌ 26 value(s) exceed their committed budget. Reduce the growth, or raise the budget in a separate pull request that names the bytes and the reason (CONTRIBUTING.md, "Size budgets").

target metric head budget over
root-static brotli 45,596 45,579 +17
hydrate-root gzip 44,219 44,196 +23
hydrate-frame raw 366,104 365,796 +308
hydrate-frame gzip 113,537 113,469 +68
suspense-transition raw 141,591 140,222 +1,369
suspense-transition gzip 44,768 44,401 +367
suspense-transition brotli 39,293 39,132 +161
binding-base-ui-vite raw 199,790 198,414 +1,376
binding-base-ui-vite gzip 62,781 62,427 +354
binding-base-ui-vite brotli 54,502 54,383 +119
binding-base-ui-esbuild raw 215,315 213,931 +1,384
binding-base-ui-esbuild gzip 69,747 69,369 +378
binding-base-ui-esbuild brotli 60,479 60,247 +232
binding-radix-vite raw 201,825 200,448 +1,377
binding-radix-vite gzip 63,366 63,043 +323
binding-radix-vite brotli 55,095 54,923 +172
binding-radix-esbuild raw 217,139 215,755 +1,384
binding-radix-esbuild gzip 70,298 69,893 +405
binding-radix-esbuild brotli 61,053 60,813 +240
binding-mantine-hooks-vite gzip 28,398 28,396 +2
binding-apollo-client-vite raw 342,242 340,859 +1,383
binding-apollo-client-vite gzip 105,073 104,741 +332
binding-apollo-client-vite brotli 91,665 91,544 +121
binding-apollo-client-esbuild raw 365,458 364,070 +1,388
binding-apollo-client-esbuild gzip 116,087 115,728 +359
binding-apollo-client-esbuild brotli 101,330 101,084 +246

No changes across 111 measured values.

The compiler AST emit audit rejects hand-built node objects. A logical-chain
operand is always a read position, so pass no parent there instead of a
stand-in LogicalExpression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit 1e74902 into main Oct 8, 2026
28 of 31 checks passed
@trueadm
trueadm deleted the fix/vite-dev-profile-guards branch October 8, 2026 14:47
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