From 0183be558fcd081329ea90d1e38699d5a2924ab2 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Fri, 13 Nov 2026 05:16:19 +0100 Subject: [PATCH] [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. --- .../HashNode/_HashNode+Builder.swift | 4 +-- .../HashNode/_HashNode+Subtree Modify.swift | 5 ++- .../HashNode/_HashNode+Subtree Removals.swift | 8 +++-- .../TreeDictionary Tests.swift | 34 +++++++++++++++++++ .../TreeSet Tests.swift | 27 +++++++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Sources/HashTreeCollections/HashNode/_HashNode+Builder.swift b/Sources/HashTreeCollections/HashNode/_HashNode+Builder.swift index d7edfcdb..19a58b56 100644 --- a/Sources/HashTreeCollections/HashNode/_HashNode+Builder.swift +++ b/Sources/HashTreeCollections/HashNode/_HashNode+Builder.swift @@ -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): diff --git a/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Modify.swift b/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Modify.swift index 6b86e2a2..b5a1949c 100644 --- a/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Modify.swift +++ b/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Modify.swift @@ -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) diff --git a/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Removals.swift b/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Removals.swift index c0c40287..8cfee4c3 100644 --- a/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Removals.swift +++ b/Sources/HashTreeCollections/HashNode/_HashNode+Subtree Removals.swift @@ -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)) diff --git a/Tests/HashTreeCollectionsTests/TreeDictionary Tests.swift b/Tests/HashTreeCollectionsTests/TreeDictionary Tests.swift index 529a1d41..98864372 100644 --- a/Tests/HashTreeCollectionsTests/TreeDictionary Tests.swift +++ b/Tests/HashTreeCollectionsTests/TreeDictionary Tests.swift @@ -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._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 diff --git a/Tests/HashTreeCollectionsTests/TreeSet Tests.swift b/Tests/HashTreeCollectionsTests/TreeSet Tests.swift index 250da96d..7fb45d2b 100644 --- a/Tests/HashTreeCollectionsTests/TreeSet Tests.swift +++ b/Tests/HashTreeCollectionsTests/TreeSet Tests.swift @@ -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._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)