-
Notifications
You must be signed in to change notification settings - Fork 1
feat(): lru memoization for js worker #466
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7c3fede
feat(): lru memoization for js worker
e11sy 712c106
chore(): add crypto hash params
e11sy ed72270
chore(): cover with tests
e11sy c51fa74
chore(): cover with memoize entire beautifyBacktrace method
e11sy 53fd9e2
imp(): tests and types
e11sy c96b935
chore(): lint fix
e11sy 4b8b529
Update workers/javascript/package.json
e11sy 02a12e8
Update workers/javascript/src/index.ts
e11sy 4e67af6
chore(): update test
e11sy 7b97367
chore(): lint fix
e11sy 1baf710
chore(): remove test duplicate
e11sy 67cd19f
test(): cover with different arguments case
e11sy 639e632
chore(): test memoize util
e11sy e9c0f14
chore(): lint fix
e11sy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* eslint-disable | ||
| no-unused-vars, | ||
| @typescript-eslint/explicit-function-return-type, | ||
| @typescript-eslint/no-unused-vars-experimental, | ||
| jsdoc/require-param-description | ||
| */ | ||
| /** | ||
| * Ignore eslint jsdoc rules for mocked class | ||
| * Ignore eslint unused vars rule for decorator | ||
| */ | ||
|
|
||
| import { memoize } from './index'; | ||
| import Crypto from '../utils/crypto'; | ||
|
|
||
| describe('memoize decorator — per-test inline classes', () => { | ||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| jest.restoreAllMocks(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should memoize return value with concat strategy across several calls', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
|
|
||
| @memoize({ strategy: 'concat', ttl: 60_000, max: 50 }) | ||
| public async run(a: number, b: string) { | ||
| this.calls += 1; | ||
| return `${a}-${b}`; | ||
| } | ||
| } | ||
|
|
||
| const sample = new Sample(); | ||
|
|
||
| /** | ||
| * First call should memoize the method | ||
| */ | ||
| expect(await sample.run(1, 'x')).toBe('1-x'); | ||
| /** | ||
| * In this case | ||
| */ | ||
| expect(await sample.run(1, 'x')).toBe('1-x'); | ||
| expect(await sample.run(1, 'x')).toBe('1-x'); | ||
|
|
||
| expect(sample.calls).toBe(1); | ||
| }); | ||
|
|
||
| it('should memoize return value with set of arguments with concat strategy across several calls', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
|
|
||
| @memoize({ strategy: 'concat' }) | ||
| public async run(a: unknown, b: unknown) { | ||
| this.calls += 1; | ||
| return `${String(a)}|${String(b)}`; | ||
| } | ||
| } | ||
|
|
||
| const sample = new Sample(); | ||
|
|
||
| /** | ||
| * Fill the memoization cache with values | ||
| */ | ||
| await sample.run(1, 'a'); | ||
| await sample.run(2, 'a'); | ||
| await sample.run(1, 'b'); | ||
| await sample.run(true, false); | ||
| await sample.run(undefined, null); | ||
|
|
||
| expect(sample.calls).toBe(5); | ||
|
|
||
| /** | ||
| * Those calls should not call the original method, they should return from memoize | ||
| */ | ||
| await sample.run(1, 'a'); | ||
| await sample.run(2, 'a'); | ||
| await sample.run(1, 'b'); | ||
| await sample.run(true, false); | ||
| await sample.run(undefined, null); | ||
|
|
||
| expect(sample.calls).toBe(5); | ||
| }); | ||
|
|
||
| it('should memoize return value for stringified objects across several calls', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
| @memoize({ strategy: 'concat' }) | ||
| public async run(x: unknown, y: unknown) { | ||
| this.calls += 1; | ||
| return 'ok'; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
| const o1 = { a: 1 }; | ||
| const o2 = { b: 2 }; | ||
|
|
||
| await sample.run(o1, o2); | ||
| await sample.run(o1, o2); | ||
|
|
||
| expect(sample.calls).toBe(1); | ||
| }); | ||
|
|
||
| it('should memoize return value for method with non-default arguments (NaN, Infinity, -0, Symbol, Date, RegExp) still cache same-args', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
| @memoize({ strategy: 'concat' }) | ||
| public async run(...args: unknown[]) { | ||
| this.calls += 1; | ||
| return args.map(String).join(','); | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| const sym = Symbol('t'); | ||
| const d = new Date('2020-01-01T00:00:00Z'); | ||
| const re = /a/i; | ||
|
|
||
| const first = await sample.run(NaN, Infinity, -0, sym, d, re); | ||
| const second = await sample.run(NaN, Infinity, -0, sym, d, re); | ||
|
|
||
| expect(second).toBe(first); | ||
| expect(sample.calls).toBe(1); | ||
| }); | ||
|
|
||
| it('should call crypto hash with blake2b512 algo and base64url digest, should memoize return value with hash strategy', async () => { | ||
| const hashSpy = jest.spyOn(Crypto, 'hash'); | ||
|
|
||
| class Sample { | ||
| public calls = 0; | ||
| @memoize({ strategy: 'hash' }) | ||
| public async run(...args: unknown[]) { | ||
| this.calls += 1; | ||
| return 'ok'; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| await sample.run({a: 1}, undefined, 0); | ||
| await sample.run({a: 1}, undefined, 0); | ||
|
|
||
| expect(hashSpy).toHaveBeenCalledWith([{a: 1}, undefined, 0], 'blake2b512', 'base64url'); | ||
| expect(sample.calls).toBe(1); | ||
| }); | ||
|
|
||
| it('should not memoize return value with hash strategy and different arguments', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
| @memoize({ strategy: 'hash' }) | ||
| public async run(...args: unknown[]) { | ||
| this.calls += 1; | ||
| return 'ok'; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| await sample.run({ v: 1 }); | ||
| await sample.run({ v: 2 }); | ||
| await sample.run({ v: 3 }); | ||
|
|
||
| expect(sample.calls).toBe(3); | ||
| }); | ||
|
|
||
| it('should memoize return value with hash strategy across several calls with same args', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
| @memoize({ strategy: 'hash' }) | ||
| public async run(arg: unknown) { | ||
| this.calls += 1; | ||
| return 'ok'; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| await sample.run({ a: 1 }); | ||
| await sample.run({ a: 1 }); | ||
|
|
||
| expect(sample.calls).toBe(1); | ||
| }); | ||
|
|
||
| it('should memoize return value exactly for passed ttl millis', async () => { | ||
| jest.resetModules(); | ||
| jest.useFakeTimers({ legacyFakeTimers: false }); | ||
| jest.setSystemTime(new Date('2025-01-01T00:00:00Z')); | ||
|
|
||
| const { memoize: memoizeWithMockedTimers } = await import('../memoize/index'); | ||
|
|
||
| class Sample { | ||
| public calls = 0; | ||
| @memoizeWithMockedTimers({ strategy: 'concat', ttl: 1_000 }) | ||
| public async run(x: string) { | ||
| this.calls += 1; | ||
| return x; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| await sample.run('k1'); | ||
| expect(sample.calls).toBe(1); | ||
|
|
||
| /** | ||
| * Skip time beyond the ttl | ||
| */ | ||
| jest.advanceTimersByTime(1_001); | ||
|
|
||
| await sample.run('k1'); | ||
| expect(sample.calls).toBe(2); | ||
|
|
||
| }); | ||
|
|
||
| it('error calls should never be momized', async () => { | ||
| class Sample { | ||
| public calls = 0; | ||
| @memoize() | ||
| public async run(x: number) { | ||
| this.calls += 1; | ||
| if (x === 1) throw new Error('boom'); | ||
| return x * 2; | ||
| } | ||
| } | ||
| const sample = new Sample(); | ||
|
|
||
| /** | ||
| * Compute with throw | ||
| */ | ||
| await expect(sample.run(1)).rejects.toThrow('boom'); | ||
| await expect(sample.run(1)).rejects.toThrow('boom'); | ||
| expect(sample.calls).toBe(2); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import LRUCache from 'lru-cache'; | ||
| import Crypto from '../utils/crypto'; | ||
|
|
||
| /** | ||
| * Pick the strategy of cache key form | ||
| * It could be concatenated list of arguments like 'projectId:eventId' | ||
| * Or it could be hashed json object — blake2b512 algorithm | ||
| */ | ||
| export type MemoizeKeyStrategy = 'concat' | 'hash'; | ||
|
|
||
| /** | ||
| * Options of the memoize decorator | ||
| */ | ||
| export interface MemoizeOptions { | ||
| /** | ||
| * Max number of values stored in LRU cache at the same time | ||
| */ | ||
| max?: number; | ||
|
|
||
| /** | ||
| * TTL in milliseconds | ||
| */ | ||
| ttl?: number; | ||
|
|
||
| /** | ||
| * Strategy for key generation | ||
| */ | ||
| strategy?: MemoizeKeyStrategy; | ||
| } | ||
|
|
||
| /** | ||
| * Async-only, per-method LRU-backed memoization decorator. | ||
| * Cache persists for the lifetime of the class instance (e.g. worker). | ||
| * | ||
| * @param options | ||
| */ | ||
| export function memoize(options: MemoizeOptions = {}): MethodDecorator { | ||
| /* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
| const { | ||
| max = 50, | ||
| ttl = 1000 * 60 * 30, | ||
| strategy = 'concat', | ||
| } = options; | ||
| /* eslint-enable */ | ||
|
|
||
| return function ( | ||
| _target, | ||
| propertyKey, | ||
| descriptor: PropertyDescriptor | ||
| ): PropertyDescriptor { | ||
| const originalMethod = descriptor.value; | ||
|
|
||
| if (typeof originalMethod !== 'function') { | ||
| throw new Error('@Memoize can only decorate methods'); | ||
| } | ||
|
|
||
| descriptor.value = async function (...args: unknown[]): Promise<unknown> { | ||
| /** | ||
| * Create a cache key for each decorated method | ||
| */ | ||
| const cacheKey = `memoizeCache:${String(propertyKey)}`; | ||
|
|
||
| /** | ||
| * Create a new cache if it does not exists yet (for certain function) | ||
| */ | ||
| const cache: LRUCache<string, any> = this[cacheKey] ??= new LRUCache<string, any>({ | ||
| max, | ||
| maxAge: ttl, | ||
| }); | ||
|
|
||
| const key = strategy === 'hash' | ||
| ? Crypto.hash(args, 'blake2b512', 'base64url') | ||
| : args.map((arg) => JSON.stringify(arg)).join('__ARG_JOIN__'); | ||
|
|
||
| /** | ||
| * Check if we have a cached result | ||
| */ | ||
| const cachedResult = cache.get(key); | ||
|
|
||
| if (cachedResult !== undefined) { | ||
| return cachedResult; | ||
| } | ||
|
|
||
| try { | ||
| const result = await originalMethod.apply(this, args); | ||
|
|
||
| cache.set(key, result); | ||
|
|
||
| return result; | ||
| } catch (err) { | ||
| cache.del(key); | ||
| throw err; | ||
| } | ||
| }; | ||
|
|
||
| return descriptor; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
neSpecc marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.