Skip to content

Commit

Permalink
move IEq from SmallMap to the upper level, merge some stuff from FEC
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Oct 1, 2024
1 parent 2f74d70 commit 83175ac
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 154 deletions.
2 changes: 1 addition & 1 deletion playground/Experiments/FEC_ImTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public interface IEq<K>
int GetHashCode(K key);
}

/// <summary>Default comparer using the `object.GetHashCode` and `object.Equals` oveloads</summary>
/// <summary>Default comparer using the `object.GetHashCode` and `object.Equals` overloads</summary>
public struct DefaultEq<K> : IEq<K>
{
/// <inheritdoc />
Expand Down
52 changes: 2 additions & 50 deletions playground/Experiments/FHashMap9.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,6 @@
#endif
namespace ImTools.Experiments;

/// <summary>Default comparer using the `object.GetHashCode` and `object.Equals` oveloads</summary>
public struct DefaultEq<K> : IEqualityComparer<K>
{
/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public bool Equals(K x, K y) => x.Equals(y);

/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public int GetHashCode(K obj) => obj.GetHashCode();
}

/// <summary>Uses the `object.GetHashCode` and `object.ReferenceEquals`</summary>
public struct RefEq<K> : IEqualityComparer<K> where K : class
{
/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public bool Equals(K x, K y) => ReferenceEquals(x, y);

/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public int GetHashCode(K obj) => obj.GetHashCode();
}

/// <summary>Uses the integer itself as hash code and `==` for equality</summary>
public struct IntEq : IEqualityComparer<int>
{
/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public bool Equals(int x, int y) => x == y;

/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public int GetHashCode(int obj) => obj;
}

/// <summary>Uses Fibonacci hashing by multiplying the integer on the factor derived from the GoldenRatio</summary>
public struct GoldenIntEq : IEqualityComparer<int>
{
/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public bool Equals(int x, int y) => x == y;

/// <inheritdoc />
[MethodImpl((MethodImplOptions)256)]
public int GetHashCode(int obj) => (int)(obj * FHashMap91.GoldenRatio32) >>> FHashMap91.MaxProbeBits;
}

public static class FHashMap9Diagnostics
{
/// <summary>Converts the packed hashes and indexes array into the human readable info</summary>
Expand Down Expand Up @@ -383,8 +335,8 @@ public void AddOrUpdate(K key, V value)
var nextHWithoutProbes = h & HashAndIndexMask;
h = (probes << ProbeCountShift) | hWithoutProbes;
#if DEBUG
if (probes > MaxProbes)
Debug.WriteLine($"[AddOrUpdate] MaxProbes {MaxProbes = probes}");
if (probes > MaxProbes)
Debug.WriteLine($"[AddOrUpdate] MaxProbes {MaxProbes = probes}");
#endif
hWithoutProbes = nextHWithoutProbes;
probes = hProbes;
Expand Down
24 changes: 12 additions & 12 deletions playground/ImTools.Benchmarks/ImHashMapBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Runtime.CompilerServices;

using FHashMap91TypeString = ImTools.Experiments.FHashMap91<System.Type, string, ImTools.Experiments.FHashMap91.RefEq<System.Type>, ImTools.Experiments.FHashMap91.SingleArrayEntries<System.Type, string, ImTools.Experiments.FHashMap91.RefEq<System.Type>>>;
using SmallMapTypeString = ImTools.SmallMap<System.Type, string, ImTools.SmallMap.RefEq<System.Type>, ImTools.SmallMap.SingleArrayEntries<System.Type, string, ImTools.SmallMap.RefEq<System.Type>>>;
using SmallMapTypeString = ImTools.SmallMap<System.Type, string, ImTools.RefEq<System.Type>, ImTools.SmallMap.SingleArrayEntries<System.Type, string, ImTools.RefEq<System.Type>>>;
using FHashMapTypeString = FastExpressionCompiler.ImTools.FHashMap<System.Type, string, FastExpressionCompiler.ImTools.FHashMap.RefEq<System.Type>, FastExpressionCompiler.ImTools.FHashMap.SingleArrayEntries<System.Type, string, FastExpressionCompiler.ImTools.FHashMap.RefEq<System.Type>>>;

#nullable disable
Expand Down Expand Up @@ -1151,9 +1151,9 @@ public DictionarySlim<TypeVal, string> DictSlim_TryAdd()
}

// [Benchmark]
public ImTools.Experiments.FHashMap7<Type, string, ImTools.Experiments.RefEq<Type>> FHashMap7_AddOrUpdate()
public ImTools.Experiments.FHashMap7<Type, string, RefEq<Type>> FHashMap7_AddOrUpdate()
{
var map = new ImTools.Experiments.FHashMap7<Type, string, ImTools.Experiments.RefEq<Type>>();
var map = new ImTools.Experiments.FHashMap7<Type, string, RefEq<Type>>();

foreach (var key in _types)
map.AddOrUpdate(key, "a");
Expand All @@ -1176,9 +1176,9 @@ public FHashMap91TypeString FHashMap91_GetOrAddValueRef()
}

// [Benchmark]
public ImTools.Experiments.FHashMap8<Type, string, ImTools.Experiments.RefEq<Type>> FHashMap8_AddOrUpdate()
public ImTools.Experiments.FHashMap8<Type, string, RefEq<Type>> FHashMap8_AddOrUpdate()
{
var map = new ImTools.Experiments.FHashMap8<Type, string, ImTools.Experiments.RefEq<Type>>();
var map = new ImTools.Experiments.FHashMap8<Type, string, RefEq<Type>>();

foreach (var key in _types)
map.AddOrUpdate(key, "a");
Expand Down Expand Up @@ -2447,9 +2447,9 @@ public DictionarySlim<TypeVal, string> DictSlim()

private DictionarySlim<TypeVal, string> _dictSlim;

public ImTools.Experiments.FHashMap7<Type, string, ImTools.Experiments.RefEq<Type>> FillFHashMap7()
public ImTools.Experiments.FHashMap7<Type, string, RefEq<Type>> FillFHashMap7()
{
var map = new ImTools.Experiments.FHashMap7<Type, string, ImTools.Experiments.RefEq<Type>>();
var map = new ImTools.Experiments.FHashMap7<Type, string, RefEq<Type>>();

foreach (var key in _keys.Take(Count))
map.AddOrUpdate(key, "a");
Expand All @@ -2458,11 +2458,11 @@ public DictionarySlim<TypeVal, string> DictSlim()
return map;
}

private ImTools.Experiments.FHashMap7<Type, string, ImTools.Experiments.RefEq<Type>> _fHashMap7;
private ImTools.Experiments.FHashMap7<Type, string, RefEq<Type>> _fHashMap7;

public ImTools.Experiments.FHashMap9<Type, string, ImTools.Experiments.RefEq<Type>> FillFHashMap9()
public ImTools.Experiments.FHashMap9<Type, string, RefEq<Type>> FillFHashMap9()
{
var map = new ImTools.Experiments.FHashMap9<Type, string, ImTools.Experiments.RefEq<Type>>();
var map = new ImTools.Experiments.FHashMap9<Type, string, RefEq<Type>>();

foreach (var key in _keys.Take(Count))
map.AddOrUpdate(key, "a");
Expand All @@ -2471,7 +2471,7 @@ public DictionarySlim<TypeVal, string> DictSlim()
return map;
}

private ImTools.Experiments.FHashMap9<Type, string, ImTools.Experiments.RefEq<Type>> _fHashMap9;
private ImTools.Experiments.FHashMap9<Type, string, RefEq<Type>> _fHashMap9;

public FHashMap91TypeString FillFHashMap91()
{
Expand Down Expand Up @@ -2694,7 +2694,7 @@ public string SmallMap_TryGetValue()
}

[Benchmark]
public string FHashMap_TryGetValue() =>
public string FHashMap_TryGetValue() =>
_fHashMap.TryGetValueRef(LookupKey, out _);

// [Benchmark(Baseline = true)]
Expand Down
8 changes: 4 additions & 4 deletions playground/ImTools.Benchmarks/MemoryOwnerVsHashMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AntiVirusFriendlyConfig()
}
[Config(typeof(AntiVirusFriendlyConfig)),
MemoryDiagnoser,
Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.Declared),
Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.Declared),
RankColumn]
public class MemoryOwnerVsHashMap
{
Expand All @@ -43,8 +43,8 @@ public class MemoryOwnerVsHashMap

MemoryOwner<SampleClass> MemoryOwner;
ImHashMap<int, SampleClass> ImHashMap;
SmallMap<int, SampleClass, SmallMap.IntEq, SmallMap.SingleArrayEntries<int, SampleClass, SmallMap.IntEq>> SmallMap;

SmallMap<int, SampleClass, IntEq, SmallMap.SingleArrayEntries<int, SampleClass, IntEq>> SmallMap;

[GlobalSetup]
public void Setup()
Expand Down Expand Up @@ -84,7 +84,7 @@ public object ImTools_populate()
[Benchmark]
public object SmallMap_populate()
{
var imt = ImTools.SmallMap.New<int, SampleClass, SmallMap.IntEq>();
var imt = ImTools.SmallMap.New<int, SampleClass, IntEq>();
for (int i = 0; i < Count; i++)
{
// todo: @wip add AddSureNotPresent (use FHashMap for the reference)
Expand Down
Loading

0 comments on commit 83175ac

Please sign in to comment.