-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(schema-compiler): Use LRU for CompilerCache to reduce memory usage #10247
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8155caf
feat(schema-compiler): Use LRU for CompilerCache to reduce memory usage
ovr c9b9e25
refactor(schema-compiler): use flat Map storage in QueryCache
ovr db3ba9b
perf(schema-compiler): optimize cache key computation
ovr f2a453a
chore: fix
ovr d80c6f6
chore: add test
ovr 0635199
chore: allow empty values caching
ovr 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
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
70 changes: 55 additions & 15 deletions
70
packages/cubejs-schema-compiler/src/adapter/QueryCache.ts
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 |
|---|---|---|
| @@ -1,26 +1,66 @@ | ||
| export class QueryCache { | ||
| private readonly storage: {}; | ||
| export interface QueryCacheInterface { | ||
| cache(key: any[], fn: Function): any; | ||
| } | ||
|
|
||
| /** | ||
| * Uses string concatenation (+=) instead of JSON.stringify for performance: | ||
| * - V8 optimizes += with ConsString (tree of string segments) | ||
| * - JSON.stringify builds a new flat string from scratch each time | ||
| * - For strings/numbers (common case), ~3x faster than JSON.stringify | ||
| * - Only falls back to JSON.stringify for objects/arrays | ||
| */ | ||
| export function fastComputeCacheKey(key: unknown): string { | ||
| if (Array.isArray(key)) { | ||
| let result = ''; | ||
|
|
||
| for (let i = 0; i < key.length; i++) { | ||
| if (i > 0) { | ||
| result += ':'; | ||
| } | ||
|
|
||
| if (typeof key[i] === 'string' || typeof key[i] === 'number') { | ||
| result += key[i]; | ||
| } else if (key[i] === undefined) { | ||
| result += 'undefined'; | ||
| } else if (key[i] === null) { | ||
| result += 'null'; | ||
| } else if (typeof key[i] === 'function') { | ||
| throw new TypeError(`Function is not allowed as subkey, passed as ${key[i]}`); | ||
| } else { | ||
| result += JSON.stringify(key[i]); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| if (typeof key === 'string') { | ||
| return key; | ||
| } | ||
|
|
||
| return JSON.stringify(key); | ||
| } | ||
|
|
||
| export class QueryCache implements QueryCacheInterface { | ||
| private readonly storage: Map<string, any>; | ||
|
|
||
| public constructor() { | ||
| this.storage = {}; | ||
| this.storage = new Map(); | ||
| } | ||
|
|
||
| /** | ||
| * @returns Returns the result of executing a function (Either call a function or take a value from the cache) | ||
| */ | ||
| public cache(key: any[], fn: Function): any { | ||
| let keyHolder = this.storage; | ||
| const { length } = key; | ||
| for (let i = 0; i < length - 1; i++) { | ||
| if (!keyHolder[key[i]]) { | ||
| keyHolder[key[i]] = {}; | ||
| } | ||
| keyHolder = keyHolder[key[i]]; | ||
| } | ||
| const lastKey = key[length - 1]; | ||
| if (!keyHolder[lastKey]) { | ||
| keyHolder[lastKey] = fn(); | ||
| const keyString = fastComputeCacheKey(key); | ||
|
|
||
| if (this.storage.has(keyString)) { | ||
| return this.storage.get(keyString); | ||
| } | ||
| return keyHolder[lastKey]; | ||
|
|
||
| const result = fn(); | ||
| this.storage.set(keyString, result); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
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
56 changes: 56 additions & 0 deletions
56
packages/cubejs-schema-compiler/test/unit/query-cache.test.ts
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,56 @@ | ||
| import { fastComputeCacheKey, QueryCache } from '../../src/adapter/QueryCache'; | ||
|
|
||
| describe('QueryCache', () => { | ||
| let cache: QueryCache; | ||
|
|
||
| beforeEach(() => { | ||
| cache = new QueryCache(); | ||
| }); | ||
|
|
||
| it('caches function result', () => { | ||
| let callCount = 0; | ||
| const fn = () => { | ||
| callCount++; | ||
| return 'result'; | ||
| }; | ||
|
|
||
| const result1 = cache.cache(['key1'], fn); | ||
| const result2 = cache.cache(['key1'], fn); | ||
|
|
||
| expect(result1).toBe('result'); | ||
| expect(result2).toBe('result'); | ||
| expect(callCount).toBe(1); | ||
| }); | ||
|
|
||
| it('differentiates between different keys', () => { | ||
| let callCount = 0; | ||
| const fn = () => { | ||
| callCount++; | ||
| return `result-${callCount}`; | ||
| }; | ||
|
|
||
| const result1 = cache.cache(['key1'], fn); | ||
| const result2 = cache.cache(['key2'], fn); | ||
|
|
||
| expect(result1).toBe('result-1'); | ||
| expect(result2).toBe('result-2'); | ||
| expect(callCount).toBe(2); | ||
| }); | ||
|
|
||
| it('fastComputeCacheKey', () => { | ||
| expect(fastComputeCacheKey([])).toBe(''); | ||
| expect(fastComputeCacheKey(['hello'])).toBe('hello'); | ||
| expect(fastComputeCacheKey(['hello', 'world'])).toBe('hello:world'); | ||
| expect(fastComputeCacheKey(['key', 123, 'value', 456])).toBe('key:123:value:456'); | ||
| expect(fastComputeCacheKey([{ a: 1 }])).toBe('{"a":1}'); | ||
| expect(fastComputeCacheKey([ | ||
| 'string', | ||
| 42, | ||
| null, | ||
| undefined, | ||
| { obj: 'value' }, | ||
| [1, 2], | ||
| true | ||
| ])).toBe('string:42:null:u:{"obj":"value"}:[1,2]:true'); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the reason (I assume, string interning) for writing such a nested cache, but I am certain that it's better to rewrite it. I've done
fastComputeCacheKeyand tested that it's usingConsString