1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
std::vector<int> generateTestData(size_t count, int seed = 42) {
std::mt19937 gen(seed);
std::uniform_int_distribution<int> dis(0, 1000000);
std::vector<int> data;
data.reserve(count);
for (size_t i = 0; i < count; ++i) {
data.push_back(dis(gen));
}
return data;
}
template <size_t Size>
static void BM_RobinHood_Insert(benchmark::State &state) {
auto data = generateTestData(Size);
for (auto _ : state) {
RHHT<int, Size> ht;
for (const auto &val : data) {
benchmark::DoNotOptimize(ht.insert(val));
}
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * Size);
}
static void BM_UnorderedMap_Insert(benchmark::State &state) {
size_t size = state.range(0);
auto data = generateTestData(size);
for (auto _ : state) {
std::unordered_map<int, int> map;
for (const auto &val : data) {
benchmark::DoNotOptimize(map.insert({val, val}));
}
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * size);
}
template <size_t Size, int LoadFactorPercent>
static void BM_RobinHood_Lookup_Sequential(benchmark::State &state) {
size_t numElements = (Size * LoadFactorPercent) / 100;
auto data = generateTestData(numElements);
// Pre-populate
RHHT<int, Size> ht;
for (const auto &val : data) {
ht.insert(val);
}
size_t idx = 0;
for (auto _ : state) {
bool found = ht.has(data[idx % data.size()]);
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
template <size_t Size, int LoadFactorPercent>
static void BM_UnorderedMap_Lookup_Sequential(benchmark::State &state) {
size_t numElements = (Size * LoadFactorPercent) / 100;
auto data = generateTestData(numElements);
// Pre-populate
std::unordered_map<int, int> map;
for (const auto &val : data) {
map[val] = val;
}
size_t idx = 0;
for (auto _ : state) {
bool found = map.find(data[idx % data.size()]) != map.end();
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
// Random lookup pattern (more realistic)
template <size_t Size, int LoadFactorPercent>
static void BM_RobinHood_Lookup_Random(benchmark::State &state) {
size_t numElements = (Size * LoadFactorPercent) / 100;
auto data = generateTestData(numElements);
auto lookupKeys =
generateTestData(numElements, 123); // Different seed for lookups
// Pre-populate
RHHT<int, Size> ht;
for (const auto &val : data) {
ht.insert(val);
}
size_t idx = 0;
for (auto _ : state) {
bool found = ht.has(lookupKeys[idx % lookupKeys.size()]);
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
template <size_t Size, int LoadFactorPercent>
static void BM_UnorderedMap_Lookup_Random(benchmark::State &state) {
size_t numElements = (Size * LoadFactorPercent) / 100;
auto data = generateTestData(numElements);
auto lookupKeys = generateTestData(numElements, 123);
// Pre-populate
std::unordered_map<int, int> map;
for (const auto &val : data) {
map[val] = val;
}
size_t idx = 0;
for (auto _ : state) {
bool found = map.find(lookupKeys[idx % lookupKeys.size()]) != map.end();
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
// ============================================================================
// MIXED WORKLOAD (90% reads, 10% writes)
// ============================================================================
template <size_t Size>
static void BM_RobinHood_Mixed_90Read(benchmark::State &state) {
size_t numElements = (Size * 80) / 100;
auto data = generateTestData(numElements);
auto lookupKeys = generateTestData(numElements, 123);
for (auto _ : state) {
state.PauseTiming();
RHHT<int, Size> ht;
for (const auto &val : data) {
ht.insert(val);
}
state.ResumeTiming();
for (size_t i = 0; i < 1000; ++i) {
if (i % 10 == 0) {
// 10% writes
benchmark::DoNotOptimize(ht.insert(lookupKeys[i % lookupKeys.size()]));
} else {
// 90% reads
benchmark::DoNotOptimize(ht.has(lookupKeys[i % lookupKeys.size()]));
}
}
}
state.SetItemsProcessed(state.iterations() * 1000);
}
static void BM_UnorderedMap_Mixed_90Read(benchmark::State &state) {
size_t size = state.range(0);
size_t numElements = (size * 80) / 100;
auto data = generateTestData(numElements);
auto lookupKeys = generateTestData(numElements, 123);
for (auto _ : state) {
state.PauseTiming();
std::unordered_map<int, int> map;
for (const auto &val : data) {
map[val] = val;
}
state.ResumeTiming();
for (size_t i = 0; i < 1000; ++i) {
if (i % 10 == 0) {
benchmark::DoNotOptimize(map[lookupKeys[i % lookupKeys.size()]] = i);
} else {
benchmark::DoNotOptimize(map.find(lookupKeys[i % lookupKeys.size()]) !=
map.end());
}
}
}
state.SetItemsProcessed(state.iterations() * 1000);
}
// ============================================================================
// CACHE BEHAVIOR TEST (Many lookups on same small set)
// ============================================================================
template <size_t Size>
static void BM_RobinHood_HotCache(benchmark::State &state) {
size_t numElements = (Size * 80) / 100;
auto data = generateTestData(numElements);
RHHT<int, Size> ht;
for (const auto &val : data) {
ht.insert(val);
}
// Hot set: only 100 keys accessed repeatedly
std::vector<int> hotKeys(data.begin(),
data.begin() + std::min(size_t(100), data.size()));
size_t idx = 0;
for (auto _ : state) {
bool found = ht.has(hotKeys[idx % hotKeys.size()]);
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
template <size_t Size>
static void BM_UnorderedMap_HotCache(benchmark::State &state) {
size_t numElements = (Size * 80) / 100;
auto data = generateTestData(numElements);
std::unordered_map<int, int> map;
for (const auto &val : data) {
map[val] = val;
}
std::vector<int> hotKeys(data.begin(),
data.begin() + std::min(size_t(100), data.size()));
size_t idx = 0;
for (auto _ : state) {
bool found = map.find(hotKeys[idx % hotKeys.size()]) != map.end();
benchmark::DoNotOptimize(found);
idx++;
}
state.SetItemsProcessed(state.iterations());
}
// ============================================================================
// REGISTER BENCHMARKS
// ============================================================================
// Small size (1K elements)
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 1024, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 1024, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 1024, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 1024, 90);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 1024, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 1024, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 1024, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 1024, 90);
// Medium size (10K elements)
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 10240, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 10240, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 10240, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 10240, 90);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 10240, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 10240, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 10240, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 10240, 90);
// Large size (100K elements)
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 102400, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 102400, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Sequential, 102400, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Sequential, 102400, 90);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 102400, 75);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 102400, 75);
BENCHMARK_TEMPLATE(BM_RobinHood_Lookup_Random, 102400, 90);
BENCHMARK_TEMPLATE(BM_UnorderedMap_Lookup_Random, 102400, 90);
// Insertion benchmarks
BENCHMARK_TEMPLATE(BM_RobinHood_Insert, 1024);
BENCHMARK(BM_UnorderedMap_Insert)->Arg(1024);
BENCHMARK_TEMPLATE(BM_RobinHood_Insert, 10240);
BENCHMARK(BM_UnorderedMap_Insert)->Arg(10240);
BENCHMARK_TEMPLATE(BM_RobinHood_Insert, 102400);
BENCHMARK(BM_UnorderedMap_Insert)->Arg(102400);
// Mixed workload
BENCHMARK_TEMPLATE(BM_RobinHood_Mixed_90Read, 10240);
BENCHMARK(BM_UnorderedMap_Mixed_90Read)->Arg(10240);
// Hot cache
BENCHMARK_TEMPLATE(BM_RobinHood_HotCache, 10240);
BENCHMARK_TEMPLATE(BM_UnorderedMap_HotCache, 10240);
|