Skip to content

Commit 44eb218

Browse files
authored
Merge pull request o1-labs#1874 from o1-labs/2024-10-bugfix-lb-performance
Build fix for lagrange basis performance
2 parents fc16ce3 + 7958bda commit 44eb218

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1919

2020
### Fixes
2121

22+
- Performance regression when compiling recursive circuits is fixed https://github.com/o1-labs/o1js/pull/1874
2223
- Decouple offchain state instances from their definitions https://github.com/o1-labs/o1js/pull/1834
2324

2425
## [1.9.0](https://github.com/o1-labs/o1js/compare/450943...f15293a69) - 2024-10-15

src/lib/proof-system/cache.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { jsEnvironment } from '../../bindings/crypto/bindings/env.js';
1111
export { Cache, CacheHeader };
1212

1313
// internal API
14-
export { readCache, writeCache, withVersion, cacheHeaderVersion };
14+
export {
15+
readCache,
16+
writeCache,
17+
withVersion,
18+
cacheHeaderVersion,
19+
LAGRANGE_BASIS_PREFIX,
20+
};
1521

1622
/**
1723
* Interface for storing and retrieving values, for caching.
@@ -90,6 +96,8 @@ type StepKeyHeader<Kind> = {
9096
type WrapKeyHeader<Kind> = { kind: Kind; programName: string; hash: string };
9197
type PlainHeader<Kind> = { kind: Kind };
9298

99+
const LAGRANGE_BASIS_PREFIX = 'lagrange-basis' as const;
100+
93101
/**
94102
* A header that is passed to the caching layer, to support rich caching strategies.
95103
*
@@ -101,7 +109,7 @@ type CacheHeader = (
101109
| WrapKeyHeader<'wrap-pk'>
102110
| WrapKeyHeader<'wrap-vk'>
103111
| PlainHeader<'srs'>
104-
| PlainHeader<'lagrange-basis'>
112+
| PlainHeader<typeof LAGRANGE_BASIS_PREFIX>
105113
) &
106114
CommonHeader;
107115

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Cache, LAGRANGE_BASIS_PREFIX } from './cache.js';
2+
import { SelfProof, ZkProgram } from './zkprogram.js';
3+
import { Field } from '../provable/field.js';
4+
import { it, describe, after, before } from 'node:test';
5+
import { expect } from 'expect';
6+
import { promises as fs } from 'fs';
7+
8+
const __cacheDirname = './.tmpcache';
9+
10+
const exampleProgram = ZkProgram({
11+
name: 'example',
12+
publicOutput: Field,
13+
methods: {
14+
init: {
15+
privateInputs: [],
16+
async method() {
17+
return new Field(0);
18+
},
19+
},
20+
run: {
21+
privateInputs: [SelfProof],
22+
async method(p: SelfProof<undefined, Field>) {
23+
return p.publicOutput.add(new Field(1));
24+
},
25+
},
26+
},
27+
});
28+
29+
describe('Compiling a program with a cache', () => {
30+
const cache: Cache & { lagrangeBasisReadCount?: number } =
31+
Cache.FileSystem(__cacheDirname);
32+
const originalRead = cache.read;
33+
cache.lagrangeBasisReadCount = 0;
34+
cache.read = ({ persistentId, uniqueId, dataType }) => {
35+
if (persistentId.startsWith(LAGRANGE_BASIS_PREFIX)) {
36+
const readCount = cache.lagrangeBasisReadCount || 0;
37+
cache.lagrangeBasisReadCount = readCount + 1;
38+
}
39+
return originalRead({ persistentId, uniqueId, dataType } as any);
40+
};
41+
42+
before(async () => {
43+
await fs.mkdir(__cacheDirname, { recursive: true });
44+
});
45+
46+
after(async () => {
47+
await fs.rm(__cacheDirname, { recursive: true });
48+
});
49+
50+
/**
51+
* This test is a regression test for https://github.com/o1-labs/o1js/issues/1869
52+
* It ensures that the lagrange basis cache is accessed properly. If the file system cache is not
53+
* read during compile, that means that the lagrange basis was returned from WASM on the first attempt.
54+
*
55+
* This is not necessarily a problem. If the WASM code is updated such that we expect the LB to be
56+
* returned on the first try, and we explicitly skip the file system cache, then this test can be
57+
* safely removed. Otherwise, a failure here probably indicates a performance regression.
58+
*/
59+
it('should attempt to read lagrange basis from the cache during compile', async () => {
60+
cache.lagrangeBasisReadCount = 0;
61+
await exampleProgram.compile({ cache });
62+
expect(cache.lagrangeBasisReadCount).not.toBe(0);
63+
cache.lagrangeBasisReadCount = 0;
64+
});
65+
});

src/mina

Submodule mina updated 150 files

0 commit comments

Comments
 (0)