Repository navigation
fix(octane): fold profiling guards out of the vite dev runtime - #1898
Conversation
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]>
Benchmark report
Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
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").
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").
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]>
Summary
vite devwith 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, soprofiling.tsanddevtools-hook.tsare never fetched or evaluated.octanepre-bundled by Vite's dependency optimizer (compiler plugin only, octane from npm) get the same result: the optimizer now receives the constant throughoptimizeDeps.rolldownOptions.transform.define.profile: true, ordevtools: trueresolving to'auto'undervite dev) and all production builds are unchanged.Follow-up to #1886 / #1894. The gap predates #1894 and affects every existing guard.
Why
Vite's
defineis static replacement only invite build. Invite dev, thevite:defineplugin skips client-consumer modules andvite/dist/client/env.mjsinstalls each entry as a global (globalThis.__OCTANE_PROFILE_ENABLED__ = false). Measured on main before this change, the dev-servedruntime.tshad 138 reads of the define across its guards (createBlock,renderBlockInner,renderBranchSlot,unmountBlockInner,drainQueue, setState paths, …), each a global read plustypeof, and it statically importedprofiling.tsanddevtools-hook.ts(about 41 KB of source). The dependency optimizer also ignoresdefine, so a pre-bundledoctanecarried 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'sthis.parse, oxc).if, conditional, or&&/||chain, and folds whole operand chains, sincea && typeof X !== 'undefined' && Xnests as(a && typeof …) && X.if (!block.disposed && guard)becomes{(!block.disposed);}, which is what a production build emits.sideEffectscontract is what lets production bundlers drop the same imports.slot-hooks.jsandruntime-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: areturn/throw/yieldargument, or a statement or arrow body start.packages/octane/src/compiler/vite.js:servewith profiling off, runs the fold on modules under this package'ssrc/ordist/that mention the define, before the existing compiler pipeline. Modules the compiler leaves alone still return the folded code.esbuildOptions.defineover because Vite copies it only whenrolldownOptions.transform.defineis unset.PROFILE_DEFINEnow comes fromprofile-guards.js..rulesync/rules/core-engineering.md(+ generated copies): listsprofile-guards.jsas a third narrow text-edit exception. Reprinting the 51k-line runtime through esrap on every dev start was the rejected alternative:@tsrx/coreparsesruntime.tsin 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:checkon every changed filepnpm typecheck:fileson the changed source and test filespnpm typecheck(not run locally; CI)pnpm test(not run locally per the shared-machine load rule; CI)packages/octane/tests/vite-dev-profile-guards.test.ts(new, 3 tests). Starts a real Vite dev server on a temp app.runtime.tsanduniversal-core.tshave no define and import neither profiling module. The app runs in a runnable client-consumer environment (click →1), the module graph never loadsprofiling.tsordevtools-hook.ts, and no__OCTANE_PROFILER__or__OCTANE_DEVTOOLS__global is installed.profilerrecordscomponent-renderevents andcomponent.render/commit.rootcounters, and both globals are installed.octane: no define read and no profiler with profiling off; profiling on is the control.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 thefalseglobalvite devinstalls, and as folded, and compares results. It covers ASI afterreturn, 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.hooks,effect-timing,suspense-preserves-dom,state-getter,devtools-hook,profiling,devtools-prod-strip, andprofiling-disabled(octaneproject, now running the folded runtime: 196 + 2 passing).profiling-runtimeanddevtools-runtime(octane-profile, 25 passing).vite-plugin-octanedevtools-optionandprofile-bundle(4 passing).src/index.tsandsrc/universal.ts(definefalse,minifySyntax+minifyWhitespace, tree-shaking, identifiers kept) are byte-identical with the authored and the foldedruntime.ts/universal-core.ts, under bothNODE_ENVvalues, 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 publisheddistJS shape: an oxc-transpiledruntime.tsfolds to 0 reads with no profiling import, reparses cleanly, and keeps its line count.vite.js.perf-review: see below.Perf review:
fix/vite-dev-profile-guards(origin/main..HEAD)Scan:
node scripts/perf-review-scan.mjsandnode scripts/perf-review-scan.mjs packages/octane/src/compiler/→ 0 candidates.Hot paths touched: none in shipped runtime source;
runtime.tsanduniversal-core.tsare untouched. The served dev runtime loses its profiling work:runtime.ts: 138 define reads → 0, 1,755,919 → 1,742,266 B;octane: 1,452,574 → 1,414,180 B with no profiler.foldServedProfileGuardsruntime.ts, about 25 + 5 ms foruniversal-core.ts. Memoized by exact source, one entry per module. Vite's own TS transform ofruntime.tsis about 30 ms.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 checksandbenchmark reportfail on main itself at 90a57d8. The overages (+~1.3 KB raw on 29 rows, including esbuild-built bundles that never loadvite.js) are identical on base and head ("No changes across 60/111 measured values"), so they are inherited, not caused here.compiler-ast-emit-audit, which rejects hand-built AST node objects: the fold used a stand-in{ type: 'LogicalExpression' }parent. Fixed in d37aa8c.if (false)blocks and 4 constant"boolean" !== "undefined"expressions, and bundlesprofiling.ts;devtools-hook.tsis already dropped. V8 skipsif (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/coreparsing per cold start, so it is not cheap. Left as a possible follow-up.resolve.preserveSymlinks: trueand 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
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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 leavesdefineas runtime globals in dev, so guards stayed on hot paths andprofiling.ts/devtools-hook.tsstill loaded.The Vite plugin now runs a new
profile-guards.jspass (documented as a third narrow text-edit exception alongsideslot-hooks.jsandruntime-requests.js) on Octanesrc/anddist/modules duringvite servewhen 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.definegets the same constant so pre-bundledoctanedrops 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.