* Resolve `refs` across the pool. Chunks preserve input order; the returned * arrays are the in-order concatenation of the chunk results.
(refs: UnresolvedReference[])
| 332 | * arrays are the in-order concatenation of the chunk results. |
| 333 | */ |
| 334 | async resolveBatch(refs: UnresolvedReference[]): Promise<ChunkResult> { |
| 335 | if (this.failed) throw this.failed; |
| 336 | const chunkPromises: Promise<ChunkResult>[] = []; |
| 337 | for (let i = 0; i < refs.length; i += CHUNK_SIZE) { |
| 338 | const chunk = refs.slice(i, i + CHUNK_SIZE); |
| 339 | const id = this.nextId++; |
| 340 | // Least-busy dispatch keeps workers evenly loaded regardless of chunk |
| 341 | // cost variance; result order is fixed by the promise array, not by |
| 342 | // completion order. A chunk that goes on with a file stays on the worker |
| 343 | // that has that file's lines, nodes and scans in its caches, unless that |
| 344 | // would leave it behind the others. References come file by file, so a |
| 345 | // file of 2,000 used to be read and scanned by four workers. |
| 346 | const least = this.workers.reduce((a, b) => (b.busy < a.busy ? b : a)); |
| 347 | const holder = this.fileWorkers.get(chunk[0]!.filePath); |
| 348 | const pw = holder && holder.busy <= least.busy + 1 ? holder : least; |
| 349 | this.fileWorkers.set(chunk[chunk.length - 1]!.filePath, pw); |
| 350 | pw.busy++; |
| 351 | chunkPromises.push( |
| 352 | new Promise<ChunkResult>((resolve, reject) => { |
| 353 | this.waiters.set(id, { resolve, reject }); |
| 354 | pw.worker.postMessage({ type: 'resolve', id, refs: chunk }); |
| 355 | }) |
| 356 | ); |
| 357 | } |
| 358 | const chunks = await Promise.all(chunkPromises); |
| 359 | const out: ChunkResult = { resolved: [], unresolved: [], deferredChain: [], deferredThisMember: [], byMethod: {} }; |
| 360 | for (const c of chunks) { |
| 361 | out.resolved.push(...c.resolved); |
| 362 | out.unresolved.push(...c.unresolved); |
| 363 | out.deferredChain.push(...c.deferredChain); |
| 364 | out.deferredThisMember.push(...c.deferredThisMember); |
| 365 | for (const [k, v] of Object.entries(c.byMethod)) out.byMethod[k] = (out.byMethod[k] || 0) + v; |
| 366 | } |
| 367 | return out; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Run one synthesis pass (by SYNTH_PASSES name) on the least-busy worker. |
no test coverage detected