From 2209fd4d1a19715c92173584a444cf31051b7e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Thu, 1 Feb 2024 15:21:11 +0000 Subject: [PATCH 1/5] feat: adding option to set initial and max memory --- tooling/noir_js_backend_barretenberg/src/index.ts | 2 +- tooling/noir_js_backend_barretenberg/src/types.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index 61094a3451f..14e9dcf0681 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -34,7 +34,7 @@ export class BarretenbergBackend implements Backend { async instantiate(): Promise { if (!this.api) { const { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js'); - const api = await Barretenberg.new({ threads: this.options.threads }); + const api = await Barretenberg.new(this.options); const [_exact, _total, subgroupSize] = await api.acirGetCircuitSizes(this.acirUncompressedBytecode); const crs = await Crs.new(subgroupSize + 1); diff --git a/tooling/noir_js_backend_barretenberg/src/types.ts b/tooling/noir_js_backend_barretenberg/src/types.ts index 041e36fdf91..16faba85da1 100644 --- a/tooling/noir_js_backend_barretenberg/src/types.ts +++ b/tooling/noir_js_backend_barretenberg/src/types.ts @@ -5,4 +5,5 @@ export type BackendOptions = { /** @description Number of threads */ threads: number; + memory?: { initial?: number; maximum?: number }; }; From 25ea2c3ce60af39e3160c80f661d64c4f1ddb66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Wed, 14 Feb 2024 11:15:15 +0000 Subject: [PATCH 2/5] adding back the maximum setting and making number of threads default to num of cpus --- tooling/noir_js_backend_barretenberg/src/index.ts | 3 ++- tooling/noir_js_backend_barretenberg/src/types.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index 14e9dcf0681..c5457ecb898 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -6,6 +6,7 @@ import { deflattenPublicInputs, flattenPublicInputsAsArray } from './public_inpu import { type Barretenberg } from '@aztec/bb.js'; export { publicInputsToWitnessMap } from './public_inputs.js'; +import { cpus } from "os"; // This is the number of bytes in a UltraPlonk proof // minus the public inputs. @@ -24,7 +25,7 @@ export class BarretenbergBackend implements Backend { constructor( acirCircuit: CompiledCircuit, - private options: BackendOptions = { threads: 1 }, + private options: BackendOptions = { threads: navigator ? navigator.hardwareConcurrency : cpus().length }, ) { const acirBytecodeBase64 = acirCircuit.bytecode; this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64); diff --git a/tooling/noir_js_backend_barretenberg/src/types.ts b/tooling/noir_js_backend_barretenberg/src/types.ts index 16faba85da1..fac23030aad 100644 --- a/tooling/noir_js_backend_barretenberg/src/types.ts +++ b/tooling/noir_js_backend_barretenberg/src/types.ts @@ -5,5 +5,5 @@ export type BackendOptions = { /** @description Number of threads */ threads: number; - memory?: { initial?: number; maximum?: number }; + memory?: { maximum: number }; }; From ca1b07f95b0fee051fdbf1588f08428e4b3646df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Wed, 14 Feb 2024 11:54:27 +0000 Subject: [PATCH 3/5] linter --- tooling/noir_js_backend_barretenberg/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index c5457ecb898..8fdeea1b77f 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -6,7 +6,7 @@ import { deflattenPublicInputs, flattenPublicInputsAsArray } from './public_inpu import { type Barretenberg } from '@aztec/bb.js'; export { publicInputsToWitnessMap } from './public_inputs.js'; -import { cpus } from "os"; +import { cpus } from 'os'; // This is the number of bytes in a UltraPlonk proof // minus the public inputs. From 7e159db1d500af84091b1d128f420026ea74093b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Thu, 29 Feb 2024 16:04:37 +0000 Subject: [PATCH 4/5] navigator throwing immediately --- tooling/noir_js_backend_barretenberg/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index 8fdeea1b77f..911187635e2 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -25,7 +25,7 @@ export class BarretenbergBackend implements Backend { constructor( acirCircuit: CompiledCircuit, - private options: BackendOptions = { threads: navigator ? navigator.hardwareConcurrency : cpus().length }, + private options: BackendOptions = { threads: typeof navigator !== "undefined" ? navigator.hardwareConcurrency : cpus().length}, ) { const acirBytecodeBase64 = acirCircuit.bytecode; this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64); From fe19ed7c1cc459bb4ac1ff52e2f568f6cf187339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Fri, 1 Mar 2024 11:31:27 +0000 Subject: [PATCH 5/5] mocking module in test runner --- compiler/integration-tests/package.json | 1 + compiler/integration-tests/test/mocks/os.js | 1 + .../web-test-runner.config.mjs | 15 +++++++++++-- .../noir_js_backend_barretenberg/src/index.ts | 13 +++++++++-- yarn.lock | 22 +++++++++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 compiler/integration-tests/test/mocks/os.js diff --git a/compiler/integration-tests/package.json b/compiler/integration-tests/package.json index c4e424df480..4a16d9d1e57 100644 --- a/compiler/integration-tests/package.json +++ b/compiler/integration-tests/package.json @@ -19,6 +19,7 @@ "@nomicfoundation/hardhat-chai-matchers": "^2.0.0", "@nomicfoundation/hardhat-ethers": "^3.0.0", "@web/dev-server-esbuild": "^0.3.6", + "@web/dev-server-import-maps": "^0.2.0", "@web/test-runner": "^0.15.3", "@web/test-runner-playwright": "^0.10.0", "eslint": "^8.50.0", diff --git a/compiler/integration-tests/test/mocks/os.js b/compiler/integration-tests/test/mocks/os.js new file mode 100644 index 00000000000..32333568316 --- /dev/null +++ b/compiler/integration-tests/test/mocks/os.js @@ -0,0 +1 @@ +export const os = { cpus: () => new Array(4) }; diff --git a/compiler/integration-tests/web-test-runner.config.mjs b/compiler/integration-tests/web-test-runner.config.mjs index 665ea262f99..4dfc96dd0a6 100644 --- a/compiler/integration-tests/web-test-runner.config.mjs +++ b/compiler/integration-tests/web-test-runner.config.mjs @@ -2,7 +2,8 @@ import { defaultReporter } from '@web/test-runner'; import { summaryReporter } from '@web/test-runner'; import { fileURLToPath } from 'url'; import { esbuildPlugin } from '@web/dev-server-esbuild'; -import { playwrightLauncher } from "@web/test-runner-playwright"; +import { playwrightLauncher } from '@web/test-runner-playwright'; +import { importMapsPlugin } from '@web/dev-server-import-maps'; let reporter = summaryReporter(); const debugPlugins = []; @@ -21,7 +22,7 @@ if (process.env.CI !== 'true' || process.env.RUNNER_DEBUG === '1') { export default { browsers: [ - playwrightLauncher({ product: "chromium" }), + playwrightLauncher({ product: 'chromium' }), // playwrightLauncher({ product: "webkit" }), // playwrightLauncher({ product: "firefox" }), ], @@ -29,6 +30,16 @@ export default { esbuildPlugin({ ts: true, }), + importMapsPlugin({ + inject: { + importMap: { + imports: { + // mock os module + os: '/test/mocks/os.js', + }, + }, + }, + }), ...debugPlugins, ], files: ['test/browser/**/*.test.ts'], diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index 911187635e2..197e63efbc8 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -6,7 +6,6 @@ import { deflattenPublicInputs, flattenPublicInputsAsArray } from './public_inpu import { type Barretenberg } from '@aztec/bb.js'; export { publicInputsToWitnessMap } from './public_inputs.js'; -import { cpus } from 'os'; // This is the number of bytes in a UltraPlonk proof // minus the public inputs. @@ -25,7 +24,7 @@ export class BarretenbergBackend implements Backend { constructor( acirCircuit: CompiledCircuit, - private options: BackendOptions = { threads: typeof navigator !== "undefined" ? navigator.hardwareConcurrency : cpus().length}, + private options: BackendOptions = { threads: 1 }, ) { const acirBytecodeBase64 = acirCircuit.bytecode; this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64); @@ -34,6 +33,16 @@ export class BarretenbergBackend implements Backend { /** @ignore */ async instantiate(): Promise { 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 { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js'); const api = await Barretenberg.new(this.options); diff --git a/yarn.lock b/yarn.lock index 743068f1907..26f185c208a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3965,6 +3965,13 @@ __metadata: languageName: node linkType: hard +"@import-maps/resolve@npm:^1.0.1": + version: 1.0.1 + resolution: "@import-maps/resolve@npm:1.0.1" + checksum: 17ee033e26a0fd82294de87eae76d32b553a130fdbf0fb8c70d39f2087a3e8a4a5908970a99aa32bd175153efe9b7dfee6b7f99df36f41abed08c1911dbdb19c + languageName: node + linkType: hard + "@isaacs/cliui@npm:^8.0.2": version: 8.0.2 resolution: "@isaacs/cliui@npm:8.0.2" @@ -6759,6 +6766,20 @@ __metadata: languageName: node linkType: hard +"@web/dev-server-import-maps@npm:^0.2.0": + version: 0.2.0 + resolution: "@web/dev-server-import-maps@npm:0.2.0" + dependencies: + "@import-maps/resolve": ^1.0.1 + "@types/parse5": ^6.0.1 + "@web/dev-server-core": ^0.7.0 + "@web/parse5-utils": ^2.1.0 + parse5: ^6.0.1 + picomatch: ^2.2.2 + checksum: 15dabfa385f023bab70758b80cc09443455830799793c1a404a7230d90ebf60e40984a10d8a6ceea2afb8f057e90a9f7356a76f867d5e5a2eeacbc397e41535a + languageName: node + linkType: hard + "@web/dev-server-rollup@npm:^0.4.1": version: 0.4.1 resolution: "@web/dev-server-rollup@npm:0.4.1" @@ -13400,6 +13421,7 @@ __metadata: "@nomicfoundation/hardhat-chai-matchers": ^2.0.0 "@nomicfoundation/hardhat-ethers": ^3.0.0 "@web/dev-server-esbuild": ^0.3.6 + "@web/dev-server-import-maps": ^0.2.0 "@web/test-runner": ^0.15.3 "@web/test-runner-playwright": ^0.10.0 eslint: ^8.50.0