swiss ok, needs more benchmark
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user