[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:
Karoy Lorentey committed 2026-03-10 23:06:08 -07:00
1 parent 2a53ad50b0
commit e69d570c59
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(