fix(lru-cache): include URL key length in getItemsLru size accounting#94942
Open
sleitor wants to merge 1 commit into
Open
fix(lru-cache): include URL key length in getItemsLru size accounting#94942sleitor wants to merge 1 commit into
sleitor wants to merge 1 commit into
Conversation
The calculateSize callback in LRUCache only received the value, omitting the key (URL string). In the filesystem router, this meant that the URL key – which can be long in path-heavy apps – was never counted toward the cache's memory budget, causing under-eviction and excessive heap retention on long-lived servers. Changes: - Extend LRUCache.calculateSize signature to pass key as the second argument (backward-compatible: existing callbacks that ignore it continue to work unchanged). - Update getItemsLru in filesystem.ts to include key.length in the returned size for both positive and negative (null) cache entries. Fixes vercel#94890
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.
What?
The
calculateSizecallback inLRUCacheonly received the cached value, omitting the key (URL string). In the filesystem router, the URL key – which can be long in path-heavy apps – was never counted toward the cache's memory budget.Why?
This causes under-eviction: the LRU cache believes entries are smaller than they really are, so it evicts too infrequently, leading to excessive heap retention in long-lived servers handling many unique URLs.
How?
lru-cache.ts— Extend thecalculateSizecallback signature to passkeyas a second argument. This is fully backward-compatible: all existing callers that only usevaluecontinue to work unchanged (TypeScript allows functions with fewer parameters to satisfy a type requiring more).filesystem.ts— Update thegetItemsLrusize function to includekey.lengthfor both positive cache entries and negative (null) entries, so the full URL string is counted toward the cache budget.Fixes #94890