Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add memoize decorator #8976

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions yarn-project/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/archiver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec-faucet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/bb-prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/circuit-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/circuits.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/cli-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/entrypoints/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"./collection": "./dest/collection/index.js",
"./config": "./dest/config/index.js",
"./crypto": "./dest/crypto/index.js",
"./decorators": "./dest/decorators/index.js",
"./error": "./dest/error/index.js",
"./eth-address": "./dest/eth-address/index.js",
"./eth-signature": "./dest/eth-signature/index.js",
Expand Down Expand Up @@ -68,6 +69,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down Expand Up @@ -156,4 +160,4 @@
"engines": {
"node": ">=18"
}
}
}
1 change: 1 addition & 0 deletions yarn-project/foundation/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './memoize.js';
104 changes: 104 additions & 0 deletions yarn-project/foundation/src/decorators/memoize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { sleep } from '../sleep/index.js';
import { memoize } from './memoize.js';

class Memoized {
public readonly counters: Record<string, number> = {};

@memoize
voidPublic() {
this.inc('voidPublic');
}

@memoize
synchPublic() {
this.inc('synchPublic');
return this.counters['synchPublic'] + 10;
}

callSyncPrivate() {
this.synchPrivate();
this.synchPrivate();
}

@memoize
private synchPrivate() {
this.inc('synchPrivate');
return this.counters['synchPrivate'] + 20;
}

@memoize
async asyncPublic() {
this.inc('asyncPublic');
await sleep(1);
return this.counters['asyncPublic'] + 30;
}

async callAsyncPrivate() {
await this.asyncPrivate();
await this.asyncPrivate();
}

@memoize
private async asyncPrivate() {
this.inc('asyncPrivate');
await sleep(1);
return this.counters['asyncPrivate'] + 40;
}

@memoize
async asyncVoid() {
this.inc('asyncVoid');
await sleep(1);
}

private inc(name: string) {
if (!(name in this.counters)) {
this.counters[name] = 0;
}
this.counters[name]++;
}
}

describe('memoize', () => {
let memoized: Memoized;

beforeEach(() => {
memoized = new Memoized();
});

it('memoizes public void', () => {
memoized.voidPublic();
memoized.voidPublic();
expect(memoized.counters['voidPublic']).toBe(1);
});

it('memoizes public synchronous', () => {
expect(memoized.synchPublic()).toBe(11);
expect(memoized.synchPublic()).toBe(11);
expect(memoized.counters['synchPublic']).toBe(1);
});

it('memoizes private synchronous', () => {
memoized.callSyncPrivate();
memoized.callSyncPrivate();
expect(memoized.counters['synchPrivate']).toBe(1);
});

it('memoizes public asynchronous', async () => {
expect(await memoized.asyncPublic()).toBe(31);
expect(await memoized.asyncPublic()).toBe(31);
expect(memoized.counters['asyncPublic']).toBe(1);
});

it('memoizes private asynchronous', async () => {
await memoized.callAsyncPrivate();
await memoized.callAsyncPrivate();
expect(memoized.counters['asyncPrivate']).toBe(1);
});

it('memoizes void asynchronous', async () => {
await memoized.asyncVoid();
await memoized.asyncVoid();
expect(memoized.counters['asyncVoid']).toBe(1);
});
});
11 changes: 11 additions & 0 deletions yarn-project/foundation/src/decorators/memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function memoize<This extends object, Result>(fn: () => Result, context: ClassMethodDecoratorContext) {
return function (this: This) {
const key = `__${String(context.name)}_value`;
const thisWithKey = this as { [key: string]: Result };
if (!(key in this)) {
const result = fn.call(this);
thisWithKey[key] = result;
}
return thisWithKey[key];
};
}
3 changes: 3 additions & 0 deletions yarn-project/ivc-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/key-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/kv-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/noir-contracts.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/noir-protocol-circuits-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/p2p-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/package.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
Loading
Loading