mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
[BasicContainers] Implement shortcut for insertions
We don’t need to calculate the probe length of the item currently occupying the new item’s ideal bucket — it cannot possibly be lower than the new item’s. This spares one hash operation during insertions that result in a hash collision.
This commit is contained in:
1 file changed
+7
@@ -50,6 +50,13 @@ extension _HTable {
|
||||
#else
|
||||
var b = idealBucket(forHashValue: hashValue)
|
||||
var probeLength = 1
|
||||
if bitmap.isOccupied(b) {
|
||||
// Shortcut: In the first iteration, we're at the minimum possible probe
|
||||
// length, so there is no possible way we'd replace the current item.
|
||||
// There is no point hashing it.
|
||||
self.wrapBucket(after: &b)
|
||||
probeLength &+= 1
|
||||
}
|
||||
while bitmap.isOccupied(b) {
|
||||
let oldHashValue = hashGenerator(b)
|
||||
let oldProbeLength = self.probeLength(
|
||||
|
||||
Reference in new issue
Block a user