-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
70627fb
dfafac1
7c72861
cf7ddef
3c4a648
d41aa9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,8 +100,13 @@ export type MixedStyleDeclaration = Omit< | |
| [k in MixedSizeCSSPropertiesKeys]?: number | string; | ||
| }; | ||
|
|
||
| // Bounded to avoid unbounded growth on long-lived processors. | ||
| const INLINE_CSS_CACHE_LIMIT = 256; | ||
|
pataar marked this conversation as resolved.
Outdated
|
||
|
|
||
| export class CSSProcessor { | ||
| public readonly registry: CSSPropertiesValidationRegistry; | ||
| // LRU cache: same inline string compiles to the same CSSProcessedProps. | ||
| private inlineCssCache: Map<string, CSSProcessedProps> = new Map(); | ||
|
pataar marked this conversation as resolved.
Outdated
|
||
| constructor(userConfig?: Partial<CSSProcessorConfig>) { | ||
| const config = { | ||
| ...defaultCSSProcessorConfig, | ||
|
|
@@ -126,7 +131,24 @@ export class CSSProcessor { | |
| } | ||
|
|
||
| compileInlineCSS(inlineCSS: string): CSSProcessedProps { | ||
| const cache = this.inlineCssCache; | ||
| const cached = cache.get(inlineCSS); | ||
| if (cached !== undefined) { | ||
| // LRU touch: re-insert to mark as most recently used. | ||
| cache.delete(inlineCSS); | ||
| cache.set(inlineCSS, cached); | ||
| return cached; | ||
| } | ||
| const parseRun = new CSSInlineParseRun(inlineCSS, this.registry); | ||
| return parseRun.exec(); | ||
| const result = parseRun.exec(); | ||
| if (cache.size >= INLINE_CSS_CACHE_LIMIT) { | ||
| // Evict oldest (Map preserves insertion order). | ||
| const oldest = cache.keys().next().value; | ||
| if (oldest !== undefined) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this check protect us from?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could force it, but the check is cheap i guess
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, let's keep it that way, I just checked the behaviour in a JS environment and it sticked out |
||
| cache.delete(oldest); | ||
| } | ||
| } | ||
| cache.set(inlineCSS, result); | ||
| return result; | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.