1#include <cstdint>
2#include <cstdlib>
3#include <cstring>
4#include <functional>
5#include <unordered_set>
6#include <vector>
7
8#include "benchmark/benchmark.h"
9
10#include "ContainerBenchmarks.h"
11#include "GenerateInput.h"
12#include "test_macros.h"
13
14using namespace ContainerBenchmarks;
15
16constexpr std::size_t TestNumInputs = 1024;
17
18template <class _Size>
19inline TEST_ALWAYS_INLINE _Size loadword(const void* __p) {
20 _Size __r;
21 std::memcpy(dest: &__r, src: __p, n: sizeof(__r));
22 return __r;
23}
24
25inline TEST_ALWAYS_INLINE std::size_t rotate_by_at_least_1(std::size_t __val, int __shift) {
26 return (__val >> __shift) | (__val << (64 - __shift));
27}
28
29inline TEST_ALWAYS_INLINE std::size_t hash_len_16(std::size_t __u, std::size_t __v) {
30 const std::size_t __mul = 0x9ddfea08eb382d69ULL;
31 std::size_t __a = (__u ^ __v) * __mul;
32 __a ^= (__a >> 47);
33 std::size_t __b = (__v ^ __a) * __mul;
34 __b ^= (__b >> 47);
35 __b *= __mul;
36 return __b;
37}
38
39template <std::size_t _Len>
40inline TEST_ALWAYS_INLINE std::size_t hash_len_0_to_8(const char* __s) {
41 static_assert(_Len == 4 || _Len == 8, "");
42 const uint64_t __a = loadword<uint32_t>(__s);
43 const uint64_t __b = loadword<uint32_t>(__s + _Len - 4);
44 return hash_len_16(_Len + (__a << 3), __b);
45}
46
47struct UInt32Hash {
48 UInt32Hash() = default;
49 inline TEST_ALWAYS_INLINE std::size_t operator()(uint32_t data) const {
50 return hash_len_0_to_8<4>(reinterpret_cast<const char*>(&data));
51 }
52};
53
54struct UInt64Hash {
55 UInt64Hash() = default;
56 inline TEST_ALWAYS_INLINE std::size_t operator()(uint64_t data) const {
57 return hash_len_0_to_8<8>(reinterpret_cast<const char*>(&data));
58 }
59};
60
61struct UInt128Hash {
62 UInt128Hash() = default;
63 inline TEST_ALWAYS_INLINE std::size_t operator()(__uint128_t data) const {
64 const __uint128_t __mask = static_cast<std::size_t>(-1);
65 const std::size_t __a = (std::size_t)(data & __mask);
66 const std::size_t __b = (std::size_t)((data & (__mask << 64)) >> 64);
67 return hash_len_16(__a, rotate_by_at_least_1(__b + 16, 16)) ^ __b;
68 }
69};
70
71struct UInt32Hash2 {
72 UInt32Hash2() = default;
73 inline TEST_ALWAYS_INLINE std::size_t operator()(uint32_t data) const {
74 const uint32_t __m = 0x5bd1e995;
75 const uint32_t __r = 24;
76 uint32_t __h = 4;
77 uint32_t __k = data;
78 __k *= __m;
79 __k ^= __k >> __r;
80 __k *= __m;
81 __h *= __m;
82 __h ^= __k;
83 __h ^= __h >> 13;
84 __h *= __m;
85 __h ^= __h >> 15;
86 return __h;
87 }
88};
89
90struct UInt64Hash2 {
91 UInt64Hash2() = default;
92 inline TEST_ALWAYS_INLINE std::size_t operator()(uint64_t data) const {
93 return hash_len_0_to_8<8>(reinterpret_cast<const char*>(&data));
94 }
95};
96
97// The sole purpose of this comparator is to be used in BM_Rehash, where
98// we need something slow enough to be easily noticable in benchmark results.
99// The default implementation of operator== for strings seems to be a little
100// too fast for that specific benchmark to reliably show a noticeable
101// improvement, but unoptimized bytewise comparison fits just right.
102// Early return is there just for convenience, since we only compare strings
103// of equal length in BM_Rehash.
104struct SlowStringEq {
105 SlowStringEq() = default;
106 inline TEST_ALWAYS_INLINE bool operator()(const std::string& lhs, const std::string& rhs) const {
107 if (lhs.size() != rhs.size())
108 return false;
109
110 bool eq = true;
111 for (size_t i = 0; i < lhs.size(); ++i) {
112 eq &= lhs[i] == rhs[i];
113 }
114 return eq;
115 }
116};
117
118//----------------------------------------------------------------------------//
119// BM_Hash
120// ---------------------------------------------------------------------------//
121
122template <class HashFn, class GenInputs>
123void BM_Hash(benchmark::State& st, HashFn fn, GenInputs gen) {
124 auto in = gen(st.range(pos: 0));
125 const auto end = in.data() + in.size();
126 std::size_t last_hash = 0;
127 benchmark::DoNotOptimize(value: &last_hash);
128 while (st.KeepRunning()) {
129 for (auto it = in.data(); it != end; ++it) {
130 benchmark::DoNotOptimize(last_hash += fn(*it));
131 }
132 benchmark::ClobberMemory();
133 }
134}
135
136BENCHMARK_CAPTURE(BM_Hash, uint32_random_std_hash, std::hash<uint32_t>{}, getRandomIntegerInputs<uint32_t>)
137 ->Arg(x: TestNumInputs);
138
139BENCHMARK_CAPTURE(BM_Hash, uint32_random_custom_hash, UInt32Hash{}, getRandomIntegerInputs<uint32_t>)
140 ->Arg(x: TestNumInputs);
141
142BENCHMARK_CAPTURE(BM_Hash, uint32_top_std_hash, std::hash<uint32_t>{}, getSortedTopBitsIntegerInputs<uint32_t>)
143 ->Arg(x: TestNumInputs);
144
145BENCHMARK_CAPTURE(BM_Hash, uint32_top_custom_hash, UInt32Hash{}, getSortedTopBitsIntegerInputs<uint32_t>)
146 ->Arg(x: TestNumInputs);
147
148//----------------------------------------------------------------------------//
149// BM_InsertValue
150// ---------------------------------------------------------------------------//
151
152// Sorted Ascending //
153BENCHMARK_CAPTURE(
154 BM_InsertValue, unordered_set_uint32, std::unordered_set<uint32_t>{}, getRandomIntegerInputs<uint32_t>)
155 ->Arg(x: TestNumInputs);
156
157BENCHMARK_CAPTURE(
158 BM_InsertValue, unordered_set_uint32_sorted, std::unordered_set<uint32_t>{}, getSortedIntegerInputs<uint32_t>)
159 ->Arg(x: TestNumInputs);
160
161// Top Bytes //
162BENCHMARK_CAPTURE(BM_InsertValue,
163 unordered_set_top_bits_uint32,
164 std::unordered_set<uint32_t>{},
165 getSortedTopBitsIntegerInputs<uint32_t>)
166 ->Arg(x: TestNumInputs);
167
168BENCHMARK_CAPTURE(BM_InsertValueRehash,
169 unordered_set_top_bits_uint32,
170 std::unordered_set<uint32_t, UInt32Hash>{},
171 getSortedTopBitsIntegerInputs<uint32_t>)
172 ->Arg(x: TestNumInputs);
173
174// String //
175BENCHMARK_CAPTURE(BM_InsertValue, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
176 ->Arg(x: TestNumInputs);
177
178BENCHMARK_CAPTURE(BM_InsertValueRehash, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
179 ->Arg(x: TestNumInputs);
180
181// Prefixed String //
182BENCHMARK_CAPTURE(
183 BM_InsertValue, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
184 ->Arg(x: TestNumInputs);
185
186BENCHMARK_CAPTURE(BM_InsertValueRehash,
187 unordered_set_prefixed_string,
188 std::unordered_set<std::string>{},
189 getPrefixedRandomStringInputs)
190 ->Arg(x: TestNumInputs);
191
192//----------------------------------------------------------------------------//
193// BM_Find
194// ---------------------------------------------------------------------------//
195
196// Random //
197BENCHMARK_CAPTURE(
198 BM_Find, unordered_set_random_uint64, std::unordered_set<uint64_t>{}, getRandomIntegerInputs<uint64_t>)
199 ->Arg(x: TestNumInputs);
200
201BENCHMARK_CAPTURE(BM_FindRehash,
202 unordered_set_random_uint64,
203 std::unordered_set<uint64_t, UInt64Hash>{},
204 getRandomIntegerInputs<uint64_t>)
205 ->Arg(x: TestNumInputs);
206
207// Sorted //
208BENCHMARK_CAPTURE(
209 BM_Find, unordered_set_sorted_uint64, std::unordered_set<uint64_t>{}, getSortedIntegerInputs<uint64_t>)
210 ->Arg(x: TestNumInputs);
211
212BENCHMARK_CAPTURE(BM_FindRehash,
213 unordered_set_sorted_uint64,
214 std::unordered_set<uint64_t, UInt64Hash>{},
215 getSortedIntegerInputs<uint64_t>)
216 ->Arg(x: TestNumInputs);
217
218// Sorted //
219BENCHMARK_CAPTURE(BM_Find,
220 unordered_set_sorted_uint128,
221 std::unordered_set<__uint128_t, UInt128Hash>{},
222 getSortedTopBitsIntegerInputs<__uint128_t>)
223 ->Arg(x: TestNumInputs);
224
225BENCHMARK_CAPTURE(BM_FindRehash,
226 unordered_set_sorted_uint128,
227 std::unordered_set<__uint128_t, UInt128Hash>{},
228 getSortedTopBitsIntegerInputs<__uint128_t>)
229 ->Arg(x: TestNumInputs);
230
231// Sorted //
232BENCHMARK_CAPTURE(
233 BM_Find, unordered_set_sorted_uint32, std::unordered_set<uint32_t>{}, getSortedIntegerInputs<uint32_t>)
234 ->Arg(x: TestNumInputs);
235
236BENCHMARK_CAPTURE(BM_FindRehash,
237 unordered_set_sorted_uint32,
238 std::unordered_set<uint32_t, UInt32Hash2>{},
239 getSortedIntegerInputs<uint32_t>)
240 ->Arg(x: TestNumInputs);
241
242// Sorted Ascending //
243BENCHMARK_CAPTURE(
244 BM_Find, unordered_set_sorted_large_uint64, std::unordered_set<uint64_t>{}, getSortedLargeIntegerInputs<uint64_t>)
245 ->Arg(x: TestNumInputs);
246
247BENCHMARK_CAPTURE(BM_FindRehash,
248 unordered_set_sorted_large_uint64,
249 std::unordered_set<uint64_t, UInt64Hash>{},
250 getSortedLargeIntegerInputs<uint64_t>)
251 ->Arg(x: TestNumInputs);
252
253// Top Bits //
254BENCHMARK_CAPTURE(
255 BM_Find, unordered_set_top_bits_uint64, std::unordered_set<uint64_t>{}, getSortedTopBitsIntegerInputs<uint64_t>)
256 ->Arg(x: TestNumInputs);
257
258BENCHMARK_CAPTURE(BM_FindRehash,
259 unordered_set_top_bits_uint64,
260 std::unordered_set<uint64_t, UInt64Hash>{},
261 getSortedTopBitsIntegerInputs<uint64_t>)
262 ->Arg(x: TestNumInputs);
263
264// String //
265BENCHMARK_CAPTURE(BM_Find, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
266 ->Arg(x: TestNumInputs);
267
268BENCHMARK_CAPTURE(BM_FindRehash, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
269 ->Arg(x: TestNumInputs);
270
271// Prefixed String //
272BENCHMARK_CAPTURE(
273 BM_Find, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
274 ->Arg(x: TestNumInputs);
275
276BENCHMARK_CAPTURE(
277 BM_FindRehash, unordered_set_prefixed_string, std::unordered_set<std::string>{}, getPrefixedRandomStringInputs)
278 ->Arg(x: TestNumInputs);
279
280//----------------------------------------------------------------------------//
281// BM_Rehash
282// ---------------------------------------------------------------------------//
283
284BENCHMARK_CAPTURE(BM_Rehash,
285 unordered_set_string_arg,
286 std::unordered_set<std::string, std::hash<std::string>, SlowStringEq>{},
287 getRandomStringInputs)
288 ->Arg(x: TestNumInputs);
289
290BENCHMARK_CAPTURE(BM_Rehash, unordered_set_int_arg, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
291 ->Arg(x: TestNumInputs);
292
293//----------------------------------------------------------------------------//
294// BM_Compare
295// ---------------------------------------------------------------------------//
296
297BENCHMARK_CAPTURE(
298 BM_Compare_same_container, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
299 ->Arg(x: TestNumInputs);
300
301BENCHMARK_CAPTURE(BM_Compare_same_container, unordered_set_int, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
302 ->Arg(x: TestNumInputs);
303
304BENCHMARK_CAPTURE(
305 BM_Compare_different_containers, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
306 ->Arg(x: TestNumInputs);
307
308BENCHMARK_CAPTURE(
309 BM_Compare_different_containers, unordered_set_int, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
310 ->Arg(x: TestNumInputs);
311
312///////////////////////////////////////////////////////////////////////////////
313BENCHMARK_CAPTURE(BM_InsertDuplicate, unordered_set_int, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
314 ->Arg(x: TestNumInputs);
315BENCHMARK_CAPTURE(BM_InsertDuplicate, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
316 ->Arg(x: TestNumInputs);
317
318BENCHMARK_CAPTURE(BM_EmplaceDuplicate, unordered_set_int, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
319 ->Arg(x: TestNumInputs);
320BENCHMARK_CAPTURE(BM_EmplaceDuplicate, unordered_set_string, std::unordered_set<std::string>{}, getRandomStringInputs)
321 ->Arg(x: TestNumInputs);
322
323BENCHMARK_CAPTURE(
324 BM_InsertDuplicate, unordered_set_int_insert_arg, std::unordered_set<int>{}, getRandomIntegerInputs<int>)
325 ->Arg(x: TestNumInputs);
326BENCHMARK_CAPTURE(
327 BM_InsertDuplicate, unordered_set_string_insert_arg, std::unordered_set<std::string>{}, getRandomStringInputs)
328 ->Arg(x: TestNumInputs);
329
330BENCHMARK_CAPTURE(
331 BM_EmplaceDuplicate, unordered_set_int_insert_arg, std::unordered_set<int>{}, getRandomIntegerInputs<unsigned>)
332 ->Arg(x: TestNumInputs);
333
334BENCHMARK_CAPTURE(
335 BM_EmplaceDuplicate, unordered_set_string_arg, std::unordered_set<std::string>{}, getRandomCStringInputs)
336 ->Arg(x: TestNumInputs);
337
338BENCHMARK_MAIN();
339

source code of libcxx/benchmarks/unordered_set_operations.bench.cpp