* Resolve `refs` across the pool. Chunks preserve input order; the returned * arrays are the in-order concatenation of the chunk results.
(refs: UnresolvedReference[])
| 369 | * arrays are the in-order concatenation of the chunk results. |
| 370 | */ |
| 371 | async resolveBatch(refs: UnresolvedReference[]): Promise<ChunkResult> { |
| 372 | if (this.failed) throw this.failed; |
| 373 | const chunkPromises: Promise<ChunkResult>[] = []; |
| 374 | for (let i = 0; i < refs.length; i += CHUNK_SIZE) { |
| 375 | const chunk = refs.slice(i, i + CHUNK_SIZE); |
| 376 | const id = this.nextId++; |
| 377 | // Least-busy dispatch keeps workers evenly loaded regardless of chunk |
| 378 | // cost variance; result order is fixed by the promise array, not by |
| 379 | // completion order. A chunk that goes on with a file stays on the worker |
| 380 | // that has that file's lines, nodes and scans in its caches, unless that |
| 381 | // would leave it behind the others. References come file by file, so a |
| 382 | // file of 2,000 used to be read and scanned by four workers. |
| 383 | const least = this.workers.reduce((a, b) => (b.busy < a.busy ? b : a)); |
| 384 | const holder = this.fileWorkers.get(chunk[0]!.filePath); |
| 385 | const pw = holder && holder.busy <= least.busy + 1 ? holder : least; |
| 386 | this.fileWorkers.set(chunk[chunk.length - 1]!.filePath, pw); |
| 387 | pw.busy++; |
| 388 | chunkPromises.push( |
| 389 | new Promise<ChunkResult>((resolve, reject) => { |
| 390 | this.waiters.set(id, { resolve, reject }); |
| 391 | pw.worker.postMessage({ type: 'resolve', id, refs: chunk }); |
| 392 | }) |
| 393 | ); |
| 394 | } |
| 395 | const chunks = await Promise.all(chunkPromises); |
| 396 | const out: ChunkResult = { resolved: [], unresolved: [], deferredChain: [], deferredThisMember: [], byMethod: {} }; |
| 397 | for (const c of chunks) { |
| 398 | out.resolved.push(...c.resolved); |
| 399 | out.unresolved.push(...c.unresolved); |
| 400 | out.deferredChain.push(...c.deferredChain); |
| 401 | out.deferredThisMember.push(...c.deferredThisMember); |
| 402 | for (const [k, v] of Object.entries(c.byMethod)) out.byMethod[k] = (out.byMethod[k] || 0) + v; |
| 403 | } |
| 404 | return out; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Run one synthesis pass (by SYNTH_PASSES name) on the least-busy worker. |
no test coverage detected