Skip to content

Commit 1d9711b

Browse files
authored
chore: Add memoize decorator (#8976)
Adds a new decorator for memoizing results. Extracted from #8614.
1 parent 96b6cfc commit 1d9711b

File tree

42 files changed

+238
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+238
-1
lines changed

yarn-project/accounts/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
"parser": {
5353
"syntax": "typescript",
5454
"decorators": true
55+
},
56+
"transform": {
57+
"decoratorVersion": "2022-03"
5558
}
5659
}
5760
}

yarn-project/archiver/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"parser": {
4545
"syntax": "typescript",
4646
"decorators": true
47+
},
48+
"transform": {
49+
"decoratorVersion": "2022-03"
4750
}
4851
}
4952
}

yarn-project/aztec-faucet/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"parser": {
3838
"syntax": "typescript",
3939
"decorators": true
40+
},
41+
"transform": {
42+
"decoratorVersion": "2022-03"
4043
}
4144
}
4245
}

yarn-project/aztec-node/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"parser": {
3838
"syntax": "typescript",
3939
"decorators": true
40+
},
41+
"transform": {
42+
"decoratorVersion": "2022-03"
4043
}
4144
}
4245
}

yarn-project/aztec.js/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"parser": {
5656
"syntax": "typescript",
5757
"decorators": true
58+
},
59+
"transform": {
60+
"decoratorVersion": "2022-03"
5861
}
5962
}
6063
}

yarn-project/aztec/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
"parser": {
9595
"syntax": "typescript",
9696
"decorators": true
97+
},
98+
"transform": {
99+
"decoratorVersion": "2022-03"
97100
}
98101
}
99102
}

yarn-project/bb-prover/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"parser": {
4242
"syntax": "typescript",
4343
"decorators": true
44+
},
45+
"transform": {
46+
"decoratorVersion": "2022-03"
4447
}
4548
}
4649
}

yarn-project/bot/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"parser": {
3232
"syntax": "typescript",
3333
"decorators": true
34+
},
35+
"transform": {
36+
"decoratorVersion": "2022-03"
3437
}
3538
}
3639
}

yarn-project/builder/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
"parser": {
4848
"syntax": "typescript",
4949
"decorators": true
50+
},
51+
"transform": {
52+
"decoratorVersion": "2022-03"
5053
}
5154
}
5255
}

yarn-project/circuit-types/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"parser": {
4444
"syntax": "typescript",
4545
"decorators": true
46+
},
47+
"transform": {
48+
"decoratorVersion": "2022-03"
4649
}
4750
}
4851
}

yarn-project/circuits.js/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
"parser": {
7878
"syntax": "typescript",
7979
"decorators": true
80+
},
81+
"transform": {
82+
"decoratorVersion": "2022-03"
8083
}
8184
}
8285
}

yarn-project/cli-wallet/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
"parser": {
4949
"syntax": "typescript",
5050
"decorators": true
51+
},
52+
"transform": {
53+
"decoratorVersion": "2022-03"
5154
}
5255
}
5356
}

yarn-project/cli/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
"parser": {
4949
"syntax": "typescript",
5050
"decorators": true
51+
},
52+
"transform": {
53+
"decoratorVersion": "2022-03"
5154
}
5255
}
5356
}

yarn-project/end-to-end/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
"parser": {
132132
"syntax": "typescript",
133133
"decorators": true
134+
},
135+
"transform": {
136+
"decoratorVersion": "2022-03"
134137
}
135138
}
136139
}

yarn-project/entrypoints/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"parser": {
4242
"syntax": "typescript",
4343
"decorators": true
44+
},
45+
"transform": {
46+
"decoratorVersion": "2022-03"
4447
}
4548
}
4649
}

yarn-project/ethereum/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"parser": {
5858
"syntax": "typescript",
5959
"decorators": true
60+
},
61+
"transform": {
62+
"decoratorVersion": "2022-03"
6063
}
6164
}
6265
}

