Repository navigation
Conversation
…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
|
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):
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 ( Stage decomposition (PROFILE_STARTUP, median of 4 rounds each, sums of adjacent-mark deltas):
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 Full sub-stage medians (marks ≥10 ms either side; ~0-delta marks omitted)
Raw per-round data available on request. |
…isk cache, and full UI preload
|
Update — re-verified on the new head 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 ( First-frame (ConPTY):
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):
(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), Full sub-stage medians (three-way: stock today / v1 Nov 9 / v2 today)
Functional spot-checks: the two new CA cache files ( Note: the PR description's benchmark table still reflects the first commit only — worth refreshing with |
|
Closing in favor of the upstream
If any remaining orthogonal ideas (such as pure-JS |
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)
pruneRedundantBareImportsinscripts/esm-chunk-patch.mjs):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.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).templates/bun-polyfill.js,ESM_PRELUDE):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 setNODE_COMPILE_CACHE."type": "module"in Split-ESM Platformpackage.json(scripts/build-platform-package.mjs,scripts/fetch-and-process.mjs):[MODULE_TYPELESS_PACKAGE_JSON]warning and CJS-to-ESM reparsing overhead on every.jsentry.CLAUDE_CODE_LAZY_ENV_GETTERS ??= "1") & Lazy-loadbun-ink-compat.cjs:ProxyinN5(chunk-8dm2bvpb.js), skipping ~500 eagerObject.definePropertycalls at module evaluation time.2. Stage 3 Pre-Action Bottlenecks
where.exe/Bun.whichResolution (rewriteWindowsWhere,P5,P8,__ccFindExecutableCandidates):where.exe/whichsubprocess spawns with pure-JSPATH+PATHEXTresolution (__ccFindExecutableCandidatesand memoizedBun.which), preserving upstream'scwdshadow detection (pQn(r, a)),.com/.exe/.bat/.cmdextension filter (b(r)), andn.remembercache.X509Certificate.validTo(rewriteSystemCaCertCache,__ccGetCertValidTo):validTotimestamps inos.tmpdir()/claude-code-ca-validto-v1.jsonsoLn()inchunk-ad5hwaq2.jsdoes not instantiate 500+new X509Certificate(pem)objects synchronously on every startup.using WhWrappers (rewriteNoopSlowOpUsing):using <id> = Wh\...`;declarations aroundJSON.parse,JSON.stringify,statSync,existsSync,readFileSync`, etc.3. Stage 4 & Stage 5 Critical-Path Overlapping
rewriteWindowsProcessCreationTime,__ccGetSelfProcStart):.unref()backgroundpowershell.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.Win32_Process ... .CreationDatequeries inchunk-m1nq7hxb.jsto(Get-Process -Id ...).StartTimeand adds a1ms(1e4ticks) tolerance inGEe/Fto account for WMI's microsecond digit truncation.AppRoot+REPLImports (rewriteParallelReplImports,__ccPreloadRepl):await import("./chunk-pxd59j4h.js"); await import("./chunk-z2sjg6vz.js")inh5s/kYrintoPromise.alland triggers__ccPreloadRepl()at the start of interactivesetup()(Zi), overlapping the 350 UI chunk loads with Stage 4 setup..tmpSweep (rewriteUdsSweep):await Ee(c, i)ino2s(chunk-we66nf8b.js) off the critical startup path viasetTimeout(..., 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.302win32-x64package (3-run averages):2.1.302)bun-polyfill.mjs19.7 ms34.8 ms32.4 ms+12.7 ms(starts background self-PID ticks pre-warm)chunk-14fc6qh5.js, 805 chunks)1246.3 ms899.3 ms707.2 msJxCA certs +$Br× 5 tool lookups)513.1 ms96.0 ms72.2 msLn, 541 certs)67.5 ms63.2 ms45.7 ms$Br× 5:git/rg/powershell/node/cmd)445.6 ms32.8 ms26.6 mssetup()(await O_()self process start + REPL preload)1114.9 ms434.6 ms278.9 msawait O_())1114.7 ms35.3 ms30.6 msAppRoot+REPLdynamic imports (350 chunks)536.1 ms0.0 ms0.0 mstotalInteractiveMs)3430.1 ms1464.8 ms1090.7 msclaude --help(end-to-end wall-clock)1431.2 ms2292.4 ms(1st cache write)841.7 msVerification
test/startup-perf-patches.test.mjscovering all new AST rewrites,pruneRedundantBareImportsDFS post-order equivalence,__ccFindExecutableCandidates,Bun.which, and__ccGetCertValidTo.110 / 110tests passing vianpm test).