]> zoso.dev Git - nano-pow.git/commitdiff
Eliminate a call to Math.random() by using sliding 32-bit randomness.
authorChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 18:50:09 +0000 (11:50 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 22 Apr 2025 18:50:09 +0000 (11:50 -0700)
src/lib/gpu/index.ts

index fc7c20ad6772625316ec9c9e476d69200d85fd4b..b9a8b13db7e3e482bd3ee0666b14a5659f3c8eaf 100644 (file)
@@ -274,11 +274,12 @@ export class NanoPowGpu {
                let start = performance.now()
                let nonce = 0n
                let result = 0n
+               let random = BigInt(Math.floor(Math.random() * 0xffffffff))
+               let seed = random
                do {
                        start = performance.now()
-                       const random0 = Math.floor(Math.random() * 0xffffffff)
-                       const random1 = Math.floor(Math.random() * 0xffffffff)
-                       const seed = (BigInt(random0) << 32n) | BigInt(random1)
+                       random = BigInt(Math.floor(Math.random() * 0xffffffff))
+                       seed = (seed & 0xffffffffn) << 32n | random
                        if (this.#debug) console.log('seed', seed.toString(16).padStart(16, '0'))
                        await this.#dispatch(this.#searchPipeline, seed, hash, difficulty, effort)
                        const found = !!this.#resultView.getUint32(0)