]> zoso.dev Git - libnemo.git/commitdiff
Eliminate an allocation by making work arrays static since they are randomized on...
authorChris Duncan <chris@zoso.dev>
Thu, 12 Dec 2024 17:17:50 +0000 (09:17 -0800)
committerChris Duncan <chris@zoso.dev>
Thu, 12 Dec 2024 17:17:50 +0000 (09:17 -0800)
src/lib/workers/pow.ts

index ad26e337d46971cf009841d62547b0e841e12210..ff48dd9bae78a8ace1b09d94f84e228800e70a64 100644 (file)
@@ -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
                                }