Skip to content

Commit

Permalink
Merge pull request #683 from o1-labs/fix/apple-silicon-performance
Browse files Browse the repository at this point in the history
Fix/apple silicon performance
  • Loading branch information
Trivo25 committed Jan 13, 2023
2 parents 4aea352 + 7d7b367 commit 0e0ca37
Show file tree
Hide file tree
Showing 14 changed files with 1,648 additions and 978 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `SmartContract.fetchEvents` fixed when multiple event types existed https://github.com/o1-labs/snarkyjs/issues/627
- Error when using reduce with a `Struct` as state type https://github.com/o1-labs/snarkyjs/pull/689
- Fix use of stale cached accounts in `Mina.transaction` https://github.com/o1-labs/snarkyjs/issues/430
- Fixed Apple silicon performance issue https://github.com/o1-labs/snarkyjs/issues/491

## [0.7.3](https://github.com/o1-labs/snarkyjs/compare/5f20f496...d880bd6e)

Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
},
"dependencies": {
"blakejs": "1.2.1",
"detect-gpu": "^5.0.5",
"env": "^0.0.2",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
Expand Down
25 changes: 25 additions & 0 deletions src/chrome_bindings/getEfficientNumWorkers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getGPUTier } from 'detect-gpu';

export { getEfficientNumWorkers as default };

// 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
async function getEfficientNumWorkers() {
let gpuTier = await getGPUTier();
let numCpus = navigator.hardwareConcurrency;
// gpuTier.gpu is undefined if page was rendered server side
let gpuModel = gpuTier.gpu || 'SSR';

var numWorkers =
{
'apple m1': 2,
'apple m1 pro': numCpus === 10 ? 3 : 2,
'apple m1 max': 3,
'apple m1 ultra': 7,
'apple m2': 2,
'SSR': ((numCpus / 6) >> 0) + 1,
}[gpuModel] || numCpus - 1;

return numWorkers;
}
4 changes: 3 additions & 1 deletion src/chrome_bindings/plonk_init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import plonkWasm from './plonk_wasm.js';
import workerRun from './worker_run.js';
import workerInit from './worker_init.js';
import getEfficientNumWorkers from './getEfficientNumWorkers';
import { srcFromFunctionModule, inlineWorker } from './workerHelpers.js';
import snarkyJsChromeSrc from 'string:./snarky_js_chrome.bc.js';
let plonk_wasm = plonkWasm();
Expand All @@ -10,9 +11,10 @@ let { override_bindings } = workerRun();
export async function initSnarkyJS() {
let { memory } = await init();
let module = init.__wbindgen_wasm_module;
let numWorkers = await getEfficientNumWorkers();

let worker = inlineWorker(srcFromFunctionModule(workerInit));
worker.postMessage({ type: 'init', memory, module });
worker.postMessage({ type: 'init', memory, module, numWorkers });

let workersReady = new Promise((resolve) => (worker.onmessage = resolve));
await workersReady;
Expand Down
Loading

0 comments on commit 0e0ca37

Please sign in to comment.