From f7b5183abe01cccd695d68069e1ab472984fa315 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 7 Jan 2025 13:31:06 -0800 Subject: [PATCH] Refactor GPU device loading to avoid high promise stack. --- src/lib/workers/powgpu.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) 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 -- 2.34.1