Parallelize session discovery and file-processing loops#533
Merged
Conversation
Discovery (sessionDiscovery.ts): - Replace sequential VS Code path-existence checks with Promise.all - Replace nested sequential workspace/globalStorage scan loops with Promise.all per variant and per workspace dir - Replace sequential CLI subdir events.jsonl stat checks with Promise.all - Replace sequential Crush project DB queries with Promise.all Stats calculation (extension.ts + cli/src/helpers.ts): - Add runWithConcurrency<R>(files, fn, limit=20) helper to both files -- worker-pool pattern, no new dependencies - Parallelize calculateTokenUsage, calculateDetailedStats, calculateDailyStats, and calculateUsageAnalysis loops in extension.ts - Parallelize calculateDetailedStats loop in cli/src/helpers.ts - Two-phase approach: gather stat+cache data in parallel, aggregate results synchronously (safe in Node.js single-threaded model) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…o reduce redundant file reads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
With many supported IDEs (VS Code, Cursor, VSCodium, OpenCode, Crush, Continue, Visual Studio, Claude Code) and potentially hundreds of session files, the initial load and every 5-minute refresh was bottlenecked by fully sequential I/O: every path-existence check, every directory scan, and every file stat+parse waited for the one before it. On machines with many workspaces this added seconds of unnecessary wall-clock time.
What changed
Discovery phase (
vscode-extension/src/sessionDiscovery.ts)All four sequential discovery loops are now parallel:
pathExists()calls for editor variants (stable, insiders, VSCodium, Cursor, remote paths) now run as a singlePromise.all()instead of one-at-a-time.forloop).events.jsonlstat checks across all UUID subdirs now run as a singlePromise.all().discoverSessionsInDb()call runs concurrently across all known projects.Loading phase (
extension.ts+cli/src/helpers.ts)Added a small
runWithConcurrency<R>(files, fn, limit = 20)worker-pool helper (no new dependencies) that processes up to 20 files at a time. Four sequential loops are replaced:calculateTokenUsagecalculateDetailedStatscalculateDailyStatscalculateUsageAnalysiscalculateDetailedStatsThe two-phase approach (parallel gather → sequential aggregate) keeps all Map/counter mutations on the single JS thread — no races, no locks needed.
Trade-offs
Promise.all()because the counts are small (≤10 paths, ≤N workspace dirs per path) and each operation is a cheap syscall.