Skip to content

Commit 0ad75fe

Browse files
authored
feat: adding option to set initial and max memory (AztecProtocol#3265)
This PR adds an object with type `BackendOptions`, allowing to set number of threads and the memory to be allocated. The initial motivation was to allow for `bb.js` to generate proofs on phones, as some would immediately kill the worker if the default value for `maximum` was used (`2 ** 16`, about 4gb). Turned it into an object so it closely matches the implementation in `@noir-lang/backend_barretenberg` # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist).
1 parent 35316fc commit 0ad75fe

File tree

7 files changed

+15
-10
lines changed

7 files changed

+15
-10
lines changed

barretenberg/acir_tests/browser-test-app/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function runTest(
1313
const CIRCUIT_SIZE = 2 ** 19;
1414

1515
debug("starting test...");
16-
const api = await Barretenberg.new(threads);
16+
const api = await Barretenberg.new({ threads });
1717

1818
// Important to init slab allocator as first thing, to ensure maximum memory efficiency.
1919
await api.commonInitSlabAllocator(CIRCUIT_SIZE);

barretenberg/ts/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ To create the API and do a blake2s hash:
8989
```typescript
9090
import { Crs, Barretenberg, RawBuffer } from './index.js';
9191

92-
const api = await Barretenberg.new(/* num_threads */ 1);
92+
const api = await Barretenberg.new(/* num_threads */ { threads: 1 });
9393
const input = Buffer.from('hello world!');
9494
const result = await api.blake2s(input);
9595
await api.destroy();

barretenberg/ts/src/barretenberg/blake2s.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('blake2s async', () => {
55
let api: Barretenberg;
66

77
beforeAll(async () => {
8-
api = await Barretenberg.new(1);
8+
api = await Barretenberg.new({ threads: 1 });
99
});
1010

1111
afterAll(async () => {

barretenberg/ts/src/barretenberg/common.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe('env', () => {
44
let api: Barretenberg;
55

66
beforeAll(async () => {
7-
api = await Barretenberg.new(3);
7+
api = await Barretenberg.new({ threads: 3 });
88
}, 15000);
99

1010
afterAll(async () => {

barretenberg/ts/src/barretenberg/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import createDebug from 'debug';
88

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

11+
export type BackendOptions = {
12+
threads?: number;
13+
memory?: { initial?: number; maximum?: number };
14+
};
15+
1116
/**
1217
* The main class library consumers interact with.
1318
* It extends the generated api, and provides a static constructor "new" to compose components.
@@ -23,11 +28,11 @@ export class Barretenberg extends BarretenbergApi {
2328
* and blocking the main thread in the browser is not allowed.
2429
* It threads > 1 (defaults to hardware availability), child threads will be created on their own workers.
2530
*/
26-
static async new(desiredThreads?: number) {
31+
static async new({ threads: desiredThreads, memory }: BackendOptions = {}) {
2732
const worker = createMainWorker();
2833
const wasm = getRemoteBarretenbergWasm<BarretenbergWasmMainWorker>(worker);
2934
const { module, threads } = await fetchModuleAndThreads(desiredThreads);
30-
await wasm.init(module, threads, proxy(debug));
35+
await wasm.init(module, threads, proxy(debug), memory?.initial, memory?.maximum);
3136
return new Barretenberg(worker, wasm);
3237
}
3338

barretenberg/ts/src/barretenberg/schnorr.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('schnorr', () => {
88
let api: Barretenberg;
99

1010
beforeAll(async () => {
11-
api = await Barretenberg.new(1);
11+
api = await Barretenberg.new({ threads: 1 });
1212
}, 30000);
1313

1414
afterAll(async () => {

barretenberg/ts/src/main.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function computeCircuitSize(bytecodePath: string, api: Barretenberg) {
4444
}
4545

4646
async function init(bytecodePath: string, crsPath: string) {
47-
const api = await Barretenberg.new(threads);
47+
const api = await Barretenberg.new({ threads });
4848

4949
const circuitSize = await getGates(bytecodePath, api);
5050
const subgroupSize = Math.pow(2, Math.ceil(Math.log2(circuitSize)));
@@ -70,7 +70,7 @@ async function init(bytecodePath: string, crsPath: string) {
7070
}
7171

7272
async function initLite() {
73-
const api = await Barretenberg.new(1);
73+
const api = await Barretenberg.new({ threads: 1 });
7474

7575
// Plus 1 needed! (Move +1 into Crs?)
7676
const crs = await Crs.new(1);
@@ -140,7 +140,7 @@ export async function prove(
140140
}
141141

142142
export async function gateCount(bytecodePath: string) {
143-
const api = await Barretenberg.new(1);
143+
const api = await Barretenberg.new({ threads: 1 });
144144
try {
145145
const numberOfGates = await getGates(bytecodePath, api);
146146

0 commit comments

Comments
 (0)