-
Notifications
You must be signed in to change notification settings - Fork 14
perf(css-processor): cache compiled inline CSS strings #39
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
Open
pataar
wants to merge
4
commits into
native-html:main
Choose a base branch
from
pataar:perf/inline-css-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70627fb
perf(css-processor): cache compiled inline CSS strings
pataar dfafac1
style(css-processor): compact cache comments
pataar 7c72861
refactor(css-processor): gate inline-CSS cache behind opt-in flag
pataar cf7ddef
fix(css-processor): don't evict LRU when replacing an existing key
pataar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { createLRUCache } from '../lruCache'; | ||
|
|
||
| describe('createLRUCache', () => { | ||
| it('returns undefined for a missing key', () => { | ||
| const cache = createLRUCache<string, number>(8); | ||
| expect(cache.get('missing')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns the value previously set under a key', () => { | ||
| const cache = createLRUCache<string, number>(8); | ||
| cache.set('a', 1); | ||
| expect(cache.get('a')).toBe(1); | ||
| }); | ||
|
|
||
| it('evicts the least-recently-used entry once maxSize is reached', () => { | ||
| const cache = createLRUCache<string, number>(2); | ||
| cache.set('a', 1); | ||
| cache.set('b', 2); | ||
| cache.set('c', 3); // evicts 'a' | ||
| expect(cache.get('a')).toBeUndefined(); | ||
| expect(cache.get('b')).toBe(2); | ||
| expect(cache.get('c')).toBe(3); | ||
| }); | ||
|
|
||
| it('touches an entry on get so it is no longer the LRU', () => { | ||
| const cache = createLRUCache<string, number>(2); | ||
| cache.set('a', 1); | ||
| cache.set('b', 2); | ||
| cache.get('a'); // touch 'a' — 'b' is now LRU | ||
| cache.set('c', 3); // evicts 'b' | ||
| expect(cache.get('a')).toBe(1); | ||
| expect(cache.get('b')).toBeUndefined(); | ||
| expect(cache.get('c')).toBe(3); | ||
| }); | ||
|
|
||
| it('updates an existing key in place without evicting other entries', () => { | ||
| const cache = createLRUCache<string, number>(2); | ||
| cache.set('a', 1); | ||
| cache.set('b', 2); | ||
| cache.set('b', 99); // replace — must not evict 'a' | ||
| expect(cache.get('a')).toBe(1); | ||
| expect(cache.get('b')).toBe(99); | ||
| }); | ||
|
|
||
| it('evicts the previous entry on every new key when maxSize is 1', () => { | ||
| const cache = createLRUCache<string, number>(1); | ||
| cache.set('a', 1); | ||
| cache.set('b', 2); | ||
| expect(cache.get('a')).toBeUndefined(); | ||
| expect(cache.get('b')).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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Bounded LRU cache: reads touch the key as most-recent, writes evict the | ||
| * oldest once `maxSize` is reached. Avoids re-parsing repeated inline CSS | ||
| * without unbounded memory growth. | ||
| */ | ||
| export interface LRUCache<K, V extends {}> { | ||
| get(key: K): V | undefined; | ||
| set(key: K, value: V): void; | ||
| } | ||
|
|
||
| export function createLRUCache<K, V extends {}>( | ||
| maxSize: number | ||
| ): LRUCache<K, V> { | ||
| const map = new Map<K, V>(); | ||
| return { | ||
| get(key) { | ||
| const value = map.get(key); | ||
| if (value !== undefined) { | ||
| // LRU touch: move to most-recent end. | ||
| map.delete(key); | ||
| map.set(key, value); | ||
| } | ||
| return value; | ||
| }, | ||
| set(key, value) { | ||
| // Only evict when adding a new key; replacing an existing key updates | ||
| // in place and must not free an unrelated slot. | ||
| if (map.size >= maxSize && !map.has(key)) { | ||
| const oldest = map.keys().next().value; | ||
|
jsamr marked this conversation as resolved.
|
||
| if (oldest !== undefined) { | ||
| map.delete(oldest); | ||
| } | ||
| } | ||
| map.set(key, value); | ||
| } | ||
| }; | ||
| } | ||
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.