Skip to content

Commit

Permalink
feat: adding option to set initial and max memory (#3265)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
signorecello committed Dec 15, 2023
1 parent 35316fc commit 0ad75fe
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 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 @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/blake2s.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
9 changes: 7 additions & 2 deletions barretenberg/ts/src/barretenberg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<BarretenbergWasmMainWorker>(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);
}

Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/schnorr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
6 changes: 3 additions & 3 deletions barretenberg/ts/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0ad75fe

Please sign in to comment.