From: Chris Duncan Date: Tue, 7 Jan 2025 21:31:06 +0000 (-0800) Subject: Refactor GPU device loading to avoid high promise stack. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=f7b5183abe01cccd695d68069e1ab472984fa315;p=libnemo.git Refactor GPU device loading to avoid high promise stack. --- diff --git a/src/lib/workers/powgpu.ts b/src/lib/workers/powgpu.ts index 6267259..b60ce03 100644 --- a/src/lib/workers/powgpu.ts +++ b/src/lib/workers/powgpu.ts @@ -456,7 +456,6 @@ export class PowGpu extends WorkerInterface { .catch(err => { throw new Error(err.message) }) } - /** * Finds a nonce that satisfies the Nano proof-of-work requirements. * @@ -468,17 +467,12 @@ export class PowGpu extends WorkerInterface { if (typeof threshold !== 'number') throw new TypeError(`Invalid threshold ${threshold}`) // Ensure WebGPU is initialized before calculating, up to a max time frame - if (PowGpu.#device == null) { - if (performance.now() > 8000) { - throw new Error(`WebGPU device failed to load in time.`) - } - return await new Promise(resolve => { - setTimeout(() => { - resolve(this.search(hash, threshold)) - }, 100) + while (PowGpu.#device == null && performance.now() < 8000) { + await new Promise(resolve => { + setTimeout(resolve, 100) }) } - + if (PowGpu.#device == null) throw new Error(`WebGPU device failed to load.`) // Set up uniform buffer object // Note: u32 size is 4, but total alignment must be multiple of 16