yarn-project/foundation/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"./collection": "./dest/collection/index.js",
1616
"./config": "./dest/config/index.js",
1717
"./crypto": "./dest/crypto/index.js",
18+
"./decorators": "./dest/decorators/index.js",
1819
"./error": "./dest/error/index.js",
1920
"./eth-address": "./dest/eth-address/index.js",
2021
"./eth-signature": "./dest/eth-signature/index.js",
@@ -68,6 +69,9 @@
6869
"parser": {
6970
"syntax": "typescript",
7071
"decorators": true
72+
},
73+
"transform": {
74+
"decoratorVersion": "2022-03"
7175
}
7276
}
7377
}
@@ -156,4 +160,4 @@
156160
"engines": {
157161
"node": ">=18"
158162
}
159-
}
163+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './memoize.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { sleep } from '../sleep/index.js';
2+
import { memoize } from './memoize.js';
3+
4+
class Memoized {
5+
public readonly counters: Record<string, number> = {};
6+
7+
@memoize
8+
voidPublic() {
9+
this.inc('voidPublic');
10+
}
11+
12+
@memoize
13+
synchPublic() {
14+
this.inc('synchPublic');
15+
return this.counters['synchPublic'] + 10;
16+
}
17+
18+
callSyncPrivate() {
19+
this.synchPrivate();
20+
this.synchPrivate();
21+
}
22+
23+
@memoize
24+
private synchPrivate() {
25+
this.inc('synchPrivate');
26+
return this.counters['synchPrivate'] + 20;
27+
}
28+
29+
@memoize
30+
async asyncPublic() {
31+
this.inc('asyncPublic');
32+
await sleep(1);
33+
return this.counters['asyncPublic'] + 30;
34+
}
35+
36+
async callAsyncPrivate() {
37+
await this.asyncPrivate();
38+
await this.asyncPrivate();
39+
}
40+
41+
@memoize
42+
private async asyncPrivate() {
43+
this.inc('asyncPrivate');
44+
await sleep(1);
45+
return this.counters['asyncPrivate'] + 40;
46+
}
47+
48+
@memoize
49+
async asyncVoid() {
50+
this.inc('asyncVoid');
51+
await sleep(1);
52+
}
53+
54+
private inc(name: string) {
55+
if (!(name in this.counters)) {
56+
this.counters[name] = 0;
57+
}
58+
this.counters[name]++;
59+
}
60+
}
61+
62+
describe('memoize', () => {
63+
let memoized: Memoized;
64+
65+
beforeEach(() => {
66+
memoized = new Memoized();
67+
});
68+
69+
it('memoizes public void', () => {
70+
memoized.voidPublic();
71+
memoized.voidPublic();
72+
expect(memoized.counters['voidPublic']).toBe(1);
73+
});
74+
75+
it('memoizes public synchronous', () => {
76+
expect(memoized.synchPublic()).toBe(11);
77+
expect(memoized.synchPublic()).toBe(11);
78+
expect(memoized.counters['synchPublic']).toBe(1);
79+
});
80+
81+
it('memoizes private synchronous', () => {
82+
memoized.callSyncPrivate();
83+
memoized.callSyncPrivate();
84+
expect(memoized.counters['synchPrivate']).toBe(1);
85+
});
86+
87+
it('memoizes public asynchronous', async () => {
88+
expect(await memoized.asyncPublic()).toBe(31);
89+
expect(await memoized.asyncPublic()).toBe(31);
90+
expect(memoized.counters['asyncPublic']).toBe(1);
91+
});
92+
93+
it('memoizes private asynchronous', async () => {
94+
await memoized.callAsyncPrivate();
95+
await memoized.callAsyncPrivate();
96+
expect(memoized.counters['asyncPrivate']).toBe(1);
97+
});
98+
99+
it('memoizes void asynchronous', async () => {
100+
await memoized.asyncVoid();
101+
await memoized.asyncVoid();
102+
expect(memoized.counters['asyncVoid']).toBe(1);
103+
});
104+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function memoize<This extends object, Result>(fn: () => Result, context: ClassMethodDecoratorContext) {
2+
return function (this: This) {
3+
const key = `__${String(context.name)}_value`;
4+
const thisWithKey = this as { [key: string]: Result };
5+
if (!(key in this)) {
6+
const result = fn.call(this);
7+
thisWithKey[key] = result;
8+
}
9+
return thisWithKey[key];
10+
};
11+
}

yarn-project/ivc-integration/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
"parser": {
4040
"syntax": "typescript",
4141
"decorators": true
42+
},
43+
"transform": {
44+
"decoratorVersion": "2022-03"
4245
}
4346
}
4447
}

yarn-project/key-store/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"parser": {
3636
"syntax": "typescript",
3737
"decorators": true
38+
},
39+
"transform": {
40+
"decoratorVersion": "2022-03"
3841
}
3942
}
4043
}

yarn-project/kv-store/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"parser": {
3535
"syntax": "typescript",
3636
"decorators": true
37+
},
38+
"transform": {
39+
"decoratorVersion": "2022-03"
3740
}
3841
}
3942
}

yarn-project/merkle-tree/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"parser": {
3838
"syntax": "typescript",
3939
"decorators": true
40+
},
41+
"transform": {
42+
"decoratorVersion": "2022-03"
4043
}
4144
}
4245
}

yarn-project/noir-contracts.js/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"parser": {
3636
"syntax": "typescript",
3737
"decorators": true
38+
},
39+
"transform": {
40+
"decoratorVersion": "2022-03"
3841
}
3942
}
4043
}

yarn-project/noir-protocol-circuits-types/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
"parser": {
4141
"syntax": "typescript",
4242
"decorators": true
43+
},
44+
"transform": {
45+
"decoratorVersion": "2022-03"
4346
}
4447
}
4548
}

yarn-project/p2p-bootstrap/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
"parser": {
6262
"syntax": "typescript",
6363
"decorators": true
64+
},
65+
"transform": {
66+
"decoratorVersion": "2022-03"
6467
}
6568
}
6669
}

yarn-project/p2p/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"parser": {
3838
"syntax": "typescript",
3939
"decorators": true
40+
},
41+
"transform": {
42+
"decoratorVersion": "2022-03"
4043
}
4144
}
4245
}

yarn-project/package.common.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"parser": {
2929
"syntax": "typescript",
3030
"decorators": true
31+
},
32+
"transform": {
33+
"decoratorVersion": "2022-03"
3134
}
3235
}
3336
}

0 commit comments

Comments
 (0)