diff --git a/src/Concurrent/CMap.cs b/src/Concurrent/CMap.cs index 2019332..9fb2eb7 100644 --- a/src/Concurrent/CMap.cs +++ b/src/Concurrent/CMap.cs @@ -640,31 +640,55 @@ private static ref T Find(T[] array, uint index) #endregion + /// + /// Represents an entry in the hash table. + /// [StructLayout(LayoutKind.Sequential)] - [DebuggerDisplay("key = {Key}; value = {Value}; meta {Meta};")] + [DebuggerDisplay("key = {Key}; value = {Value}; meta = {Meta};")] internal struct Entry { + /// + /// The state of the entry, used for locking. 0 indicates unlocked, 1 indicates locked. + /// internal byte state; + + /// + /// Metadata associated with the entry. This is used to indicate the state of the entry, + /// such as whether it is empty, a tombstone, in-progress, or not empty (H2 hash) + /// internal sbyte Meta; + + /// + /// The key of the entry. + /// internal TKey Key; + + /// + /// The value of the entry. + /// internal TValue Value; /// /// Enters a critical section by acquiring a lock. Ensures thread safety. + /// This method uses a spin lock with exponential backoff to reduce contention. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Enter() { - int spinCount = 1; + int spinCount = 1; // Initialize spin count for exponential backoff while (true) { + // Attempt to acquire the lock by setting the state to 1 if it is currently 0 if (Interlocked.CompareExchange(ref state, 1, 0) == 0) { - return; + return; // Lock acquired successfully, exit the method } + // Perform a spin wait to allow other threads to progress Thread.SpinWait(spinCount); + + // Increment the spin count exponentially, but cap it to prevent excessive delays if (spinCount < 1024) { spinCount *= 2; @@ -683,27 +707,87 @@ internal class Table { #region Fields + /// + /// The bit shift value used in hash calculation to determine bucket index. + /// private byte _shift = 32; + + /// + /// The bitmask value used for secondary hash calculation. + /// private const sbyte _bitmask = (1 << 6) - 1; + + /// + /// A constant value based on the golden ratio, used for hash calculation. + /// private const uint _goldenRatio = 0x9E3779B9; + + /// + /// The size of each group of entries for migration purposes. + /// internal uint _groupSize; + + /// + /// The total number of groups in the table. + /// internal int _depleted; + + /// + /// The current index of the group being processed during migration. + /// private uint _groupIndex; #endregion #region Properties + /// + /// The array of entries in the hash table. + /// public Entry[] Entries; + + /// + /// Length of the table minus one, used for efficient modulus operations. + /// public uint LengthMinusOne; + + /// + /// Threshold for resizing the table, based on the load factor. + /// public uint Threshold; + + /// + /// The current length of the table. + /// public uint Length; + + /// + /// The number of groups minus one, used for group indexing. + /// private uint _groupsMinusOne; + + /// + /// Counter for the number of depleted groups during migration. + /// internal int _depletedCounter; + + /// + /// The maximum distance to jump during quadratic probing. + /// public byte MaxJumpDistance { get; internal set; } #endregion + #region Constructor + + /// + /// Creates a new table with the specified length and load factor. + /// + /// The length of the table. + /// The load factor of the table. + + #endregion + /// /// Creates a new table with the specified length and load factor. /// diff --git a/src/Faster.Map.csproj b/src/Faster.Map.csproj index 2a22f21..8c503fe 100644 --- a/src/Faster.Map.csproj +++ b/src/Faster.Map.csproj @@ -10,16 +10,16 @@ Add robinhood hashmap and quadratic hashmap https://github.com/Wsm2110/Faster.Map - 5.0.4 - 5.0.4 + 6.0.0 + 6.0.0 Fastest .net hashmap - 5.0.4 + 6.0.0 True Cutting-edge NuGet package offering high-performance hash map implementations leveraging SIMD (Single Instruction, Multiple Data) instructions. This innovative library combines the efficiency of SIMD operations with advanced hashing techniques https://github.com/Wsm2110/Faster.Map git MIT - Hashmap Hashtable Dictionary Faster.map performance, simd, robinhood hashing, swisstable, absl + Hashmap Hashtable Dictionary Faster.map performance, concurrency, simd, robinhood hashing, swisstable, absl