Skip to content

Commit

Permalink
use proper thread pool management for web as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 28, 2024
1 parent 649e6c3 commit d6509b9
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions js/web/web-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
waitForMessage,
} from './worker-helpers.js';
import o1jsWebSrc from 'string:../../../web_bindings/o1js_web.bc.js';
import { workers } from '../../../lib/proof-system/workers.js';
import { WithThreadPool, workers } from '../../../lib/proof-system/workers.js';

export { initializeBindings, withThreadPool, wasm };

Expand Down Expand Up @@ -53,7 +53,7 @@ async function initializeBindings() {
});
}

async function withThreadPool(run) {
async function initThreadPool() {
if (workerPromise === undefined)
throw Error('need to initialize worker first');
let worker = await workerPromise;
Expand All @@ -62,15 +62,17 @@ async function withThreadPool(run) {
workers.numWorkers ?? (navigator.hardwareConcurrency ?? 1) - 1
);
await workerCall(worker, 'initThreadPool', numWorkers);
let result;
try {
result = await run();
} finally {
await workerCall(worker, 'exitThreadPool');
}
return result;
}

async function exitThreadPool() {
if (workerPromise === undefined)
throw Error('need to initialize worker first');
let worker = await workerPromise;
await workerCall(worker, 'exitThreadPool');
}

const withThreadPool = WithThreadPool({ initThreadPool, exitThreadPool });

async function mainWorker() {
const wasm = plonkWasm();
let init = wasm.default;
Expand Down

0 comments on commit d6509b9

Please sign in to comment.