From: Chris Duncan Date: Mon, 16 Dec 2024 21:05:17 +0000 (-0800) Subject: Fix pool incorrectly dividing up jobs and falling short of distributing all data... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8403dc97514233859f71d08ba07b5a4a6b67238a;p=libnemo.git Fix pool incorrectly dividing up jobs and falling short of distributing all data. Open up access to pool thread count by status. --- diff --git a/src/lib/pool.ts b/src/lib/pool.ts index 5211d2a..9c014ea 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -21,11 +21,26 @@ type Thread = { * Processes an array of tasks using Web Workers. */ export class Pool { - static #threads: Thread[] = [] static #cores: number = Math.max(1, navigator.hardwareConcurrency - 1) static #queue: Job[] = [] + static #threads: Thread[] = [] static #url: string = URL.createObjectURL(new Blob([Workers], { type: 'text/javascript' })) + static get threadsBusy (): number { + let n = 0 + for (const thread of this.#threads) { + n += +(thread.job != null) + } + return n + } + static get threadsIdle (): number { + let n = 0 + for (const thread of this.#threads) { + n += +(thread.job == null) + } + return n + } + static { for (let i = this.#cores; i > 0; i--) { const thread = { @@ -48,7 +63,7 @@ export class Pool { thread.worker.postMessage({ name: job.name, buffer: job.data }, [job.data]) } } else { - const chunk: number = 1 + (job.data.length / this.#cores) + const chunk: number = 1 + (job.data.length / this.threadsIdle) const next = job.data.slice(0, chunk) job.data = job.data.slice(chunk) if (job.data.length === 0) this.#queue.shift()