mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
[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:
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))
|
||||
|
||||
Reference in new issue
Block a user