swiss ok, needs more benchmark
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
@@ -39,29 +40,26 @@ namespace StgSharp.Collections
|
||||
|
||||
public bool TryFind(
|
||||
nint key,
|
||||
uint Hash,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
out nint value
|
||||
)
|
||||
{
|
||||
byte ctrl = (byte)(Hash >> 8);
|
||||
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F);
|
||||
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
|
||||
for (int i = 0; i < BucketSize; i += 16)
|
||||
Vector128<byte> bucketCtrlVector = Sse2.LoadVector128((byte*)&bucket->Control);
|
||||
Vector128<byte> cmpResult = Sse2.CompareEqual(ctrlVector, bucketCtrlVector);
|
||||
int mask = Sse2.MoveMask(cmpResult);
|
||||
while (mask != 0)
|
||||
{
|
||||
Vector128<byte> bucketCtrlVector = Sse2.LoadVector128(&bucket->Control.Control + i);
|
||||
Vector128<byte> cmpResult = Sse2.CompareEqual(ctrlVector, bucketCtrlVector);
|
||||
int mask = Sse2.MoveMask(cmpResult);
|
||||
while (mask != 0)
|
||||
int bitPos = BitOperations.TrailingZeroCount(mask);
|
||||
int index = bitPos;
|
||||
if (bucket->Keys[index] == key)
|
||||
{
|
||||
int bitPos = BitOperations.TrailingZeroCount(mask);
|
||||
int index = i + bitPos;
|
||||
if (bucket->Keys.value == key)
|
||||
{
|
||||
value = bucket->Values.value;
|
||||
return true;
|
||||
}
|
||||
mask &= mask - 1;
|
||||
value = bucket->Values[index];
|
||||
return true;
|
||||
}
|
||||
mask &= mask - 1;
|
||||
}
|
||||
value = default;
|
||||
return false;
|
||||
@@ -74,29 +72,25 @@ namespace StgSharp.Collections
|
||||
out nint value
|
||||
)
|
||||
{
|
||||
byte ctrl = (byte)(hash >> 8);
|
||||
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F);
|
||||
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
|
||||
|
||||
for (int i = 0; i < BucketSize; i += 16)
|
||||
{
|
||||
Vector128<byte> bucketCtrl = Sse2.LoadVector128(
|
||||
&bucket->Control[i]);
|
||||
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
|
||||
int matches = Sse2.MoveMask(matchMask);
|
||||
Vector128<byte> bucketCtrl = Sse2.LoadVector128((byte*)&bucket->Control);
|
||||
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
|
||||
int matches = Sse2.MoveMask(matchMask);
|
||||
|
||||
while (matches != 0)
|
||||
while (matches != 0)
|
||||
{
|
||||
int bitPos = BitOperations.TrailingZeroCount(matches);
|
||||
int index = bitPos;
|
||||
if (bucket->Keys[index] == key)
|
||||
{
|
||||
int bitPos = BitOperations.TrailingZeroCount(matches);
|
||||
int index = i + bitPos;
|
||||
if (bucket->Keys[index] == key)
|
||||
{
|
||||
value = bucket->Values[index];
|
||||
bucket->Control[index] = 0xFE; // mark as DELETED
|
||||
// no need to remove Key/Value
|
||||
return true;
|
||||
}
|
||||
matches &= matches - 1;
|
||||
value = bucket->Values[index];
|
||||
bucket->Control[index] = 0xFE; // mark as DELETED
|
||||
// no need to remove Key/Value
|
||||
return true;
|
||||
}
|
||||
matches &= matches - 1;
|
||||
}
|
||||
value = default;
|
||||
return false;
|
||||
@@ -110,64 +104,44 @@ namespace StgSharp.Collections
|
||||
out bool isNewKey
|
||||
)
|
||||
{
|
||||
byte ctrl = (byte)(hash >> 8); // H2: upper 8 bits (highest bit is naturally 0)
|
||||
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F); // H2: upper 8 bits (highest bit is naturally 0)
|
||||
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
|
||||
|
||||
int firstAvailableSlot = -1; // Record the first insertable slot
|
||||
|
||||
for (int i = 0; i < BucketSize; i += 16)
|
||||
Vector128<byte> bucketCtrl = Sse2.LoadVector128(
|
||||
(byte*)&bucket->Control);
|
||||
|
||||
// === Phase 1: Look up an existing key ===
|
||||
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
|
||||
int matches = Sse2.MoveMask(matchMask);
|
||||
|
||||
while (matches != 0)
|
||||
{
|
||||
Vector128<byte> bucketCtrl = Sse2.LoadVector128(
|
||||
&bucket->Control[i]);
|
||||
|
||||
// === Phase 1: Look up an existing key ===
|
||||
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
|
||||
int matches = Sse2.MoveMask(matchMask);
|
||||
|
||||
while (matches != 0)
|
||||
int bitPos = BitOperations.TrailingZeroCount(matches);
|
||||
int index = bitPos;
|
||||
if (bucket->Keys[index] == key)
|
||||
{
|
||||
int bitPos = BitOperations.TrailingZeroCount(matches);
|
||||
int index = i + bitPos;
|
||||
if (bucket->Keys[index] == key)
|
||||
{
|
||||
// Key already exists -> update value
|
||||
bucket->Values[index] = value;
|
||||
isNewKey = false;
|
||||
return true;
|
||||
}
|
||||
matches &= matches - 1;
|
||||
}
|
||||
|
||||
// === Phase 2: Track an available insertion slot ===
|
||||
if (firstAvailableSlot == -1)
|
||||
{
|
||||
// Find EMPTY (0xFF) or DELETED (0xFE)
|
||||
// Trick: bytes with high bit set are insertable
|
||||
int emptyOrDeleted = Sse2.MoveMask(bucketCtrl);
|
||||
|
||||
// MoveMask extracts the top bit of each byte
|
||||
// EMPTY/DELETED top bit = 1 -> mask bit = 1
|
||||
// OCCUPIED top bit = 0 -> mask bit = 0
|
||||
|
||||
if (emptyOrDeleted != 0)
|
||||
{
|
||||
int bitPos = BitOperations.TrailingZeroCount(emptyOrDeleted);
|
||||
firstAvailableSlot = i + bitPos;
|
||||
}
|
||||
// Key already exists -> update value
|
||||
bucket->Values[index] = value;
|
||||
isNewKey = false;
|
||||
return true;
|
||||
}
|
||||
matches &= matches - 1;
|
||||
}
|
||||
|
||||
// === Phase 3: Key not found, try to insert ===
|
||||
if (firstAvailableSlot == -1)
|
||||
// === Phase 2: Track an available insertion slot ===
|
||||
int emptyOrDeleted = Sse2.MoveMask(bucketCtrl);
|
||||
if (emptyOrDeleted == 0)
|
||||
{
|
||||
// Bucket is full; resize is required
|
||||
isNewKey = false;
|
||||
return false;
|
||||
return false; // bucket full
|
||||
}
|
||||
|
||||
bucket->Control[firstAvailableSlot] = ctrl;
|
||||
bucket->Keys[firstAvailableSlot] = key;
|
||||
bucket->Values[firstAvailableSlot] = value;
|
||||
int slot = BitOperations.TrailingZeroCount(emptyOrDeleted);
|
||||
bucket->Control[slot] = ctrl;
|
||||
bucket->Keys[slot] = key;
|
||||
bucket->Values[slot] = value;
|
||||
isNewKey = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user