Skip to content

fix(lru-cache): include URL key length in getItemsLru size accounting#94942

Open
sleitor wants to merge 1 commit into
vercel:canaryfrom
sleitor:fix-94890
Open

fix(lru-cache): include URL key length in getItemsLru size accounting#94942
sleitor wants to merge 1 commit into
vercel:canaryfrom
sleitor:fix-94890

Conversation

@sleitor

@sleitor sleitor commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

What?

The calculateSize callback in LRUCache only 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?

  1. lru-cache.ts — Extend the calculateSize callback signature to pass key as a second argument. This is fully backward-compatible: all existing callers that only use value continue to work unchanged (TypeScript allows functions with fewer parameters to satisfy a type requiring more).

  2. filesystem.ts — Update the getItemsLru size function to include key.length for both positive cache entries and negative (null) entries, so the full URL string is counted toward the cache budget.

Fixes #94890

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getItemsLru size accounting omits URL key length, causing excessive heap retention in long-lived servers

1 participant