fix(effect): TMap.remove/removeAll clears entire bucket on hash collision#6233
Open
mvanhorn wants to merge 1 commit into
Open
Conversation
…sion The fixed code in tMap.ts had two related bugs: 1. The destructuring of Chunk.partition was reversed (toRemove/toRetain swapped) 2. The predicate compared entry[1] (value) to key instead of entry[0] (key) Together these caused removing one key with a hash collision to drop every other entry in the same bucket. Added regression tests using a CollidingKey type that forces every entry into one bucket. Fixes Effect-TS#6225
🦋 Changeset detectedLatest commit: 2b936e5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 36 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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
TMap.removeandTMap.removeAllwere clearing every entry in a bucket whenever two keys hashed to the same slot. The fix is a 2-line change inpackages/effect/src/internal/stm/tMap.ts.Why this matters
Bucket-level data loss on hash collision is a correctness bug for any user storing entries whose key types collide (custom
Hashimplementations, value types with low-entropy hashes). The bug was reported in #6225 with a minimal repro showing three entries in a single bucket where removing one drops the other two. The fixed code has two related issues:Chunk.partitionreturns[predicate-true, predicate-false]. The destructuringconst [toRemove, toRetain] = ...was backwards.Equal.equals(entry[1], key), comparing the entry's value against the key instead of the entry's key (entry[0]).Together these meant the code was retaining the matched key and removing every other key in the bucket.
Changes
packages/effect/src/internal/stm/tMap.ts- swap the destructuring to[toRetain, toRemove]and compareentry[0](key) against the key in bothremoveandremoveAll.packages/effect/test/TMap.test.ts- add two regression tests using aCollidingKeytype whoseHashalways returns 1, forcing every entry into one bucket..changeset/fix-tmap-remove-colliding-keys.md- patch-level changeset.Testing
pnpm test --filter=effect -- TMap.test(Vitest). The newremove - preserves colliding keysandremoveAll - preserves colliding keystests fail onmainand pass with the fix.Fixes #6225