From 969fa00d3199a5e51104a41429e3c7412a33df05 Mon Sep 17 00:00:00 2001 From: Wiljan Ruizendaal Date: Tue, 29 Oct 2024 14:03:47 +0100 Subject: [PATCH] Add mirrored control bytes to enable seamless wraparound in probing - Introduced mirrored control bytes at the end of the control byte array to allow seamless wraparound during group probing. - This mirrors the first 16 bytes (GroupSize) of the control array at the end, enabling efficient SIMD-based probing across boundaries without additional boundary checks or wraparound logic. - This design improvement allows continuous group probing by treating the control byte array as circular, thereby enhancing performance, reducing branching, and simplifying the probe sequence. --- src/DenseMap.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/DenseMap.cs b/src/DenseMap.cs index 2d4b4de..1171d3b 100644 --- a/src/DenseMap.cs +++ b/src/DenseMap.cs @@ -394,16 +394,13 @@ public bool Get(TKey key, out TValue value) // Compare the target vector (hashed key) with the loaded source vector to find matches. // `ExtractMostSignificantBits()` returns a mask where each bit set indicates a match. var mask = Vector128.Equals(target, source).ExtractMostSignificantBits(); - // Process any matches indicated by the mask. while (mask != 0) { // Get the position of the first set bit in the mask (indicating a match). var bitPos = BitOperations.TrailingZeroCount(mask); - // Retrieve the entry corresponding to the matched bit position within the map's entries. var entry = Find(_entries, index + Unsafe.As(ref bitPos)); - // Check if the entry's key matches the specified key using the equality comparer. if (_comparer.Equals(entry.Key, key)) { @@ -516,7 +513,7 @@ public ref TValue GetValueRefOrAddDefault(TKey key) // Set the key for the located entry to the specified `key`. entry.Key = key; // Set the control byte for the entry at position `i` to `h2` to mark it as occupied. - Find(_controlBytes, i) = h2; + SetCtrl(i, h2); // Increment the total count of entries in the hash table. Count++; @@ -615,7 +612,6 @@ public bool Update(TKey key, TValue value) } } - /// /// Removes a key and value from the map. /// Example: @@ -920,7 +916,7 @@ private void Resize() var bitPos = BitOperations.TrailingZeroCount(mask); index += Unsafe.As(ref bitPos); - Find(_controlBytes, index) = h2; + SetCtrl(i, h2); Find(_entries, index) = entry; break; }