Skip to content

Fix BidirectionalMap leaving a stale reverse entry on key update#327403

Open
dsavy4 wants to merge 2 commits into
microsoft:mainfrom
dsavy4:fix/bidirectionalmap-stale-reverse
Open

Fix BidirectionalMap leaving a stale reverse entry on key update#327403
dsavy4 wants to merge 2 commits into
microsoft:mainfrom
dsavy4:fix/bidirectionalmap-stale-reverse

Conversation

@dsavy4

@dsavy4 dsavy4 commented Jul 24, 2026

Copy link
Copy Markdown

BidirectionalMap.set updates the forward map (_m1) and the reverse map (_m2), but when an existing key is reassigned it never removes the reverse entry for the key's previous value. That leaves a stale reverse mapping.

Repro:

const map = new BidirectionalMap();
map.set('a', 1);
map.set('a', 2);
map.get('a');    // 2
map.getKey(2);   // 'a'
map.getKey(1);   // 'a'  <- stale, should be undefined

getKey(1) still resolves to 'a' even though 'a' now maps to 2. This can surface wherever a key's value is updated, for example the marker decorations service (IMarker to decoration id) when a marker's decoration id changes.

The fix deletes the previous value's reverse entry when an existing key is reassigned. True one-to-one behavior is preserved, and a regression test covering key updates is added.

BidirectionalMap.set updated the forward map but never removed the
reverse mapping of the key's previous value. After map.set('a', 1)
followed by map.set('a', 2), a reverse lookup getKey(1) still returned
'a' even though 'a' now maps to 2.

Delete the previous value's reverse entry when an existing key is
reassigned. Added a regression test covering key updates.
Copilot AI review requested due to automatic review settings July 24, 2026 21:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes stale reverse mappings when a BidirectionalMap key is reassigned.

Changes:

  • Removes the previous reverse entry before updating a key.
  • Adds regression coverage for key reassignment.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/vs/base/common/map.ts Cleans up the old reverse mapping during updates.
src/vs/base/test/common/map.test.ts Tests forward and reverse lookups after reassignment.

Comment thread src/vs/base/common/map.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants