mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
[BasicContainers] Don't resolve the hole before shrinking in UniqueDictionary
Resolves the FIXME added in #836: when removing an entry through `updateValue(forKey:with:)` triggers shrinking, there is no need to restore the hash table's invariants first, since resizing rehashes every item anyway. Factor the part of `RigidDictionary._punchHole(at:)` that doesn't touch the value slot into `_punchHole(withMovedValueAt:)`, and use it on the shrink path of `UniqueDictionary._removeEntry(withMovedValueAt:)`, which now mirrors `_removeValue(at:)`.
This commit is contained in:
1 parent
9b328ecb94
commit
9a699a1952
2 files changed
+22
-8
No files matched your search
@@ -60,11 +60,23 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable {
|
||||
@inlinable
|
||||
@unsafe
|
||||
package mutating func _punchHole(at bucket: _Bucket) -> Value {
|
||||
assert(unsafe self._keys._table.isOccupied(bucket))
|
||||
let oldValue = unsafe self._valuePtr(at: bucket).move()
|
||||
unsafe _punchHole(withMovedValueAt: bucket)
|
||||
return oldValue
|
||||
}
|
||||
|
||||
/// Like `_punchHole(at:)`, but assumes the value slot at `bucket` has
|
||||
/// already been moved out (e.g., by `updateValue(forKey:with:)`).
|
||||
///
|
||||
/// This operation is intended to be used just before resizing the table.
|
||||
@inlinable
|
||||
@unsafe
|
||||
package mutating func _punchHole(withMovedValueAt bucket: _Bucket) {
|
||||
assert(unsafe self._keys._table.isOccupied(bucket))
|
||||
unsafe self._keys._table.createHole(at: bucket)
|
||||
unsafe self._keyPtr(at: bucket).deinitialize(count: 1)
|
||||
|
||||
let oldValue = unsafe self._valuePtr(at: bucket).move()
|
||||
let keys = unsafe self._keys._members.unsafelyUnwrapped
|
||||
unsafe self._keys._table.finalizeHole(
|
||||
at: bucket,
|
||||
@@ -72,7 +84,6 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable {
|
||||
unsafe (keys + $1.offset).initialize(to: (keys + $0.offset).move())
|
||||
unsafe (_values + $1.offset).initialize(to: (_values + $0.offset).move())
|
||||
})
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@inlinable
|
||||
|
||||
@@ -41,13 +41,16 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable {
|
||||
@inlinable
|
||||
@unsafe
|
||||
package mutating func _removeEntry(withMovedValueAt bucket: _Bucket) {
|
||||
let shrink = self.count <= _HTable.minimumCapacity(forScale: self._scale)
|
||||
unsafe _storage._keyPtr(at: bucket).deinitialize(count: 1)
|
||||
// FIXME: Avoid fully resolving the hole when `shrink` is true -- see `_punchHole`
|
||||
unsafe _storage._resolveHole(at: bucket)
|
||||
if shrink {
|
||||
_resize(minimumCapacity: self.count)
|
||||
guard self.count <= _HTable.minimumCapacity(forScale: self._scale) else {
|
||||
unsafe _storage._keyPtr(at: bucket).deinitialize(count: 1)
|
||||
unsafe _storage._resolveHole(at: bucket)
|
||||
return
|
||||
}
|
||||
|
||||
// Shrink storage. There is no need to restore hash table invariants first,
|
||||
// as resizing rehashes every item anyway.
|
||||
unsafe _storage._punchHole(withMovedValueAt: bucket)
|
||||
_resize(minimumCapacity: self.count)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
|
||||
Reference in new issue
Block a user