perf(ink): replace cache.clear() with LRU eviction in line-width cache#2322
Open
HUQIANTAO wants to merge 1 commit into
Open
perf(ink): replace cache.clear() with LRU eviction in line-width cache#2322HUQIANTAO wants to merge 1 commit into
HUQIANTAO wants to merge 1 commit into
Conversation
The old code called cache.clear() when the Map hit 4096 entries, discarding ALL cached widths at once. This created a sawtooth hit-rate pattern: slowly build cache → clear everything → cold-start 4096 misses → build again. Replace with LRU eviction via plain Map insertion-order semantics: - On hit: delete + re-set promotes the entry (survives next eviction). - On miss with full cache: evict only the single oldest entry. The renderer calls lineWidth ~100k times per frame with high temporal locality, so keeping 4095 warm entries instead of zero makes a measurable difference.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
lineWidthcache inpackages/ink/src/line-width-cache.tsusedcache.clear()when the Map reached 4096 entries, which discards ALL cached widths at once. This creates a sawtooth hit-rate pattern:cache.clear()drops every entryThe renderer calls
lineWidth()~100k times per frame with high temporal locality — the same lines appear across consecutive frames.Fix
Replace
cache.clear()with LRU eviction using plainMapinsertion-order semantics:delete+setpromotes the entry to the end, so it survives future eviction scansThis is a zero-dependency change — no new imports, no new data structures.
Mapalready provides stable insertion order per the ES2015 spec.Before/After
Verification
tsc --noEmit -p packages/ink/tsconfig.jsonpasseslineWidth()signature is unchanged