Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-tmap-remove-colliding-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix TMap.remove and removeAll erroneously clearing entire bucket on hash collision.
4 changes: 2 additions & 2 deletions packages/effect/src/internal/stm/tMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const remove = dual<
const buckets = tRef.unsafeGet(self.tBuckets, journal)
const index = indexOf(key, buckets.chunk.length)
const bucket = tRef.unsafeGet(buckets.chunk[index], journal)
const [toRemove, toRetain] = Chunk.partition(bucket, (entry) => Equal.equals(entry[1], key))
const [toRetain, toRemove] = Chunk.partition(bucket, (entry) => Equal.equals(entry[0], key))
if (Chunk.isNonEmpty(toRemove)) {
const currentSize = tRef.unsafeGet(self.tSize, journal)
tRef.unsafeSet(buckets.chunk[index], toRetain, journal)
Expand All @@ -325,7 +325,7 @@ export const removeAll = dual<
const buckets = tRef.unsafeGet(self.tBuckets, journal)
const index = indexOf(next.value, buckets.chunk.length)
const bucket = tRef.unsafeGet(buckets.chunk[index], journal)
const [toRemove, toRetain] = Chunk.partition(bucket, (entry) => Equal.equals(next.value)(entry[0]))
const [toRetain, toRemove] = Chunk.partition(bucket, (entry) => Equal.equals(next.value)(entry[0]))
if (Chunk.isNonEmpty(toRemove)) {
const currentSize = tRef.unsafeGet(self.tSize, journal)
tRef.unsafeSet(buckets.chunk[index], toRetain, journal)
Expand Down
54 changes: 54 additions & 0 deletions packages/effect/test/TMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class HashContainer implements Equal.Equal {
}
}

class CollidingKey implements Equal.Equal {
constructor(readonly id: string) {}
[Hash.symbol](): number {
return 1
}
[Equal.symbol](that: unknown): boolean {
return that instanceof CollidingKey && this.id === that.id
}
}

const mapEntriesArb: fc.Arbitrary<Array<readonly [string, number]>> = fc.uniqueArray(fc.char())
.chain((keys) =>
fc.uniqueArray(fc.integer())
Expand Down Expand Up @@ -190,6 +200,50 @@ describe("TMap", () => {
assertNone(result)
}))

it.effect("remove - preserves colliding keys", () =>
Effect.gen(function*() {
const a = new CollidingKey("a")
const b = new CollidingKey("b")
const c = new CollidingKey("c")
const transaction = STM.gen(function*() {
const map = yield* (TMap.make([a, 1], [b, 2], [c, 3]))
yield* (pipe(map, TMap.remove(b)))
return yield* (STM.all({
a: pipe(map, TMap.get(a)),
b: pipe(map, TMap.get(b)),
c: pipe(map, TMap.get(c)),
size: TMap.size(map)
}))
})
const result = yield* (STM.commit(transaction))
assertSome(result.a, 1)
assertNone(result.b)
assertSome(result.c, 3)
strictEqual(result.size, 2)
}))

it.effect("removeAll - preserves colliding keys", () =>
Effect.gen(function*() {
const a = new CollidingKey("a")
const b = new CollidingKey("b")
const c = new CollidingKey("c")
const transaction = STM.gen(function*() {
const map = yield* (TMap.make([a, 1], [b, 2], [c, 3]))
yield* (pipe(map, TMap.removeAll([b])))
return yield* (STM.all({
a: pipe(map, TMap.get(a)),
b: pipe(map, TMap.get(b)),
c: pipe(map, TMap.get(c)),
size: TMap.size(map)
}))
})
const result = yield* (STM.commit(transaction))
assertSome(result.a, 1)
assertNone(result.b)
assertSome(result.c, 3)
strictEqual(result.size, 2)
}))

it.effect("removeIf", () =>
Effect.gen(function*() {
const transaction = STM.gen(function*() {
Expand Down
Loading