Skip to content

Commit

Permalink
fix: limit number of bb.js threads to 32 (#9070)
Browse files Browse the repository at this point in the history
This PR pulls out the limiting of the number of threads from #8861
without the other changes which are failing CI.
  • Loading branch information
TomAFrench authored Oct 8, 2024
1 parent 9d28414 commit 97e4b9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion barretenberg/acir_tests/browser-test-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createDebug from "debug";
import { inflate } from "pako";

createDebug.enable("*");
const debug = createDebug("simple_test");
const debug = createDebug("browser-test-app");

async function runTest(
bytecode: Uint8Array,
Expand Down
20 changes: 0 additions & 20 deletions barretenberg/ts/src/barretenberg/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ export class UltraPlonkBackend {
/** @ignore */
async instantiate(): Promise<void> {
if (!this.api) {
if (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) {
this.options.threads = navigator.hardwareConcurrency;
} else {
try {
const os = await import('os');
this.options.threads = os.cpus().length;
} catch (e) {
console.log('Could not detect environment. Falling back to one thread.', e);
}
}
const api = await Barretenberg.new(this.options);

const honkRecursion = false;
Expand Down Expand Up @@ -125,16 +115,6 @@ export class UltraHonkBackend {
/** @ignore */
async instantiate(): Promise<void> {
if (!this.api) {
if (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) {
this.options.threads = navigator.hardwareConcurrency;
} else {
try {
const os = await import('os');
this.options.threads = os.cpus().length;
} catch (e) {
console.log('Could not detect environment. Falling back to one thread.', e);
}
}
const api = await Barretenberg.new(this.options);
const honkRecursion = true;
await api.acirInitSRS(this.acirUncompressedBytecode, honkRecursion);
Expand Down
11 changes: 0 additions & 11 deletions barretenberg/ts/src/barretenberg/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ export class BarretenbergVerifier {
/** @ignore */
async instantiate(): Promise<void> {
if (!this.api) {
if (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) {
this.options.threads = navigator.hardwareConcurrency;
} else {
try {
const os = await import('os');
this.options.threads = os.cpus().length;
} catch (e) {
console.log('Could not detect environment. Falling back to one thread.', e);
}
}

const api = await Barretenberg.new(this.options);
await api.initSRSForCircuitSize(0);

Expand Down
24 changes: 21 additions & 3 deletions barretenberg/ts/src/barretenberg_wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ import { fetchCode } from './fetch_code/index.js';

const debug = createDebug('bb.js:wasm');

export async function fetchModuleAndThreads(desiredThreads?: number) {
export async function fetchModuleAndThreads(desiredThreads = 32) {
const shared = getSharedMemoryAvailable();
const threads = shared ? desiredThreads : 1;

const availableThreads = shared ? await getAvailableThreads() : 1;
// We limit the number of threads to 32 as we do not benefit from greater numbers.
const limitedThreads = Math.min(desiredThreads, availableThreads, 32);

const code = await fetchCode(shared);
const module = await WebAssembly.compile(code);
return { module, threads };
return { module, threads: limitedThreads };
}

async function getAvailableThreads(): Promise<number> {
if (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) {
return navigator.hardwareConcurrency;
} else {
try {
const os = await import('os');
return os.cpus().length;
} catch (e) {
debug(`Could not detect environment. Falling back to one thread.: {e}`);
return 1;
}
}
}

export class BarretenbergWasm extends BarretenbergWasmMain {
Expand Down

0 comments on commit 97e4b9b

Please sign in to comment.