Skip to content

perf: fan out the discovery sweep instead of walking it one syscall at a time - #1105

Merged
iamtoruk merged 1 commit into
mainfrom
perf/1104-discovery-sweep
Aug 23, 2026
Merged

iamtoruk merged 1 commit into
mainfrom
perf/1104-discovery-sweep

Conversation

@iamtoruk

@iamtoruk iamtoruk commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #1104 (partially — see honest scope note at the end).

What

The discovery sweep was syscall-serial: one readdir/stat at a time across ~21k files, leaving the OS idle between calls. This PR overlaps the metadata syscalls (bounded, concurrency 32) while keeping every order-sensitive reconcile loop serial and in discovery order.

  • fs-utils: mapWithConcurrency — order-preserving bounded pool
  • providers/index: providers discover concurrently, concatenated in registry order
  • kimicode/codex/grok/claude: nested serial walks → level-by-level fan-out (kimicode was the single biggest term)
  • parser.ts scanProjectDirs/parseProviderSources: walk+fingerprint fan out, reconcile stays serial (changedFiles order drives worker-result pairing and seenMsgIds pre-seeding)
  • collectJsonlFiles: typed readdir; plain files no longer cost a wasted subagents/ probe (isFile() deliberately, so symlinked dirs are still probed — test covers it)
  • codex-cache: latent bug exposed by the fan-out — loadCache memoized only after resolve, so 32 concurrent callers each re-read + re-parsed the 59MB results file (first attempt regressed 6.3s→11.0s). Fixed by sharing the in-flight promise.

Never-lose

No change signal was touched: fingerprints are still (dev, ino, mtimeMs, size) from one stat, reconcileFile unchanged. No early-exit removed, no writes parallelized, no cache version bump, no payload shape moved. Discovery order preserved everywhere via index-addressed results.

Parity

APFS-cloned all provider roots into a frozen HOME + isolated cache; ran main vs branch:

  • Warm: today, report -p month, models — identical except /generated (run's own timestamp)
  • Cold (empty cache both sides): report -p all, models, sessions, per-provider report -p all --provider {codex,kimicode,grok,claude} — all identical except /generated

Perf (21k files / 9 providers)

main branch
warm today (isolated, median of 5) 5.30s 2.99s (1.77x)
cold (alternating A/B) 34.7s 31.2s (1.11x)
warm, real env (menubar + 2 serve children) ~8-9s ~5.6s

sys time −31%. Interleaved A/B runs confirm the gap is the build, not drift.

Tests

tests/discovery-concurrency.test.ts (7) + tests/codex-cache-concurrent-load.test.ts (1, verified non-vacuous). Full suite: 231 files / 3177 tests green.

Scope note

This is 1.77x, not the 'low hundreds of ms' prize. Post-change profile: the sweep is now 570ms/pass but renderDashboard runs the whole pipeline three times (three range-distinct parseAllSessions, no shared memo), and the non-sweep floor (correlateCrossProviderPrSessions 270ms/pass, loadCache 190ms/pass, parse/summarize) × 3 ≈ 2.6s that no discovery work can touch. Follow-up issue incoming for the 3× pipeline collapse — that's the next real lever.

…t a time

Every dated command re-walks and re-stats every provider tree before it can
decide what the cache already covers. That sweep was strictly serial at every
level -- one readdir, one stat, one state.json read at a time, and one provider
after another -- so on a 21k-file / 9-provider corpus it owned most of a warm
run's wall clock with the machine idle waiting on the kernel.

Measured per warm `today` pass on that corpus before this change: 859 ms in
discoverAllSessions (kimicode 467, codex 143, claude 135, grok 77) and 727 ms in
the Claude project walk + fingerprint pass.

- fs-utils: mapWithConcurrency + FS_SCAN_CONCURRENCY, one bounded, order-
  preserving helper for the whole sweep.
- providers/index: run provider discovery concurrently, concatenated in
  registry order.
- claude/codex/grok/kimicode: walk each level with the level fanned out,
  re-concatenated in readdir order before anything reconciles.
- parser: the Claude dir walk and both fingerprint passes (scanProjectDirs and
  parseProviderSources) fan out, then reconcile serially in discovery order,
  which is what changedFiles ordering and the seenMsgIds pre-seed depend on.
- parser: collectJsonlFiles reads entries with their types, so a plain file no
  longer costs a wasted subagents/ probe.
- codex-cache: share one in-flight load between concurrent readers. The memo is
  only populated after the read + parse resolves, so concurrent discovery had
  every caller re-reading and re-parsing the same (here 59 MB) file.

No reconciliation logic changed: the same fingerprints reach the same cache
comparison in the same order. Warm `today` 5.30s -> 2.99s, cold 34.7s -> 31.1s
(medians of 5 / 2, isolated HOME + cache). today/report -p month/models/sessions
JSON, warm and cold, per-provider and combined, are byte-identical apart from
the run's own `generated` timestamp.

Closes #1104
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.

perf: the discovery sweep (~6.4s stat/fingerprint pass over the full tree) dominates every dated command

1 participant