-
Notifications
You must be signed in to change notification settings - Fork 0
Worker Pool
Arham-Qureshi edited this page Jul 21, 2026
·
1 revision
Parsing is CPU-bound (tree-sitter compiles native C++ parsers). To maximize throughput, codebase-vis spawns a pool of child processes via fork() to parse files in parallel.
WorkerPool(size = CPU count - 1)
│
├── worker[0] (fork → parse-worker.js)
├── worker[1] (fork → parse-worker.js)
├── ...
└── worker[n] (fork → parse-worker.js)
The WorkerPool class manages a pool of forked child processes:
class WorkerPool {
#size // Number of workers
#free // Array of idle worker processes
#queue // Array of pending tasks
#pending // Map of active worker → { resolve, reject }
}-
Initialization —
new WorkerPool(size)spawnsnworkers, each runningparse-worker.js -
Task submission —
pool.run(file)enqueues the file path and returns a Promise -
Draining —
#drain()assigns queued tasks to free workers viaworker.send(file) -
Processing — Each worker loads its own tree-sitter parsers, parses the file, and sends results back via
process.send() -
Collection — The main process captures results at the correct index (
results[i] = result) to preserve input order regardless of completion order -
Termination —
pool.terminate()sends SIGTERM to all workers and clears state
Main Process Worker Process
│ │
├── worker.send(file) ────────►│
│ ├── parseFile(file)
│ │ (loads own parsers)
│ ├── process.send({ id, dependencies, entities })
│◄─────────────────────────────┤
│ │
├── (capture result at index) │
├── #drain() ─── next task ───►│
If a worker exits with non-zero code or throws an error:
- The pool rejects the pending promise for that worker's current task
- The failed worker is removed from
#free - A replacement worker is spawned via
#addWorker() - Any unprocessed tasks in the queue are re-dispatched
This prevents a single corrupted file from stalling the entire parse.
| Setting | Default | Description |
|---|---|---|
| Pool size | CPU count - 1 |
e.g., 7 workers on an 8-core machine |
--jobs flag |
CPU - 1 | CLI override for worker count |
Each worker is a self-contained Node.js process that:
- Mirrors the
GRAMMAR_MAPfromparser/index.jsto know which parsers to load - Maintains its own
parserCacheper-process (tree-sitter grammars are cached per-process) - Listens for
process.on('message')— receives file paths from the parent - Parses the file and sends the result back via
process.send() - Handles errors gracefully by returning
{ id, error: true }instead of crashing