From e69d570c59fcf5747fe074127ecd0f3041013128 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 10 Mar 2026 18:06:06 -0700 Subject: [PATCH] [BasicContainers] Implement shortcut for insertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/BasicContainers/HashTable/_HTable+Insert.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/BasicContainers/HashTable/_HTable+Insert.swift b/Sources/BasicContainers/HashTable/_HTable+Insert.swift index 9fa036c6..57a12d6a 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Insert.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Insert.swift @@ -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(