Repository navigation
Conversation
maxEntrySize defaults to maxSize, but may be configured higher. In that case an item whose size exceeds maxSize passed the maxEntrySize guard, and #addItemSize tried to evict down to a negative budget. That evicted the item being added along with other entries, leaving calculatedSize negative so that later items could push the real total over maxSize. Treat size > maxSize as a no-op in #set, preserving any existing value and its size accounting, as documented for maxSize. Values larger than maxEntrySize keep the existing behavior. A background fetch placeholder for the key is dropped, since it is not a cached value.
|
Pushed empty commits ( The Windows matrix failures are unrelated to this change: they are the time-based assertion in |
|
Ok, but why do you have a maxSize that is smaller than maxEntrySize? That feels like it should just throw an error at creation time, because it's obviously a mistake. |
|
Good question. The constructor validates each limit independently (positive integer) but never compares them. When the configuration is set that way, the I kept the runtime guard rather than only rejecting the configuration because One behavior detail the guard preserves: the |
|
I'm still struggling to understand how this would ever happen in an actual program. Yes, technically these fields are mutable, but does anyone ever change them after being set at construction? Feels like a stretch. Have you observed it? Or is it strictly coming from analysis of the code? I'd prefer not to add runtime checks to this module for things that don't happen in reality. |
|
Strictly from analysis: I have not observed this in a real program, and there is no user report. I should not have leaned on the mutable-field argument; there is no observed code that changes these after construction. The only way in is an explicit configuration with That points at the constructor check you suggested, and I agree it is the better shape: catch the contradiction once instead of carrying a runtime branch for it. I can replace this PR with a narrow constructor validation (throw when an explicit |
|
Yeah, ok, then I think a guard in the constructor is best. Cheap and simple, and avoids the most likely problem. If someone explicitly sets it incorrectly later, they're doing a weird thing and can expect weird results 👍 Thanks for taking this on. |
maxSizedocuments that an item larger than the limit is not cached and does not evict other items (src/index.ts:915-917). WhenmaxEntrySizeis explicitly larger thanmaxSize, aset()withmaxSize < size <= maxEntrySizeslips past themaxEntrySizeguard into#addItemSize; the eviction loop then runs with a negative budget, evicts the item being measured and other items, and leavescalculatedSizenegative. Later entries start from that negative budget, so real bytes can exceedmaxSize.This adds a guard in
#setafter the existingmaxEntrySizecheck: whenmaxSizeis set and the entry size exceeds it, the write is a no-op — the previous value and accounting are retained, and a background-fetch placeholder is removed. ExplicitmaxEntrySizerejections,maxSize: 0,size === maxSize, and the update path are unchanged.Regression coverage in
test/size-calculation.test.tscovers a new oversized entry and an oversized update; both fail on 2056b03 withcalculatedSize does not equal sum of sizes.npx ubqpasses 487 tests; oxlint and prettier are clean.