Skip to content

Commit b192028

Browse files
committed
CHG: minor changes to blitzmap - reduced CacheMisses/Op
1 parent b33566e commit b192028

File tree

14 files changed

+552
-405
lines changed

14 files changed

+552
-405
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Engines;
3+
using Faster.Map.Contracts;
4+
using Faster.Map.Core;
5+
using Faster.Map.Hashing;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Numerics;
10+
using System.Runtime.CompilerServices;
11+
12+
namespace Faster.Map.Benchmark
13+
{
14+
[MarkdownExporterAttribute.GitHub]
15+
[MemoryDiagnoser]
16+
[DisassemblyDiagnoser]
17+
[SimpleJob(RunStrategy.Monitoring, launchCount: 1, iterationCount: 8, warmupCount: 3)]
18+
public class CorrelatedKeyBenchmark
19+
{
20+
#region Fields
21+
22+
private DenseMap<uint, uint, FastHasherUint> _denseMapAES;
23+
private DenseMap<uint, uint, AvalancheHasherUint> _denseMapAvalanche;
24+
private BlitzMap<uint, uint, FastHasherUint> _blitzMapAes;
25+
private BlitzMap<uint, uint, AvalancheHasherUint> _blitzMapAvalance;
26+
private uint[] keys;
27+
28+
#endregion
29+
30+
#region Params
31+
32+
[Params("Random", "Correlated")]
33+
public string KeyPattern { get; set; }
34+
35+
[Params(0.6, 0.75, 0.85, 0.9, 0.95)]
36+
public double LoadFactor { get; set; }
37+
38+
[Params(16_777_216)]
39+
public uint Length { get; set; }
40+
41+
#endregion
42+
43+
#region Setup
44+
45+
[GlobalSetup]
46+
public void Setup()
47+
{
48+
int count = (int)(Length * LoadFactor);
49+
keys = new uint[count];
50+
51+
if (KeyPattern == "Random")
52+
{
53+
var rnd = new Random(123);
54+
var set = new HashSet<uint>(count);
55+
while (set.Count < count)
56+
set.Add((uint)rnd.Next());
57+
58+
keys = set.ToArray();
59+
}
60+
else
61+
{
62+
// MMF / pointer-style correlated keys
63+
uint baseAddr = 0x4000_0000;
64+
for (int i = 0; i < count; i++)
65+
keys[i] = baseAddr + ((uint)i << 4); // only 4 bits change
66+
}
67+
68+
uint capacity = BitOperations.RoundUpToPowerOf2(Length);
69+
70+
_denseMapAES = new DenseMap<uint, uint, FastHasherUint>(capacity);
71+
_denseMapAvalanche = new DenseMap<uint, uint, AvalancheHasherUint>(capacity);
72+
_blitzMapAes = new BlitzMap<uint, uint, FastHasherUint>((int)capacity);
73+
_blitzMapAvalance = new BlitzMap<uint, uint, AvalancheHasherUint>((int)capacity);
74+
75+
foreach (var key in keys)
76+
{
77+
_denseMapAES.InsertOrUpdate(key, key);
78+
_denseMapAvalanche.InsertOrUpdate(key, key);
79+
80+
_blitzMapAes.InsertOrUpdate(key, key);
81+
_blitzMapAvalance.InsertOrUpdate(key, key);
82+
}
83+
}
84+
85+
#endregion
86+
87+
#region Benchmarks
88+
89+
[Benchmark(Baseline = true)]
90+
public void DenseMap_AES()
91+
{
92+
for (int i = 0; i < keys.Length; i++)
93+
_denseMapAES.Get(keys[i], out _);
94+
}
95+
96+
[Benchmark]
97+
public void DenseMap_Avalanche()
98+
{
99+
for (int i = 0; i < keys.Length; i++)
100+
_denseMapAvalanche.Get(keys[i], out _);
101+
}
102+
103+
[Benchmark]
104+
public void BlitzMapAes()
105+
{
106+
for (int i = 0; i < keys.Length; i++)
107+
_blitzMapAes.Get(keys[i], out _);
108+
}
109+
110+
[Benchmark]
111+
public void BlitzMapAvalanche()
112+
{
113+
for (int i = 0; i < keys.Length; i++)
114+
_blitzMapAvalance.Get(keys[i], out _);
115+
}
116+
117+
#endregion
118+
}
119+
120+
#region AvalancheHasher
121+
122+
public struct AvalancheHasherUint : IHasher<uint>
123+
{
124+
125+
public bool Equals(uint x, uint y)
126+
{
127+
return x == y;
128+
}
129+
130+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
131+
public uint ComputeHash(uint key)
132+
{
133+
uint h = key;
134+
h ^= h >> 16;
135+
h *= 0x85ebca6b;
136+
h ^= h >> 13;
137+
h *= 0xc2b2ae35;
138+
h ^= h >> 16;
139+
return h;
140+
}
141+
}
142+
143+
#endregion
144+
}

