[HashTreeCollections] Don't read hash buckets below the last hash level

Two keys with equal hashes whose hash also agrees with a third key on
every level except the last end up in a collision node that sits below
the last hash level. Removing one of the colliding keys (remove,
removeValue, subscript setter/_modify to nil, subtracting, filter,
symmetricDifference, ...) computed `hash[level]` for that node, which
trips the `assert(!level.isAtBottom)` in `_Hash.subscript` in debug
builds. Collision nodes don't use buckets, and parents replace the
bucket of an inlined child item with their own, so release builds were
not affected.

Skip the bucket lookup at the bottom level, the same way lookups in
`_findInCollision` already do.
This commit is contained in:
0z5a committed 2026-11-13 08:53:29 +01:00
1 parent 88754449ab
commit 0183be558f
5 files changed
+72 -6

No files matched your search

@@ -178,9 +178,9 @@ extension _HashNode.Builder {
assert(level == self.level)
switch kind {
case .empty:
kind = .item(newItem, at: hash[level])
kind = .item(newItem, at: level.isAtBottom ? .invalid : hash[level])
case .item(let oldItem, at: let bucket):
assert(hash[level] == bucket)
assert(level.isAtBottom || hash[level] == bucket)
let node = _HashNode._collisionNode(hash, oldItem, newItem)
kind = .collisionNode(node)
case .collisionNode(var node):
@@ -156,7 +156,10 @@ extension _HashNode {
assert(isUnique())
if level == path.level {
return unsafe _removeItemFromUniqueLeafNode(
level, at: hash[level], path.currentItemSlot, by: { _ in }
level,
at: level.isAtBottom ? .invalid : hash[level],
path.currentItemSlot,
by: { _ in }
).remainder
}
let slot = unsafe path.childSlot(at: level)
@@ -30,7 +30,8 @@ extension _HashNode {
return (r.removed, remainder)
}
guard let r = find(level, key, hash) else { return nil }
let bucket = hash[level]
// Collision nodes below the last hash level do not use buckets.
let bucket = level.isAtBottom ? .invalid : hash[level]
guard r.descend else {
let r = unsafe _removeItemFromUniqueLeafNode(level, at: bucket, r.slot) {
unsafe $0.move()
@@ -56,7 +57,7 @@ extension _HashNode {
_ level: _HashLevel, _ key: Key, _ hash: _Hash
) -> (removed: Element, replacement: Builder)? {
guard let r = find(level, key, hash) else { return nil }
let bucket = hash[level]
let bucket = level.isAtBottom ? .invalid : hash[level]
guard r.descend else {
return unsafe _removingItemFromLeaf(level, at: bucket, r.slot)
}
@@ -157,7 +158,8 @@ extension _HashNode {
if unsafe $0.collisionCount == 2 {
// Node will evaporate
let remainder = _HashSlot(1 &- slot.value)
let bucket = unsafe $0.collisionHash[level]
let bucket =
unsafe level.isAtBottom ? .invalid : $0.collisionHash[level]
return (
removed: unsafe $0[item: slot],
replacement: unsafe .item(level, $0[item: remainder], at: bucket))
@@ -931,6 +931,40 @@ class TreeDictionaryTests: CollectionTestCase {
}
}
func test_collision_node_at_maximum_depth() {
// `a1` and `a2` have the same hash, and `b` only differs from them on the
// last hash level, so `a1` and `a2` end up in a collision node that is
// below the last level that has a bucket in the hash.
let depth = TreeDictionary<Int, Int>._maxDepth
let prefix = String(repeating: "0", count: depth - 1)
let a1 = RawCollider(1, Hash(prefix + "1")!)
let a2 = RawCollider(2, Hash(prefix + "1")!)
let b = RawCollider(3, Hash(prefix + "0")!)
let expected: [(key: RawCollider, value: Int)] = [(a1, 1), (b, 3)]
withEvery("isShared", in: [false, true]) { isShared in
var d: TreeDictionary = [a1: 1, a2: 2, b: 3]
withHiddenCopies(if: isShared, of: &d) { d in
expectEqual(d.removeValue(forKey: a2), 2)
d._invariantCheck()
expectEqualDictionaries(d, expected)
}
d = [a1: 1, a2: 2, b: 3]
withHiddenCopies(if: isShared, of: &d) { d in
mutate(&d[a2]) { value in
expectEqual(value, 2)
value = nil
}
d._invariantCheck()
expectEqualDictionaries(d, expected)
}
}
let d: TreeDictionary = [a1: 1, a2: 2, b: 3]
expectEqualDictionaries(d.filter { $0.key != a2 }, expected)
}
func test_subscript_modify_remove_fixtures_removeAll() {
withEachFixture { fixture in
withEvery("isShared", in: [false, true]) { isShared in
@@ -602,6 +602,33 @@ class TreeSetTests: CollectionTestCase {
expectTrue(union.contains(b2))
}
func test_collision_node_at_maximum_depth() {
// `a1` and `a2` have the same hash, and `b` only differs from them on the
// last hash level, so `a1` and `a2` end up in a collision node that is
// below the last level that has a bucket in the hash.
let depth = TreeSet<Int>._maxDepth
let prefix = String(repeating: "0", count: depth - 1)
let a1 = RawCollider(1, Hash(prefix + "1")!)
let a2 = RawCollider(2, Hash(prefix + "1")!)
let b = RawCollider(3, Hash(prefix + "0")!)
withEvery("isShared", in: [false, true]) { isShared in
var s: TreeSet = [a1, a2, b]
withHiddenCopies(if: isShared, of: &s) { s in
expectEqual(s.remove(a2), a2)
s._invariantCheck()
expectEqualSets(s, [a1, b])
}
}
let s: TreeSet = [a1, a2, b]
expectEqualSets(s.subtracting([a2]), [a1, b])
expectEqualSets(s.subtracting([a2] as TreeSet), [a1, b])
expectEqualSets(s.filter { $0 != a2 }, [a1, b])
expectEqualSets(s.intersection([a1, b] as TreeSet), [a1, b])
expectEqualSets(s.symmetricDifference([a2] as TreeSet), [a1, b])
}
func test_symmetricDifference_exhaustive() {
withEverySubset("a", of: testItems) { a in
let x = TreeSet(a)