perf(index): faster fresh indexing + parallel reference resolution, byte-identical graphs (#1305)
* perf(index): ~34% faster fresh indexing, byte-identical graphs Profiling a fresh init on a medium TS repo (excalidraw, 657 files) showed the main thread as the critical path: per-row SQLite statement calls, repeated import-resolution walks, and per-row FTS trigger firings, with the parse workers ~75% idle behind it. This lands the semantics-preserving tranche of fixes: - Multi-row batched INSERTs (nodes/edges/unresolved refs/name segments) behind cached per-batch-size prepared statements; row order preserved, so rowid-based resolution determinism (#1015) is unchanged. - storeFileBundle: one transaction per file instead of four; nested transaction() calls now flatten (BEGIN-in-BEGIN previously threw, so no caller depended on nested rollback). - Dedicated store-writer thread for the fresh-DB bulk path (bundles applied in file order on a single writer connection; main thread does no DB work during the parse loop). Kill switch: CODEGRAPH_NO_STORE_WORKER=1. - Bulk FTS mode: drop the nodes_fts sync triggers during the bulk load, rebuild once at the end; crash inside the window self-heals on the next open. - Per-context memos for resolveImportPath/findExportedSymbol + a per-file exported-symbol index, invalidated exactly where clearCaches() already resets the resolver's own caches. - Fast-init on completely fresh DBs (journal in memory, no fsync until the index completes; interrupted init re-runs from scratch). Kill switch: CODEGRAPH_NO_FAST_INIT=1. - MaybeYield returns undefined on the not-due path so per-ref yield checks stop paying a promise + microtask hop each. - Parse pool prewarm for bulk indexing; compile-cache enabled at CLI and worker entry points. Excalidraw fresh init: 5.11s -> 3.36s median (n=5, warm cache, M-series). Graph dumps byte-identical across init, re-index, and sync paths; full suite green (2403 passed). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * perf(resolution): parallel reference resolution with canonical admission Fan resolution batches across a pool of read-only worker threads, each hosting a full ReferenceResolver over its own SQLite connection; results are admitted on the main thread in chunk order, so edge insertion order, row cleanup, failure parking, and deferred post-pass queues are exactly the sequence the single-threaded loop produces. Per-ref inputs match the baseline because the sequential path already resolves each batch against the state committed BEFORE that batch. Validated byte-identical on excalidraw (pool forced on) and apache/dubbo (4,048 Java files): dubbo full index 39s -> 19s (2.05x) with identical graph dumps (91,495 nodes / 223,953 edges). The pool only engages when total pending refs clear a threshold (default 150k, CODEGRAPH_PARALLEL_RESOLVE_MIN to tune, CODEGRAPH_NO_PARALLEL_RESOLVE=1 to disable): measured on a ~58k-ref repo the workers' boot CPU contends with resolution on the same cores and makes indexing slower, so small repos keep the sequential path. When fast-init left the DB in memory-journal mode, WAL is restored before resolution only when the pool will run (readers + rollback-journal writers don't mix). Also: sqlite adapter readOnly open support. TreeCursor spine rewrite of the body walker was built, measured neutral on real repos and equal in a 20k-child microbench (web-tree-sitter's namedChild(i) is not quadratic in this binding), and rejected — per-node JS<->WASM marshaling is the floor, which a traversal swap cannot remove. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
246aee8373
commit
5736e24bb6
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* ResolverPool — main-thread client for the parallel-resolution workers.
|
||||
*
|
||||
* resolveBatch() splits a rowid-ordered batch into ordered chunks, fans the
|
||||
* chunks across the pool, and reassembles the results IN CHUNK ORDER, so the
|
||||
* caller's admission (edge inserts, row cleanup, failure parking, deferred
|
||||
* post-pass queues) is byte-for-byte the sequence the single-threaded loop
|
||||
* would have produced. Any worker failure fails the batch — the caller falls
|
||||
* back to the sequential path. Kill switch: CODEGRAPH_NO_PARALLEL_RESOLVE=1.
|
||||
*/
|
||||
|
||||
import { Worker } from 'worker_threads';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import type { UnresolvedReference } from '../types';
|
||||
import type { ResolvedRef, UnresolvedRef } from './types';
|
||||
|
||||
export interface ChunkResult {
|
||||
resolved: ResolvedRef[];
|
||||
unresolved: UnresolvedRef[];
|
||||
deferredChain: UnresolvedRef[];
|
||||
deferredThisMember: UnresolvedRef[];
|
||||
byMethod: Record<string, number>;
|
||||
}
|
||||
|
||||
interface PoolWorker {
|
||||
worker: Worker;
|
||||
ready: Promise<void>;
|
||||
busy: number;
|
||||
}
|
||||
|
||||
const MIN_PARALLEL_BATCH = 1000;
|
||||
const CHUNK_SIZE = 500;
|
||||
|
||||
/**
|
||||
* Minimum TOTAL pending refs before the pool is created at all. Pool boot
|
||||
* (module load + readonly DB open + framework detect + cache warm, times N
|
||||
* workers) costs real CPU that CONTENDS with sequential resolution on the
|
||||
* same cores — measured on a medium repo (~40k refs, ~1.2s of resolution)
|
||||
* the pool made indexing slower. It pays off when resolution runs for tens
|
||||
* of seconds to minutes (large JVM/Spring-class repos). Override:
|
||||
* CODEGRAPH_PARALLEL_RESOLVE_MIN=<refs> (0 forces the pool on).
|
||||
*/
|
||||
export function minRefsForPool(): number {
|
||||
const raw = process.env.CODEGRAPH_PARALLEL_RESOLVE_MIN;
|
||||
if (raw !== undefined) {
|
||||
const parsed = Number.parseInt(raw, 10);
|
||||
if (Number.isFinite(parsed) && parsed >= 0) return parsed;
|
||||
}
|
||||
return 150_000;
|
||||
}
|
||||
|
||||
export class ResolverPool {
|
||||
private workers: PoolWorker[] = [];
|
||||
private nextId = 0;
|
||||
private waiters = new Map<number, { resolve: (r: ChunkResult) => void; reject: (e: Error) => void }>();
|
||||
private failed: Error | null = null;
|
||||
|
||||
/**
|
||||
* Create a pool when the compiled worker exists (absent when running from
|
||||
* source in tests → callers use the sequential path), the kill switch is
|
||||
* off, and the machine has cores to spare. Returns null otherwise.
|
||||
*/
|
||||
static tryCreate(dbPath: string, projectRoot: string): ResolverPool | null {
|
||||
if (process.env.CODEGRAPH_NO_PARALLEL_RESOLVE === '1') return null;
|
||||
const workerScript = path.join(__dirname, 'resolver-worker.js');
|
||||
if (!fs.existsSync(workerScript)) return null;
|
||||
const size = Math.max(1, Math.min(os.cpus().length - 2, 6));
|
||||
if (size < 2) return null;
|
||||
try {
|
||||
return new ResolverPool(workerScript, dbPath, projectRoot, size);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private constructor(workerScript: string, dbPath: string, projectRoot: string, size: number) {
|
||||
for (let i = 0; i < size; i++) {
|
||||
const worker = new Worker(workerScript);
|
||||
let readyResolve!: () => void;
|
||||
let readyReject!: (e: Error) => void;
|
||||
const ready = new Promise<void>((resolve, reject) => {
|
||||
readyResolve = resolve;
|
||||
readyReject = reject;
|
||||
});
|
||||
const pw: PoolWorker = { worker, ready, busy: 0 };
|
||||
worker.on('message', (msg: { type: string; id?: number; message?: string } & Partial<ChunkResult>) => {
|
||||
if (msg.type === 'ready') {
|
||||
readyResolve();
|
||||
} else if (msg.type === 'result' && msg.id !== undefined) {
|
||||
pw.busy--;
|
||||
const waiter = this.waiters.get(msg.id);
|
||||
this.waiters.delete(msg.id);
|
||||
waiter?.resolve({
|
||||
resolved: msg.resolved!,
|
||||
unresolved: msg.unresolved!,
|
||||
deferredChain: msg.deferredChain!,
|
||||
deferredThisMember: msg.deferredThisMember!,
|
||||
byMethod: msg.byMethod!,
|
||||
});
|
||||
} else if (msg.type === 'error') {
|
||||
pw.busy--;
|
||||
const err = new Error(`resolver worker: ${msg.message}`);
|
||||
if (msg.id !== undefined && this.waiters.has(msg.id)) {
|
||||
const waiter = this.waiters.get(msg.id)!;
|
||||
this.waiters.delete(msg.id);
|
||||
waiter.reject(err);
|
||||
} else {
|
||||
this.fail(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
worker.on('error', (err) => {
|
||||
this.fail(err instanceof Error ? err : new Error(String(err)));
|
||||
readyReject(this.failed!);
|
||||
});
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0) {
|
||||
this.fail(new Error(`resolver worker exited with code ${code}`));
|
||||
readyReject(this.failed!);
|
||||
}
|
||||
});
|
||||
worker.postMessage({ type: 'open', dbPath, projectRoot });
|
||||
this.workers.push(pw);
|
||||
}
|
||||
}
|
||||
|
||||
private fail(err: Error): void {
|
||||
if (!this.failed) this.failed = err;
|
||||
for (const [, waiter] of this.waiters) waiter.reject(this.failed);
|
||||
this.waiters.clear();
|
||||
}
|
||||
|
||||
/** Whether this batch is worth fanning out. */
|
||||
static worthParallel(batchLength: number): boolean {
|
||||
return batchLength >= MIN_PARALLEL_BATCH;
|
||||
}
|
||||
|
||||
async ready(): Promise<void> {
|
||||
await Promise.all(this.workers.map((w) => w.ready));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve `refs` across the pool. Chunks preserve input order; the returned
|
||||
* arrays are the in-order concatenation of the chunk results.
|
||||
*/
|
||||
async resolveBatch(refs: UnresolvedReference[]): Promise<ChunkResult> {
|
||||
if (this.failed) throw this.failed;
|
||||
const chunkPromises: Promise<ChunkResult>[] = [];
|
||||
for (let i = 0; i < refs.length; i += CHUNK_SIZE) {
|
||||
const chunk = refs.slice(i, i + CHUNK_SIZE);
|
||||
const id = this.nextId++;
|
||||
// Least-busy dispatch keeps workers evenly loaded regardless of chunk
|
||||
// cost variance; result order is fixed by the promise array, not by
|
||||
// completion order.
|
||||
const pw = this.workers.reduce((a, b) => (b.busy < a.busy ? b : a));
|
||||
pw.busy++;
|
||||
chunkPromises.push(
|
||||
new Promise<ChunkResult>((resolve, reject) => {
|
||||
this.waiters.set(id, { resolve, reject });
|
||||
pw.worker.postMessage({ type: 'resolve', id, refs: chunk });
|
||||
})
|
||||
);
|
||||
}
|
||||
const chunks = await Promise.all(chunkPromises);
|
||||
const out: ChunkResult = { resolved: [], unresolved: [], deferredChain: [], deferredThisMember: [], byMethod: {} };
|
||||
for (const c of chunks) {
|
||||
out.resolved.push(...c.resolved);
|
||||
out.unresolved.push(...c.unresolved);
|
||||
out.deferredChain.push(...c.deferredChain);
|
||||
out.deferredThisMember.push(...c.deferredThisMember);
|
||||
for (const [k, v] of Object.entries(c.byMethod)) out.byMethod[k] = (out.byMethod[k] || 0) + v;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
await Promise.all(
|
||||
this.workers.map(
|
||||
(pw) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const t = setTimeout(() => {
|
||||
void pw.worker.terminate().then(() => resolve());
|
||||
}, 5000);
|
||||
pw.worker.once('exit', () => {
|
||||
clearTimeout(t);
|
||||
resolve();
|
||||
});
|
||||
pw.worker.postMessage({ type: 'close' });
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user