Skip to content

Commit 8bd98d7

Browse files
committed
Add HashMap enhancements and benchmarking updates
Added `Count` and `Capacity` properties to the `HashMap` class to expose its size and capacity. Introduced a `Clear` method to reset the hash map. Simplified `Main` in `Program.cs` by removing `Utils.QSort` usage. Added a `HashMap<int, int>` instance to `UnsafeDictionaryBenchmark` for performance testing. Updated `Setup` in `UnsafeDictionaryBenchmark` to clear the `HashMap` during initialization. Added a new `AddHashMap` benchmark method to measure `HashMap` performance, including automatic clearing when size exceeds 100,000.
1 parent 01c3829 commit 8bd98d7

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

Hexa.NET.Utilities/HashMap.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public static int Find(int hash, Tag tag, TKey key, in Columns cols, int capacit
223223
private int size;
224224
private nint equals;
225225

226+
public int Count => size;
227+
228+
public int Capacity => capacity;
229+
226230
private static void RehashMoveRow(in Columns src, in Columns dst, int newCapacity)
227231
{
228232
var tag = *src.tags;
@@ -360,6 +364,17 @@ public void Release()
360364
capacity = 0;
361365
size = 0;
362366
}
367+
368+
public void Clear()
369+
{
370+
if (tags == null)
371+
{
372+
return;
373+
}
374+
var tagsInBytes = (nuint)(capacity * sizeof(Tag));
375+
NativeMemory.Clear(tags, tagsInBytes);
376+
size = 0;
377+
}
363378
}
364379

365380

TestApp/Program.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ public class Program
88
{
99
private static unsafe void Main(string[] args)
1010
{
11-
int[] array = { 10, 7, 8, 9, 1, 5 };
12-
int length = array.Length;
13-
14-
fixed (int* ptr = array)
15-
{
16-
Utils.QSort(ptr, length, (a, b) => a.CompareTo(b)); // Default integer comparison
17-
}
18-
19-
//var summary = BenchmarkRunner.Run<UnsafeDictionaryBenchmark>();
11+
var summary = BenchmarkRunner.Run<UnsafeDictionaryBenchmark>();
2012
//var summary = BenchmarkRunner.Run<UnsafeListBenchmark>();
2113
/*
2214
UnsafeDictionaryBenchmark benchmark = new();
@@ -89,6 +81,7 @@ public unsafe class UnsafeDictionaryBenchmark
8981
// worst case scenario benchmark using linear keys where the hash code of int is always the value of itself.
9082

9183
private UnsafeDictionary<int, int> _unsafeDict; // about 29% faster and saves 7 bytes per entry and has a way lower Std-dev.
84+
private HashMap<int, int> _hashMap;
9285
private Dictionary<int, int> _managedDict = new();
9386
private int iterationIndex = 0;
9487

@@ -97,6 +90,7 @@ public void Setup()
9790
{
9891
iterationIndex = 0;
9992
_unsafeDict.Clear();
93+
_hashMap.Clear();
10094
_managedDict.Clear();
10195
}
10296

@@ -110,6 +104,16 @@ public void AddUnsafeDictionary()
110104
}
111105
}
112106

107+
[Benchmark]
108+
public void AddHashMap()
109+
{
110+
_hashMap.Add(iterationIndex++, 2); // Add elements to the managed list
111+
if (_hashMap.Count > 100000)
112+
{
113+
_hashMap.Clear();
114+
}
115+
}
116+
113117
[Benchmark(Baseline = true)]
114118
public void AddManagedDictionary()
115119
{

0 commit comments

Comments
 (0)