From 0ad75fe745099119726976f964a92d1587f32fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Fri, 15 Dec 2023 16:31:29 +0000 Subject: [PATCH] feat: adding option to set initial and max memory (#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). --- barretenberg/acir_tests/browser-test-app/src/index.ts | 2 +- barretenberg/ts/README.md | 2 +- barretenberg/ts/src/barretenberg/blake2s.test.ts | 2 +- barretenberg/ts/src/barretenberg/common.test.ts | 2 +- barretenberg/ts/src/barretenberg/index.ts | 9 +++++++-- barretenberg/ts/src/barretenberg/schnorr.test.ts | 2 +- barretenberg/ts/src/main.ts | 6 +++--- 7 files changed, 15 insertions(+), 10 deletions(-) diff --git a/barretenberg/acir_tests/browser-test-app/src/index.ts b/barretenberg/acir_tests/browser-test-app/src/index.ts index c634b2b1a51..56708214eeb 100644 --- a/barretenberg/acir_tests/browser-test-app/src/index.ts +++ b/barretenberg/acir_tests/browser-test-app/src/index.ts @@ -13,7 +13,7 @@ async function runTest( const CIRCUIT_SIZE = 2 ** 19; debug("starting test..."); - const api = await Barretenberg.new(threads); + const api = await Barretenberg.new({ threads }); // Important to init slab allocator as first thing, to ensure maximum memory efficiency. await api.commonInitSlabAllocator(CIRCUIT_SIZE); diff --git a/barretenberg/ts/README.md b/barretenberg/ts/README.md index 42cd716059a..4e3544af942 100644 --- a/barretenberg/ts/README.md +++ b/barretenberg/ts/README.md @@ -89,7 +89,7 @@ To create the API and do a blake2s hash: ```typescript import { Crs, Barretenberg, RawBuffer } from './index.js'; -const api = await Barretenberg.new(/* num_threads */ 1); +const api = await Barretenberg.new(/* num_threads */ { threads: 1 }); const input = Buffer.from('hello world!'); const result = await api.blake2s(input); await api.destroy(); diff --git a/barretenberg/ts/src/barretenberg/blake2s.test.ts b/barretenberg/ts/src/barretenberg/blake2s.test.ts index 23c6f9d678f..49ae654fe74 100644 --- a/barretenberg/ts/src/barretenberg/blake2s.test.ts +++ b/barretenberg/ts/src/barretenberg/blake2s.test.ts @@ -5,7 +5,7 @@ describe('blake2s async', () => { let api: Barretenberg; beforeAll(async () => { - api = await Barretenberg.new(1); + api = await Barretenberg.new({ threads: 1 }); }); afterAll(async () => { diff --git a/barretenberg/ts/src/barretenberg/common.test.ts b/barretenberg/ts/src/barretenberg/common.test.ts index 5697b255859..705bcedd8d3 100644 --- a/barretenberg/ts/src/barretenberg/common.test.ts +++ b/barretenberg/ts/src/barretenberg/common.test.ts @@ -4,7 +4,7 @@ describe('env', () => { let api: Barretenberg; beforeAll(async () => { - api = await Barretenberg.new(3); + api = await Barretenberg.new({ threads: 3 }); }, 15000); afterAll(async () => { diff --git a/barretenberg/ts/src/barretenberg/index.ts b/barretenberg/ts/src/barretenberg/index.ts index 6b00a9b752a..73e935d3372 100644 --- a/barretenberg/ts/src/barretenberg/index.ts +++ b/barretenberg/ts/src/barretenberg/index.ts @@ -8,6 +8,11 @@ import createDebug from 'debug'; const debug = createDebug('bb.js:wasm'); +export type BackendOptions = { + threads?: number; + memory?: { initial?: number; maximum?: number }; +}; + /** * The main class library consumers interact with. * It extends the generated api, and provides a static constructor "new" to compose components. @@ -23,11 +28,11 @@ export class Barretenberg extends BarretenbergApi { * and blocking the main thread in the browser is not allowed. * It threads > 1 (defaults to hardware availability), child threads will be created on their own workers. */ - static async new(desiredThreads?: number) { + static async new({ threads: desiredThreads, memory }: BackendOptions = {}) { const worker = createMainWorker(); const wasm = getRemoteBarretenbergWasm(worker); const { module, threads } = await fetchModuleAndThreads(desiredThreads); - await wasm.init(module, threads, proxy(debug)); + await wasm.init(module, threads, proxy(debug), memory?.initial, memory?.maximum); return new Barretenberg(worker, wasm); } diff --git a/barretenberg/ts/src/barretenberg/schnorr.test.ts b/barretenberg/ts/src/barretenberg/schnorr.test.ts index 7945cbce3b2..b82195f2a08 100644 --- a/barretenberg/ts/src/barretenberg/schnorr.test.ts +++ b/barretenberg/ts/src/barretenberg/schnorr.test.ts @@ -8,7 +8,7 @@ describe('schnorr', () => { let api: Barretenberg; beforeAll(async () => { - api = await Barretenberg.new(1); + api = await Barretenberg.new({ threads: 1 }); }, 30000); afterAll(async () => { diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 016c8a63c04..de4cf64631d 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -44,7 +44,7 @@ async function computeCircuitSize(bytecodePath: string, api: Barretenberg) { } async function init(bytecodePath: string, crsPath: string) { - const api = await Barretenberg.new(threads); + const api = await Barretenberg.new({ threads }); const circuitSize = await getGates(bytecodePath, api); const subgroupSize = Math.pow(2, Math.ceil(Math.log2(circuitSize))); @@ -70,7 +70,7 @@ async function init(bytecodePath: string, crsPath: string) { } async function initLite() { - const api = await Barretenberg.new(1); + const api = await Barretenberg.new({ threads: 1 }); // Plus 1 needed! (Move +1 into Crs?) const crs = await Crs.new(1); @@ -140,7 +140,7 @@ export async function prove( } export async function gateCount(bytecodePath: string) { - const api = await Barretenberg.new(1); + const api = await Barretenberg.new({ threads: 1 }); try { const numberOfGates = await getGates(bytecodePath, api);