Files
Kazuki Yamada 67e1d7d8a5 perf(core): Overlap metrics warm-up and output module load with search
Reorder pack() startup so two pre-existing latencies stop sitting on the
critical path between searchFiles and produceOutput:

1. Pre-init the metrics worker pool BEFORE searchFiles. Each metrics
   worker pays ~200-285ms to parse gpt-tokenizer's BPE table; we already
   ran this in parallel with the post-search pipeline, but the search
   phase itself (~100-150ms) was outside the overlap window. Pool sizing
   no longer depends on the file count, capped at METRICS_PREWARM_THREAD_CAP
   (=3) — enough to absorb the typical metrics workload while small repos
   stop over-spawning workers they will never use.

2. Lazy-load `produceOutput` via the dynamic-import deps pattern already
   used for `packSkill`. The output-style module chain (Handlebars compile
   + style modules) now resolves in parallel with searchFiles/collect
   instead of blocking packager.ts entry.

3. Drop the `await metricsWarmupPromise` and `await tokenCacheLoadPromise`
   barriers before `produceOutput`. Neither produceOutput nor file
   collection depend on those promises; the awaits only need to fire
   before `calculateMetrics` actually dispatches tasks, so they move
   inside the metrics branch of the trailing Promise.all.

The metrics taskRunner construction moves OUTSIDE the try block so a
synchronous throw never escapes without cleanup; the surrounding try
expands upward to wrap searchFiles + sort/regroup so a search failure
still triggers `metricsTaskRunner.cleanup()`. A new packager.test
asserts this leak path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 19:43:53 +09:00
..