swiss ok, needs more benchmark

This commit is contained in:
Youmu Liu
2026-03-06 18:22:30 +08:00
parent 83997f64d0
commit f6e897ed06
70 changed files with 1889 additions and 3371 deletions
+52 -78
View File
@@ -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;
}
+58 -29
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="SwissTable"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -41,41 +41,51 @@ namespace StgSharp.Collections
{
private const int BucketCount = 1 << EntryMaskWidth;
private const int BucketSize = 256;
private const int EntryMaskWidth = 10; // 1024 entries total, 256 per bucket
public const int MaxCapacity = 1024 * BucketSize; // 256 entries per bucket, 1024 buckets
private const int BucketSize = 16;
private const uint CrcMagic = 0xffff9961; // Magic number for CRC32C, used in hash computation
private const int EntryMaskWidth = 16; // 65536 entries total, 16 per bucket
public const int MaxCapacity = BucketCount * BucketSize; // 256 entries per bucket, 1024 buckets
private readonly Entry* _entries;
private SwissTable(Entry* buffer)
private SwissTable(
Entry* buffer
)
{
_entries = buffer;
for (int i = 0; i < BucketCount; i++)
{
// Set all control bytes to 0xFF (empty)
Unsafe.InitBlock(&_entries[i].Bucket.Control, 0xFF, BucketSize);
}
}
public static SwissTable Create()
{
Entry* buffer = (Entry*)NativeMemory.AlignedAlloc((nuint)Marshal.SizeOf<Entry>(), 16);
Entry* buffer = (Entry*)NativeMemory.AlignedAlloc((nuint)Unsafe.SizeOf<Entry>(), 16);
for (int i = 0; i < BucketCount; i++)
{
// Set all control bytes to 0xFF (empty)
Unsafe.InitBlock(&(*buffer)[i].Control, 0xFF, BucketSize);
}
return new SwissTable(buffer);
}
public bool TryAddOrSet(nint key, nint value, out bool isNewKey)
public bool TryAddOrSet(
nint key,
nint value,
out bool isNewKey
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
ushort entry = (ushort)(hash & (BucketCount - 1));
Bucket* bucket = &(*_entries)[entry];
return IBucketScanner.Current.TrySet(key, hash, bucket, value, out isNewKey);
}
public bool TryGet(nint key, out nint value)
public bool TryGet(
nint key,
out nint value
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
ushort entry = (ushort)(hash & (BucketCount - 1));
Bucket* bucket = &(*_entries)[entry];
return IBucketScanner.Current.TryFind(key, hash, bucket, out value);
/*
* hash = CRC32 of key
@@ -86,11 +96,14 @@ namespace StgSharp.Collections
*/
}
public bool TryRemove(nint key, out nint value)
public bool TryRemove(
nint key,
out nint value
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
ushort entry = (ushort)(hash & (BucketCount - 1));
Bucket* bucket = &(*_entries)[entry];
return IBucketScanner.Current.TryRemove(key, hash, bucket, out value);
}
@@ -121,15 +134,31 @@ namespace StgSharp.Collections
static IBucketScanner Current { get; } = Create();
bool TryFind(nint key, uint Hash, Bucket* bucket, out nint value);
bool TryFind(
nint key,
uint Hash,
Bucket* bucket,
out nint value
);
unsafe bool TryRemove(nint key, uint hash, Bucket* bucket, out nint value);
unsafe bool TryRemove(
nint key,
uint hash,
Bucket* bucket,
out nint value
);
bool TrySet(nint key, uint hash, Bucket* bucket, nint value, out bool isNewKey);
bool TrySet(
nint key,
uint hash,
Bucket* bucket,
nint value,
out bool isNewKey
);
private static IBucketScanner Create()
{
if (X86Base.IsSupported)
if (Sse42.IsSupported)
{
return new BucketScannerX86();
} else if (ArmBase.IsSupported)