Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by reverting Apple silicon workaround (root problem is fixed upstream) #251

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions js/node/node-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isMainThread, parentPort, workerData, Worker } from 'worker_threads';
import os from 'os';
import wasm_ from '../../compiled/_node_bindings/plonk_wasm.cjs';
import { fileURLToPath } from 'url';
import { workers } from '../../../lib/proof-system/workers.js';
let url = import.meta.url;
let filename = url !== undefined ? fileURLToPath(url) : __filename;

Expand Down Expand Up @@ -82,7 +83,10 @@ async function withThreadPool(run) {
async function initThreadPool() {
if (!isMainThread) return;
workersReady = new Promise((resolve) => (workersReadyResolve = resolve));
await wasm.initThreadPool(getEfficientNumWorkers(), filename);
await wasm.initThreadPool(
Math.max(1, workers.numWorkers ?? (os.availableParallelism() ?? 1) - 1),
filename
);
await workersReady;
workersReady = undefined;
}
Expand Down Expand Up @@ -126,24 +130,3 @@ async function terminateWorkers() {
() => (wasmWorkers = undefined)
);
}

// Return the most efficient number of workers for the platform we're running
// on. This is required because of an issue with Apple silicon that's outlined
// here: https://bugs.chromium.org/p/chromium/issues/detail?id=1228686
function getEfficientNumWorkers() {
let cpus = os.cpus();
let numCpus = cpus.length;
let cpuModel = cpus[0].model;

let numWorkers =
{
'Apple M1': 2,
'Apple M1 Pro': numCpus === 10 ? 3 : 2,
'Apple M1 Max': 3,
'Apple M1 Ultra': 7,
'Apple M2': 2,
'Apple M2 Pro': 2,
}[cpuModel] || numCpus - 1;

return numWorkers;
}
24 changes: 0 additions & 24 deletions js/web/num-workers.js

This file was deleted.

4 changes: 2 additions & 2 deletions js/web/web-backend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import plonkWasm from '../../../web_bindings/plonk_wasm.js';
import { workerSpec } from './worker-spec.js';
import { getEfficientNumWorkers } from './num-workers.js';
import {
srcFromFunctionModule,
inlineWorker,
waitForMessage,
} from './worker-helpers.js';
import o1jsWebSrc from 'string:../../../web_bindings/o1js_web.bc.js';
import { workers } from '../../../lib/proof-system/workers.js';

export { initO1, withThreadPool };

Expand Down Expand Up @@ -55,7 +55,7 @@ async function withThreadPool(run) {
if (workerPromise === undefined)
throw Error('need to initialize worker first');
let worker = await workerPromise;
numWorkers ??= await getEfficientNumWorkers();
numWorkers ??= Math.max(1, workers.numWorkers ?? (navigator.hardwareConcurrency ?? 1) - 1);
await workerCall(worker, 'initThreadPool', numWorkers);
let result;
try {
Expand Down
Loading