Skip to content

perf(esm,polyfill): cut Windows interactive startup by ~68% (#20) - #21

Closed
BeatSeat wants to merge 2 commits into
CometixSpace:masterfrom
BeatSeat:perf/windows-startup-optimizations
Closed

BeatSeat wants to merge 2 commits into
CometixSpace:masterfrom
BeatSeat:perf/windows-startup-optimizations

Conversation

@BeatSeat

@BeatSeat BeatSeat commented Nov 9, 2026

Copy link
Copy Markdown

Closes #20

Summary

Following the 5-stage root-cause analysis in #20 on @cometix/claude-code@2.1.302 (win32-x64, 1,707 split-ESM chunks), this PR implements build-time AST rewrites and polyfill-layer optimizations that cut Windows interactive startup time from ~3.43s to ~1.09s (-68.2%, 3.14x faster) while keeping the upstream fetch/patch/release pipeline 100% automated.


Key Optimizations (Patch & Polyfill Layer Only)

1. ESM Module Graph & Compile Cache (Stage 1 / Stage 2 / Stage 5)

  • Redundant Bare-Import Pruning (pruneRedundantBareImports in scripts/esm-chunk-patch.mjs):
    • Uses Tarjan's SCC condensation + bottom-up reachability closure to prune bare import "./chunk-xxx.js"; statements that are already transitively imported by earlier sibling imports in the same module (or that point to comment-only empty chunks), while preserving exact DFS post-order evaluation.
    • On 2.1.302, this removes 171,147 of 179,412 bare imports across 842 chunks (-95.4% bare imports / -84.6% total static import edges).
  • Automatic V8 Bytecode Compile Cache (templates/bun-polyfill.js, ESM_PRELUDE):
    • Invokes module.enableCompileCache?.() automatically at startup so dynamically imported chunks (chunk-14fc6qh5.js, AppRoot, REPL, etc.) use V8 compile cache out of the box without requiring users to set NODE_COMPILE_CACHE.
  • Emit "type": "module" in Split-ESM Platform package.json (scripts/build-platform-package.mjs, scripts/fetch-and-process.mjs):
    • Eliminates Node's [MODULE_TYPELESS_PACKAGE_JSON] warning and CJS-to-ESM reparsing overhead on every .js entry.
  • Activate Upstream Lazy Environment Proxy (CLAUDE_CODE_LAZY_ENV_GETTERS ??= "1") & Lazy-load bun-ink-compat.cjs:
    • Activates upstream's built-in Proxy in N5 (chunk-8dm2bvpb.js), skipping ~500 eager Object.defineProperty calls at module evaluation time.

2. Stage 3 Pre-Action Bottlenecks

  • Zero-Subprocess where.exe / Bun.which Resolution (rewriteWindowsWhere, P5, P8, __ccFindExecutableCandidates):
    • Replaces synchronous where.exe / which subprocess spawns with pure-JS PATH + PATHEXT resolution (__ccFindExecutableCandidates and memoized Bun.which), preserving upstream's cwd shadow detection (pQn(r, a)), .com/.exe/.bat/.cmd extension filter (b(r)), and n.remember cache.
  • Persistent SHA-256 Cache for System CA X509Certificate.validTo (rewriteSystemCaCertCache, __ccGetCertValidTo):
    • Caches parsed validTo timestamps in os.tmpdir()/claude-code-ca-validto-v1.json so Ln() in chunk-ad5hwaq2.js does not instantiate 500+ new X509Certificate(pem) objects synchronously on every startup.
  • Strip No-Op using Wh Wrappers (rewriteNoopSlowOpUsing):
    • Removes 27 no-op using <id> = Wh\...`;declarations aroundJSON.parse, JSON.stringify, statSync, existsSync, readFileSync`, etc.

3. Stage 4 & Stage 5 Critical-Path Overlapping

  • Background Pre-Warming of Windows Self Process Creation Time (rewriteWindowsProcessCreationTime, __ccGetSelfProcStart):
    • Spawns an .unref() background powershell.exe -NoProfile -Command "(Get-Process -Id <pid>).StartTime.Ticks" job during Stage 1 (bun-polyfill.js) so it runs in parallel with Stage 2/3 module loading.
    • Rewrites Win32_Process ... .CreationDate queries in chunk-m1nq7hxb.js to (Get-Process -Id ...).StartTime and adds a 1ms (1e4 ticks) tolerance in GEe/F to account for WMI's microsecond digit truncation.
  • Preload & Parallelize AppRoot + REPL Imports (rewriteParallelReplImports, __ccPreloadRepl):
    • Converts sequential await import("./chunk-pxd59j4h.js"); await import("./chunk-z2sjg6vz.js") in h5s/kYr into Promise.all and triggers __ccPreloadRepl() at the start of interactive setup() (Zi), overlapping the 350 UI chunk loads with Stage 4 setup.
  • Defer Stale UDS .tmp Sweep (rewriteUdsSweep):
    • Defers await Ee(c, i) in o2s (chunk-we66nf8b.js) off the critical startup path via setTimeout(..., 2000).unref?.().

Measured Benchmark Results on Windows (win32 x64, node.exe v25.2.1, Native NTFS)

Tested directly on Windows against the unpacked 2.1.302 win32-x64 package (3-run averages):

Stage Baseline (2.1.302) Patched (Cold Cache) Patched (Warm Cache) Delta / Speedup
Stage 1: bun-polyfill.mjs 19.7 ms 34.8 ms 32.4 ms +12.7 ms (starts background self-PID ticks pre-warm)
Stage 2: Main Module Graph (chunk-14fc6qh5.js, 805 chunks) 1246.3 ms 899.3 ms 707.2 ms -539.1 ms (-43.3%)
Stage 3: Pre-Action (Jx CA certs + $Br × 5 tool lookups) 513.1 ms 96.0 ms 72.2 ms -440.9 ms (-85.9%, 7.1x)
├─ Stage 3a: System CA store filter (Ln, 541 certs) 67.5 ms 63.2 ms 45.7 ms -21.8 ms (-32.3%)
└─ Stage 3b: Executable lookup ($Br × 5: git/rg/powershell/node/cmd) 445.6 ms 32.8 ms 26.6 ms -419.0 ms (-94.0%, 16.8x)
Stage 4: setup() (await O_() self process start + REPL preload) 1114.9 ms 434.6 ms 278.9 ms -836.0 ms (-75.0%, 4.0x)
└─ Stage 4a: Windows self process creation time (await O_()) 1114.7 ms 35.3 ms 30.6 ms -1084.1 ms (-97.3%, 36.4x)
Stage 5: AppRoot + REPL dynamic imports (350 chunks) 536.1 ms 0.0 ms 0.0 ms -536.1 ms (-100%, preloaded during Stage 4)
Total Interactive Startup (totalInteractiveMs) 3430.1 ms 1464.8 ms 1090.7 ms -2339.4 ms (-68.2%, 3.14x faster)
claude --help (end-to-end wall-clock) 1431.2 ms 2292.4 ms (1st cache write) 841.7 ms -589.5 ms (-41.2%)

Verification

  • Added 10 unit & integration tests in test/startup-perf-patches.test.mjs covering all new AST rewrites, pruneRedundantBareImports DFS post-order equivalence, __ccFindExecutableCandidates, Bun.which, and __ccGetCertValidTo.
  • Full test suite passes (110 / 110 tests passing via npm test).

…pace#20)

Interactive startup of the split-ESM build on Windows (2.1.302, 1,707 chunks)
spends ~3.4s across five stages: linking 179k redundant bare import edges,
re-parsing typeless .js files without compile cache, spawning where.exe per
tool lookup, validating 500+ system CA certificates with new X509Certificate,
blocking on a synchronous PowerShell WMI Win32_Process query for process.pid,
and loading the 350 AppRoot/REPL chunks sequentially after setup().

All fixes live in the polyfill and build-time patchers so CI and upstream
updates remain automatic:

1. Prune redundant bare imports (pruneRedundantBareImports): condense the
   chunk graph with Tarjan SCC and drop bare import "./chunk-x.js" statements
   already reachable through earlier imports in the same module (or pointing
   to comment-only chunks). On 2.1.302 this removes 171,147 of 179,412 bare
   imports (-95.4% bare / -84.6% total edges) with zero DFS post-order drift.
2. Enable Node V8 compile cache automatically (module.enableCompileCache in
   bun-polyfill.js and ESM_PRELUDE), set CLAUDE_CODE_LAZY_ENV_GETTERS=1 by
   default, lazy-load bun-ink-compat.cjs, and emit "type": "module" in split
   platform package.json to eliminate MODULE_TYPELESS_PACKAGE_JSON reparsing.
3. Replace where.exe and which/where subprocesses with pure-JS PATH/PATHEXT
   resolution (__ccFindExecutableCandidates and memoized Bun.which) in
   rewriteWindowsWhere, P5, and P8.
4. Pre-warm process.pid StartTime.Ticks in the background at polyfill load on
   Windows and rewrite Win32_Process.CreationDate WMI queries to Get-Process
   StartTime with a 1ms (1e4 ticks) WMI rounding tolerance in GEe/F.
5. Cache X509Certificate validTo timestamps by SHA-256 on disk in
   rewriteSystemCaCertCache, defer the UDS .tmp sweep off the critical path in
   rewriteUdsSweep, strip 27 no-op using Wh`...` wrappers in
   rewriteNoopSlowOpUsing, and overlap AppRoot/REPL dynamic imports with
   setup() via __ccPreloadRepl in rewriteParallelReplImports.

Closes CometixSpace#20
@Huan-zhaojun

Copy link
Copy Markdown

Independent verification on different hardware

I built this branch locally (2.1.302, win32-x64, from the PR head) and measured it against the stock package with the ConPTY first-frame probe — no instrumentation on the absolute numbers, control batches at both ends, alternating paired runs to cancel machine drift. Slower hardware than the PR's (Win 11 26100, i7-12700H, NVMe, Node v24.22.1).

Build fidelity: the patch log reports 171,147 pruned bare imports — exactly the number in the PR description.

First-frame (ConPTY):

config n min median max
stock 2.1.302 (batch-start control) 8 5472 5565 6218
stock (batch-end control, warm drift) 6 5769 6080 6617
this PR, first-ever run (cold caches) 1 — 4353 —
this PR, warm 8 3285 3462 3695

Alternating paired runs (stock → PR, ×5): per-pair gains of 1770 / 2413 / 2576 / 2159 / 2703 ms — five for five. On this machine: first-frame 5.6–6.1 s → 3.5 s (−38…−43 %). The −68 % in the PR body is different hardware and a different endpoint (totalInteractiveMs excludes node boot, entry loading and first-frame drawing that the ConPTY probe includes). Cold start (4.35 s) beats the stock warm median by over a second.

Stage decomposition (PROFILE_STARTUP, median of 4 rounds each, sums of adjacent-mark deltas):

stage stock this PR Δ
boot + entry 275 238 −37
main import graph 1904 1114 −790
init 708 411 −297
setup (UDS, tools, MCP configs) 1455 1193 −262
REPL dynamic wave 1335 480 −855
first-frame window 1209 1466 +257
sum 6885 4902 −1983

Wall-clock medians of these profiled batches: 5987 ms vs 3718 ms.

The four growing windows (setup_uds_start +366, action_after_setup +163, action_commands_loaded +95, run_after_parse +257, ≈ +880 combined) are where the preloaded AppRoot/REPL chunk loading now lands — it overlaps the UDS wait instead of running after it, which is also what empties uds_inbox_key_published (644 → 95, the prewarmed Get-Process). The net of all of it is the first-frame table above. mcp_configs_loaded is essentially unchanged (213 → 176), consistent with the PR's scope.

Full sub-stage medians (marks ≥10 ms either side; ~0-delta marks omitted)
mark (delta to previous) stock this PR
cli_before_main_import 275 238
main_tsx_entry 1904 1114
init_safe_env_vars_applied 45 45
init_after_1p_event_logging 18 2
init_after_remote_settings_check 99 32
init_network_configured 123 108
preAction_after_remote_settings 110 66
preAction_after_plugin_early_kick 191 78
action_after_input_prompt 141 82
setup_uds_start 144 510
setup_uds_imported 152 49
uds_inbox_key_published 644 95
setup_before_prefetch 31 7
setup_after_prefetch 89 6
action_after_setup 42 205
action_tools_loaded 108 18
action_commands_loaded 32 127
action_mcp_configs_loaded 213 176
telemetry_init_start 1335 480
run_after_parse 1209 1466

Raw per-round data available on request.

@Huan-zhaojun

Copy link
Copy Markdown

Update — re-verified on the new head a307911 (Nov 10)

The PR gained a second commit (lazy Vet engine init, microtask env cache, system CA disk cache, full UI preload). I rebuilt from the new head and repeated the same protocol (ConPTY first-frame probe, alternating paired runs, control batches at both ends; same machine).

Build fidelity: 171,147 pruned bare imports again; all new markers present (__ccEnvGen in chunk-8dm2bvpb.js, __ccLazy_*, __ccFableRe, system-CA disk cache). The repo's full test suite passes locally on Windows (112/112).

First-frame (ConPTY):

config n min median max
stock 2.1.302 (batch-start control, today) 8 5286 5557 6161
stock (batch-end control, today) 6 5181 5203 5477
first commit 2803a3f, warm (Nov 9, same machine) 8 3285 3462 3695
first commit, cold run (Nov 9) 1 — 4353 —
new head a307911, first-ever run (cold caches) 1 — 4370 —
new head a307911, warm 8 2992 3251 3389

The Nov-9 numbers come from yesterday's run of the same protocol on this machine; the stock baseline was stable across the two days (batch-start medians 5565 → 5557), so the cross-day comparison holds. vs the first commit: warm median 3462 → 3251, another −211 ms (−6%). Alternating paired runs today (stock → PR, ×5): +2416 / +2153 / +2244 / +2167 / +2276 ms — five for five, median +2244 (yesterday: five for five, median +2159). Cold start essentially unchanged (4353 → 4370).

Stage decomposition (PROFILE_STARTUP, median of 4 rounds each; v1 column from yesterday's run):

stage stock (today) v1 2803a3f v2 a307911 v2 − v1
boot + entry 265 238 6 −232
main import graph 1642 1114 1148 +34
init 630 411 339 −72
setup (UDS, tools, MCP) 1333 1193 1038 −155
REPL dynamic wave 1198 480 1010 +530
first-frame window 1160 1466 784 −682
sum 6227 4902 4324 −578

(Profiled rounds have day-to-day wall-clock drift — the stock sum was 6885 on Nov 9 — so the cross-day relative drop is the robust figure: −28.8% for v1, −30.6% for v2. The paired no-instrumentation numbers above are unaffected.)

The full-UI-preload commit shifts work earlier again: the first-frame window drops 1466 → 784 while the REPL wave grows 480 → 1010 (net −152 across the two), uds_inbox_key_published stays prewarmed (594 → 96), and the boot+entry segment nearly vanishes (238 → 6). One caveat on those two medians: per round they trade off against each other — the new preload races the module graph, and where the 350 UI chunks land varies by round (REPL-wave/first-frame pairs of 29/1464, 1581/160, 439/1403, 1791/164 ms; v1's pairs were stable at ~390–530/~1090–1900). The combined window is the robust figure, and it is both lower and tighter on the new head (1493–1955 ms across rounds vs 1528–2427 on the first commit) — the median split between the two stages is a scheduling lottery, not a stable stage shift.

Full sub-stage medians (three-way: stock today / v1 Nov 9 / v2 today)
mark (delta to previous) stock v1 v2
cli_before_main_import 265 238 6
main_tsx_entry 1642 1114 1148
init_safe_env_vars_applied 43 45 39
init_after_1p_event_logging 16 2 1
init_after_remote_settings_check 83 32 28
init_network_configured 109 108 71
preAction_after_remote_settings 94 66 57
preAction_after_plugin_early_kick 171 78 72
action_after_input_prompt 130 82 72
setup_uds_start 128 510 436
setup_uds_imported 127 49 43
uds_inbox_key_published 594 95 96
setup_before_prefetch 30 7 7
setup_after_prefetch 83 6 7
action_after_setup 41 205 178
action_tools_loaded 105 18 14
action_commands_loaded 27 127 104
action_mcp_configs_loaded 198 176 153
telemetry_init_start (REPL wave) 1198 480 1010
run_after_parse (first-frame) 1160 1466 784

Functional spot-checks: the two new CA cache files (claude-code-system-ca-v1.json, ~296 KB) are written once on the first cold run and reused thereafter; the /model picker renders the full configured model list correctly — lazy Vet init and the model gate shortcut don't break it.

Note: the PR description's benchmark table still reflects the first commit only — worth refreshing with a307911 numbers.

@BeatSeat

Copy link
Copy Markdown
Author

Closing in favor of the upstream #20 landing on master:

  • 3b12334 (module.registerHooks direct chunk resolver + user-scoped module.enableCompileCache) addresses the ESM module graph & bytecode cache bottleneck cleanly without rewriting the chunk AST graph.
  • a29e267 (cometix-native supplying Bun.ant.windowsProcessCreationTime) resolves the Windows WMI Win32_Process startup cost via a native N-API call instead of a background PowerShell warm-up or minified AST rewrite.

If any remaining orthogonal ideas (such as pure-JS Bun.which to avoid spawning where.exe, or tls.getCACertificates("system") caching) are still useful on top of master, they can be opened as small standalone PRs cleanly.

@BeatSeat BeatSeat closed this Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows 启动耗时 5-6s:多阶段实测分解与归因

2 participants