benchmarks/Faster.Map.Benchmark/Faster.Map.Benchmark.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
35-
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.14.0" />
34+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
35+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.15.8" />
3636
<PackageReference Include="ObjectLayoutInspector" Version="0.1.4" />
3737
</ItemGroup>
3838

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
using BenchmarkDotNet.Attributes;
2-
using System.Collections.Generic;
3-
using System.Collections;
4-
using System.Numerics;
5-
using System.Linq;
2+
using BenchmarkDotNet.Diagnosers;
63
using BenchmarkDotNet.Engines;
74
using Faster.Map.Benchmark.Utilities;
85
using Faster.Map.Core;
6+
using Faster.Map.Hashing;
7+
using Faster.Map.Hashing.Algorithm;
8+
using System.Collections;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Numerics;
912

1013
namespace Faster.Map.Benchmark
1114
{
12-
[MarkdownExporterAttribute.GitHub]
13-
[DisassemblyDiagnoser]
14-
[MemoryDiagnoser]
15-
[SimpleJob(RunStrategy.Monitoring, launchCount: 1, iterationCount: 10, warmupCount: 3)]
15+
[MarkdownExporterAttribute.GitHub]
16+
[SimpleJob(RunStrategy.Monitoring, launchCount: 1, iterationCount: 20, warmupCount: 5)]
17+
//[HardwareCounters(
18+
//HardwareCounter.BranchMispredictions,
19+
//HardwareCounter.BranchInstructions,
20+
//HardwareCounter.CacheMisses,
21+
//HardwareCounter.TotalCycles)]
1622

1723
public class GetBenchmark
1824
{
1925
#region Fields
2026

21-
private DenseMap<uint, uint> _denseMap;
22-
private BlitzMap<uint, uint> _blitz;
27+
private DenseMap<uint, uint, FastHasherUint> _denseMap;
28+
private BlitzMap<uint, uint, FastHasherUint> _blitz;
2329
private Dictionary<uint, uint> _dictionary;
24-
private RobinhoodMap<uint, uint> _robinHoodMap;
30+
private RobinhoodMap<uint, uint, FastHasherUint> _robinHoodMap;
2531

2632
private uint[] keys;
2733

2834
#endregion
2935

3036
#region Properties
3137

32-
[Params(/*0.1, 0.2, 0.3, 0.4, 0.5,*/ 0.6/*, 0.7, 0.8*/)]
38+
[Params(/*0.1, 0.2, 0.3, 0.4, 0.5,*/ 0.5/*, 0.7, 0.8*/)]
3339
public static double LoadFactor { get; set; }
3440

3541
[Params(134_217_728)]
@@ -56,59 +62,59 @@ public void Setup()
5662
uint length = BitOperations.RoundUpToPowerOf2(Length);
5763
int dicLength = HashHelpers.GetPrime((int)Length);
5864

59-
_denseMap = new DenseMap<uint, uint>(length);
60-
_blitz = new BlitzMap<uint, uint>((int)length, LoadFactor);
65+
_denseMap = new DenseMap<uint, uint, FastHasherUint>(length);
66+
_blitz = new BlitzMap<uint, uint, FastHasherUint>((int)length, LoadFactor);
6167

6268
_dictionary = new Dictionary<uint, uint>(dicLength);
63-
_robinHoodMap = new RobinhoodMap<uint, uint>(length, 0.9);
69+
_robinHoodMap = new RobinhoodMap<uint, uint, FastHasherUint>(length, 0.9);
6470

6571
foreach (var key in keys)
6672
{
67-
_dictionary.Add(key, key);
68-
_denseMap.InsertOrUpdate(key, key);
73+
// _dictionary.Add(key, key);
74+
// _denseMap.InsertOrUpdate(key, key);
6975
_blitz.Insert(key, key);
70-
_robinHoodMap.Emplace(key, key);
76+
// _robinHoodMap.Emplace(key, key);
7177
}
7278
}
7379

74-
//[Benchmark]
75-
//public void BlitzMap()
76-
//{
77-
// for (int i = 0; i < keys.Length; i++)
78-
// {
79-
// var key = keys[i];
80-
// _blitz.Get(key, out var _);
81-
// }
82-
//}
83-
8480
[Benchmark]
85-
public void DenseMap()
81+
public void BlitzMap()
8682
{
8783
for (int i = 0; i < keys.Length; i++)
8884
{
8985
var key = keys[i];
90-
_denseMap.Get(key, out var _);
86+
_blitz.Get(key, out var _);
9187
}
9288
}
9389

94-
[Benchmark]
95-
public void Dictionary()
96-
{
97-
for (int i = 0; i < keys.Length; i++)
98-
{
99-
var key = keys[i];
100-
_dictionary.TryGetValue(key, out var _);
101-
}
102-
}
90+
//[Benchmark]
91+
//public void DenseMap()
92+
//{
93+
// for (int i = 0; i < keys.Length; i++)
94+
// {
95+
// var key = keys[i];
96+
// _denseMap.Get(key, out var _);
97+
// }
98+
//}
10399

104-
[Benchmark]
105-
public void RobinhoodMap()
106-
{
107-
for (int i = 0; i < keys.Length; i++)
108-
{
109-
var key = keys[i];
110-
_robinHoodMap.Get(key, out var _);
111-
}
112-
}
100+
//[Benchmark]
101+
//public void Dictionary()
102+
//{
103+
// for (int i = 0; i < keys.Length; i++)
104+
// {
105+
// var key = keys[i];
106+
// _dictionary.TryGetValue(key, out var _);
107+
// }
108+
//}
109+
110+
//[Benchmark]
111+
//public void RobinhoodMap()
112+
//{
113+
// for (int i = 0; i < keys.Length; i++)
114+
// {
115+
// var key = keys[i];
116+
// _robinHoodMap.Get(key, out var _);
117+
// }
118+
//}
113119
}
114120
}

benchmarks/Faster.Map.Benchmark/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using BenchmarkDotNet.Configs;
22
using BenchmarkDotNet.Running;
3+
using Microsoft.Diagnostics.Tracing.Parsers.MicrosoftWindowsTCPIP;
4+
using System;
35

46
namespace Faster.Map.Benchmark
57
{
@@ -10,7 +12,9 @@ static void Main(string[] args)
1012
//BenchmarkRunner.Run<AddBenchmark>();
1113
// BenchmarkRunner.Run<EnumerableBenchmark>();
1214
//BenchmarkRunner.Run<UpdateBenchmark>();
13-
BenchmarkRunner.Run<GetBenchmark>(/*new DebugInProcessConfig()*/);
15+
BenchmarkRunner.Run<GetBenchmark>();
16+
17+
Console.ReadLine();
1418
}
1519
}
1620
}

0 commit comments

Comments
 (0)