fix(utils): bump BoundedMap access order on overwrite - #1153
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
set() on an existing key updated the value but left the key at its original insertion position, so a frequently-overwritten key stayed oldest and was evicted before newer entries. Delete-then-reinsert on overwrite to move the key to most-recent, matching the intended LRU eviction order. Adds a regression test that fails on the pre-fix code. Closes Nano-Collective#1143.
MsfPablo
requested review from
Avtrkrb,
akramcodez and
will-lamerton
as code owners
September 2, 2026 12:02
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
Fixes #1143.
BoundedMap.set(key, value)on an existing key updated the value but did not change the key's position in the underlyingMap's iteration order. Since eviction picks the oldest key (this.map.keys().next().value), a frequently-overwritten key stayed at the oldest position and was evicted before newer entries — the opposite of the intended LRU behavior.Change
In
set(), when the key already exists, delete it before re-inserting soMapmoves it to the most-recent (last-evicted) position:The existing "skip eviction when overwriting" guard (
!this.map.has(key)) is unchanged, so overwriting still never evicts a different entry — it now also stops evicting the overwritten key itself.Test
Adds a regression test (
overwrite bumps access order so the key is not evicted) that fails on the pre-fix code:maxSize: 2; inserta,b; overwritea; insertc.ais evicted (it kept the oldest position). ✘bis evicted,asurvives. ✔All 22
bounded-map.spec.tstests pass; the new test was verified to fail without the fix.Checklist
.changeset/fix-bounded-map-overwrite-order.md)test:ava,test:types, lint, and format clean on the touched files