Skip to content

Commit

Permalink
Add mirrored control bytes to enable seamless wraparound in probing
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Wsm2110 committed Oct 29, 2024
1 parent 310063f commit 969fa00
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/DenseMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, byte>(ref bitPos));

// Check if the entry's key matches the specified key using the equality comparer.
if (_comparer.Equals(entry.Key, key))
{
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -615,7 +612,6 @@ public bool Update(TKey key, TValue value)
}
}


/// <summary>
/// Removes a key and value from the map.
/// Example:
Expand Down Expand Up @@ -920,7 +916,7 @@ private void Resize()
var bitPos = BitOperations.TrailingZeroCount(mask);
index += Unsafe.As<int, uint>(ref bitPos);

Find(_controlBytes, index) = h2;
SetCtrl(i, h2);
Find(_entries, index) = entry;
break;
}
Expand Down

0 comments on commit 969fa00

Please sign in to comment.