From: Chris Duncan Date: Thu, 12 Dec 2024 17:17:50 +0000 (-0800) Subject: Eliminate an allocation by making work arrays static since they are randomized on... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8bc4d95be99a1996092b5bbfe629ddd89963c6b3;p=libnemo.git Eliminate an allocation by making work arrays static since they are randomized on every draw call anyway. --- diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index ad26e33..ff48dd9 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -32,6 +32,8 @@ export class Pow { static webglWidth = 256 * 4 static webglHeight = 256 * 4 static gl = new OffscreenCanvas(this.webglWidth, this.webglHeight).getContext('webgl2') + static work0 = new Uint8Array(4) + static work1 = new Uint8Array(4) static hexify (arr: number[] | Uint8Array): string { let out = '' @@ -290,17 +292,15 @@ export class Pow { const work1Location = this.gl.getUniformLocation(program, 'u_work1') // Draw output until success or progressCallback says to stop - const work0 = new Uint8Array(4) - const work1 = new Uint8Array(4) const draw = (): void => { if (this.gl == null) throw new Error('webgl2_required') const start = performance.now() - crypto.getRandomValues(work0) - crypto.getRandomValues(work1) + crypto.getRandomValues(this.work0) + crypto.getRandomValues(this.work1) - this.gl.uniform4uiv(work0Location, work0) - this.gl.uniform4uiv(work1Location, work1) + this.gl.uniform4uiv(work0Location, this.work0) + this.gl.uniform4uiv(work1Location, this.work1) this.gl.clear(this.gl.COLOR_BUFFER_BIT) this.gl.drawArrays(this.gl.TRIANGLES, 0, 6) @@ -316,11 +316,11 @@ export class Pow { console.log(`frame time: ${performance.now() - start}`) // Return the work value with the custom bits typeof callback === 'function' && - callback(this.hexify(work1) + this.hexify([ + callback(this.hexify(this.work1) + this.hexify([ pixels[i + 2], pixels[i + 3], - work0[2] ^ (pixels[i] - 1), - work0[3] ^ (pixels[i + 1] - 1) + this.work0[2] ^ (pixels[i] - 1), + this.work0[3] ^ (pixels[i + 1] - 1) ])) return }