Bound measureText/wrapText caches (LRU) to stop unbounded growth - #987
Open
fudianchn wants to merge 1 commit into
Open
Bound measureText/wrapText caches (LRU) to stop unbounded growth#987fudianchn wants to merge 1 commit into
fudianchn wants to merge 1 commit into
Conversation
measureText() and wrapText() each kept a module-level cache with no eviction, so every distinct string an app ever rendered was retained for the lifetime of the process. Apps whose text changes over time (a streaming/typing indicator, a growing log, a clock, a progress line) thus leaked monotonically until OOM (vadimdemedes#986). Replace the unbounded Map/Record with a small bounded LRU cache (capped, refresh-on-read). Recomputation on eviction is cheap and recently-used entries stay hot, so normal apps are unaffected; only ever-growing distinct-string sets are now bounded. Closes vadimdemedes#986
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
measureText()andwrapText()each keep a module-level cache with no eviction, keyed by the full text:src/measure-text.ts—const cache = new Map()src/wrap-text.ts—const cache = {}Every distinct string an app ever renders is retained for the lifetime of the process, so any app whose text changes over time (a streaming/typing indicator, a growing log, a clock, a progress line) leaks monotonically until it OOMs (#986). Both caches are fed from the Yoga measure function on every layout pass, so entries accumulate even with
renderThrottleMs.Fix
Replace the unbounded
Map/Recordwith a small bounded LRU cache (src/lru-cache.ts):Normal apps (which measure/wrap a bounded set of strings) are unaffected; only ever-growing distinct-string sets are now bounded.
Verification
npm run typecheck✅ (ink'serasableSyntaxOnlytsconfig — note the helper uses an explicit field, not a constructor parameter property)npm run lint(xo) ✅ava✅ — addedtest/lru-cache.ts(eviction + refresh-on-read + missing-key recency); existingmeasure-text/flex-wrap(which exerciseswrapText) tests still pass.This matches the LRU-cap approach validated in the issue (costajhnt: capping the caches flat-lines the retained heap).
Closes #986