Skip to content

fix(core): PHPMNT-394 Normalise maxSize so invalid values cannot disable memoization - #68

Draft
TomA-R wants to merge 1 commit into
masterfrom
phpmnt-394-normalise-max-size
Draft

fix(core): PHPMNT-394 Normalise maxSize so invalid values cannot disable memoization#68
TomA-R wants to merge 1 commit into
masterfrom
phpmnt-394-normalise-max-size

Conversation

@TomA-R

@TomA-R TomA-R commented Aug 13, 2026

Copy link
Copy Markdown
Member

Jira: PHPMNT-394

What/Why?

A fractional or negative maxSize silently turns memoization off entirely:

const memoized = memoize(fn, { maxSize: 0.5 });
memoized(1, 1); // fn runs
memoized(1, 1); // fn runs again — every call is a miss

With maxSize: 0.5, the LRU set (size 1 after the first call) always "exceeds" the limit, so the key that was just issued is immediately expired — every call recomputes and the consumer gets no signal that anything is wrong. Negative and NaN values behave the same way, and Infinity has the opposite problem: every key is tracked for expiry but none ever expires, so the tracking set grows forever. The maxSize type is number, so a computed value can hit any of these without a type error.

Fix: the resolver now floors fractions and treats anything below 1 or non-finite as "no limit" (Number.isFinite(maxSize) ? Math.max(0, Math.floor(maxSize)) : 0). So 2.7 behaves as 2, while 0.5, -1, NaN, and Infinity behave as the default unlimited cache — degraded gracefully instead of silently misbehaving.

Rollout/Rollback

Merge / revert

Testing

Tests added at the resolver level (below-one values, non-finite values, fractional flooring) and the memoize level.

…ble memoization

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@TomA-R
TomA-R force-pushed the phpmnt-394-normalise-max-size branch from 5b74725 to 467795b Compare August 13, 2026 07:32
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.

1 participant