-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove excessive event listener creation
- Loading branch information
Showing
7 changed files
with
64 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
import { state, stateBuffer } from "./state.js"; | ||
|
||
function createWorker() { | ||
const worker = new Worker('./js/worker.js') | ||
function createWorkers(count) { | ||
const workers_promise = new Array(count) | ||
for (let i = 0; i < count; ++i) { | ||
const worker = new Worker('./js/worker.js') | ||
|
||
return new Promise(resolve => worker.addEventListener('message', e => { | ||
const msg = e.data | ||
if (msg != 'ready') throw new Error(`Expected 'ready', got ${msg}`) | ||
workers_promise[i] = new Promise(resolve => { | ||
worker.addEventListener('message', e => { | ||
const msg = e.data | ||
if (msg != 'ready') throw new Error(`Expected 'ready', got ${msg}`) | ||
|
||
resolve(worker) | ||
}, { once: true })) | ||
resolve(worker) | ||
}, { once: true }) | ||
}) | ||
} | ||
|
||
return Promise.all(workers_promise) | ||
} | ||
|
||
function createWorkers(count) { | ||
let workers = [createWorker()] | ||
for (let i = 1; i < count; ++i) workers.push(createWorker()) | ||
const workerCount = Math.max(navigator.hardwareConcurrency, 1) | ||
|
||
return Promise.all(workers) | ||
} | ||
export async function registerAI(callback) { | ||
let workers = await createWorkers(workerCount) | ||
let working = 0 | ||
|
||
const workerCount = navigator.hardwareConcurrency | ||
function workerCallback(e) { | ||
if (working == workers.length) state.set_children(e.data) | ||
else state.add_children(e.data) | ||
|
||
let workers = await createWorkers(workerCount) | ||
let working = false | ||
working-- | ||
|
||
export async function invokeAI(strength) { | ||
const start = performance.now() | ||
let promises = [] | ||
if (!working) { | ||
state.best_move(e.data.byteLength) | ||
callback() | ||
} | ||
} | ||
|
||
for (const worker of workers) { | ||
promises.push(new Promise(resolve => { | ||
worker.addEventListener('message', e => { | ||
resolve(e.data) | ||
}, { once: true }) | ||
})) | ||
worker.addEventListener('message', workerCallback) | ||
} | ||
|
||
working = true | ||
for (const worker of workers) { | ||
const arr = new Uint8Array(stateBuffer.length) | ||
arr.set(stateBuffer) | ||
function invokeAI(strength) { | ||
working = workers.length | ||
for (const worker of workers) { | ||
const arr = new Uint8Array(stateBuffer.length) | ||
arr.set(stateBuffer) | ||
|
||
const buf = arr.buffer | ||
worker.postMessage({ strength, buf }, [buf]) | ||
const buf = arr.buffer | ||
worker.postMessage({ strength, buf }, [buf]) | ||
} | ||
} | ||
|
||
const results = await Promise.all(promises) | ||
working = false | ||
async function stopAI() { | ||
if (!working) return | ||
|
||
state.set_children(results[0]) | ||
for (let i = 1; i < promises.length; ++i) state.add_children(promises[i]) | ||
for (const worker of workers) worker.terminate() | ||
workers = await createWorkers(workerCount) | ||
|
||
state.best_move(results[0].byteLength) | ||
console.log(`Search time: ${performance.now() - start}`) | ||
} | ||
|
||
export async function stopAI() { | ||
if (!working) return | ||
for (const worker of workers) { | ||
worker.addEventListener("message", workerCallback) | ||
} | ||
|
||
for (const worker of workers) worker.terminate() | ||
workers = await createWorkers(workerCount) | ||
working = 0 | ||
} | ||
|
||
working = false | ||
return { invokeAI, stopAI } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.