From 7b36dd709610156dba75f4d666ee905c80423bf4 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 17 May 2024 17:02:00 -0700 Subject: [PATCH 1/2] dowork --- .../ThroughputBenchmark.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs index 332299cd..d4cce166 100644 --- a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs +++ b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs @@ -101,7 +101,7 @@ protected override double Run(Stage stage, int iter, int threads, IThroughputBen void action(int index) { long[] samples = config.GetTestData(index); - int func(long x) => (int)x; + int func(long x) => Hash32(x); for (int i = 0; i < config.Iterations; i++) { @@ -137,6 +137,15 @@ void action(int index) // throughput = million ops/sec return throughput; } + + private static long a = 46601, b = 471486146934863, c = 7411438065634025597l; + + private static int Hash32(long x) + { + int low = (int)x; + int high = (int)((uint)x >> 32); + return (int)((uint)(a * low + b * high + c) >> 32); + } } public class UpdateThroughputBenchmark : ThroughputBenchmarkBase From 331df7d4c8d1d5fcbd372b0bd49e7b4655f34068 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 17 May 2024 20:02:00 -0700 Subject: [PATCH 2/2] spread --- .../ThroughputBenchmark.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs index d4cce166..4fa553dc 100644 --- a/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs +++ b/BitFaster.Caching.ThroughputAnalysis/ThroughputBenchmark.cs @@ -101,7 +101,7 @@ protected override double Run(Stage stage, int iter, int threads, IThroughputBen void action(int index) { long[] samples = config.GetTestData(index); - int func(long x) => Hash32(x); + int func(long x) => Spread(Spread(Spread(Hash32(x)))); for (int i = 0; i < config.Iterations; i++) { @@ -138,14 +138,29 @@ void action(int index) return throughput; } - private static long a = 46601, b = 471486146934863, c = 7411438065634025597l; + // https://lemire.me/blog/2018/08/15/fast-strongly-universal-64-bit-hashing-everywhere/ + private static readonly long a = 46601; + private static long b = 471486146934863; + private static long c = 7411438065634025597l; + [MethodImpl(MethodImplOptions.NoInlining)] private static int Hash32(long x) { int low = (int)x; int high = (int)((uint)x >> 32); return (int)((uint)(a * low + b * high + c) >> 32); } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static int Spread(int x) + { + x ^= (int)((uint)x >> 17); + x = (int)(x * 0xed5ad4bb); + x ^= (int)((uint)x >> 11); + x = (int)(x * 0xac4c1b51); + x ^= (int)((uint)x >> 15); + return x; + } } public class UpdateThroughputBenchmark : ThroughputBenchmarkBase