5 #include <unordered_set>
8 #include "benchmark/benchmark.h"
10 #include "ContainerBenchmarks.h"
11 #include "GenerateInput.h"
12 #include "test_macros.h"
14 using namespace ContainerBenchmarks
;
16 constexpr std::size_t TestNumInputs
= 1024;
18 template <class _Size
>
19 inline TEST_ALWAYS_INLINE _Size
loadword(const void* __p
) {
21 std::memcpy(&__r
, __p
, sizeof(__r
));
25 inline TEST_ALWAYS_INLINE
std::size_t rotate_by_at_least_1(std::size_t __val
, int __shift
) {
26 return (__val
>> __shift
) | (__val
<< (64 - __shift
));
29 inline 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
;
33 std::size_t __b
= (__v
^ __a
) * __mul
;
39 template <std::size_t _Len
>
40 inline 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
);
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
));
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
));
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
;
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;
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
));
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.
104 struct 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())
111 for (size_t i
= 0; i
< lhs
.size(); ++i
) {
112 eq
&= lhs
[i
] == rhs
[i
];
118 //----------------------------------------------------------------------------//
120 // ---------------------------------------------------------------------------//
122 template <class HashFn
, class GenInputs
>
123 void BM_Hash(benchmark::State
& st
, HashFn fn
, GenInputs gen
) {
124 auto in
= gen(st
.range(0));
125 const auto end
= in
.data() + in
.size();
126 std::size_t last_hash
= 0;
127 benchmark::DoNotOptimize(&last_hash
);
128 while (st
.KeepRunning()) {
129 for (auto it
= in
.data(); it
!= end
; ++it
) {
130 benchmark::DoNotOptimize(last_hash
+= fn(*it
));
132 benchmark::ClobberMemory();
136 BENCHMARK_CAPTURE(BM_Hash
, uint32_random_std_hash
, std::hash
<uint32_t>{}, getRandomIntegerInputs
<uint32_t>)
137 ->Arg(TestNumInputs
);
139 BENCHMARK_CAPTURE(BM_Hash
, uint32_random_custom_hash
, UInt32Hash
{}, getRandomIntegerInputs
<uint32_t>)
140 ->Arg(TestNumInputs
);
142 BENCHMARK_CAPTURE(BM_Hash
, uint32_top_std_hash
, std::hash
<uint32_t>{}, getSortedTopBitsIntegerInputs
<uint32_t>)
143 ->Arg(TestNumInputs
);
145 BENCHMARK_CAPTURE(BM_Hash
, uint32_top_custom_hash
, UInt32Hash
{}, getSortedTopBitsIntegerInputs
<uint32_t>)
146 ->Arg(TestNumInputs
);
148 //----------------------------------------------------------------------------//
150 // ---------------------------------------------------------------------------//
152 // Sorted Ascending //
154 BM_InsertValue
, unordered_set_uint32
, std::unordered_set
<uint32_t>{}, getRandomIntegerInputs
<uint32_t>)
155 ->Arg(TestNumInputs
);
158 BM_InsertValue
, unordered_set_uint32_sorted
, std::unordered_set
<uint32_t>{}, getSortedIntegerInputs
<uint32_t>)
159 ->Arg(TestNumInputs
);
162 BENCHMARK_CAPTURE(BM_InsertValue
,
163 unordered_set_top_bits_uint32
,
164 std::unordered_set
<uint32_t>{},
165 getSortedTopBitsIntegerInputs
<uint32_t>)
166 ->Arg(TestNumInputs
);
168 BENCHMARK_CAPTURE(BM_InsertValueRehash
,
169 unordered_set_top_bits_uint32
,
170 std::unordered_set
<uint32_t, UInt32Hash
>{},
171 getSortedTopBitsIntegerInputs
<uint32_t>)
172 ->Arg(TestNumInputs
);
175 BENCHMARK_CAPTURE(BM_InsertValue
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
176 ->Arg(TestNumInputs
);
178 BENCHMARK_CAPTURE(BM_InsertValueRehash
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
179 ->Arg(TestNumInputs
);
181 // Prefixed String //
183 BM_InsertValue
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
184 ->Arg(TestNumInputs
);
186 BENCHMARK_CAPTURE(BM_InsertValueRehash
,
187 unordered_set_prefixed_string
,
188 std::unordered_set
<std::string
>{},
189 getPrefixedRandomStringInputs
)
190 ->Arg(TestNumInputs
);
192 //----------------------------------------------------------------------------//
194 // ---------------------------------------------------------------------------//
198 BM_Find
, unordered_set_random_uint64
, std::unordered_set
<uint64_t>{}, getRandomIntegerInputs
<uint64_t>)
199 ->Arg(TestNumInputs
);
201 BENCHMARK_CAPTURE(BM_FindRehash
,
202 unordered_set_random_uint64
,
203 std::unordered_set
<uint64_t, UInt64Hash
>{},
204 getRandomIntegerInputs
<uint64_t>)
205 ->Arg(TestNumInputs
);
209 BM_Find
, unordered_set_sorted_uint64
, std::unordered_set
<uint64_t>{}, getSortedIntegerInputs
<uint64_t>)
210 ->Arg(TestNumInputs
);
212 BENCHMARK_CAPTURE(BM_FindRehash
,
213 unordered_set_sorted_uint64
,
214 std::unordered_set
<uint64_t, UInt64Hash
>{},
215 getSortedIntegerInputs
<uint64_t>)
216 ->Arg(TestNumInputs
);
219 BENCHMARK_CAPTURE(BM_Find
,
220 unordered_set_sorted_uint128
,
221 std::unordered_set
<__uint128_t
, UInt128Hash
>{},
222 getSortedTopBitsIntegerInputs
<__uint128_t
>)
223 ->Arg(TestNumInputs
);
225 BENCHMARK_CAPTURE(BM_FindRehash
,
226 unordered_set_sorted_uint128
,
227 std::unordered_set
<__uint128_t
, UInt128Hash
>{},
228 getSortedTopBitsIntegerInputs
<__uint128_t
>)
229 ->Arg(TestNumInputs
);
233 BM_Find
, unordered_set_sorted_uint32
, std::unordered_set
<uint32_t>{}, getSortedIntegerInputs
<uint32_t>)
234 ->Arg(TestNumInputs
);
236 BENCHMARK_CAPTURE(BM_FindRehash
,
237 unordered_set_sorted_uint32
,
238 std::unordered_set
<uint32_t, UInt32Hash2
>{},
239 getSortedIntegerInputs
<uint32_t>)
240 ->Arg(TestNumInputs
);
242 // Sorted Ascending //
244 BM_Find
, unordered_set_sorted_large_uint64
, std::unordered_set
<uint64_t>{}, getSortedLargeIntegerInputs
<uint64_t>)
245 ->Arg(TestNumInputs
);
247 BENCHMARK_CAPTURE(BM_FindRehash
,
248 unordered_set_sorted_large_uint64
,
249 std::unordered_set
<uint64_t, UInt64Hash
>{},
250 getSortedLargeIntegerInputs
<uint64_t>)
251 ->Arg(TestNumInputs
);
255 BM_Find
, unordered_set_top_bits_uint64
, std::unordered_set
<uint64_t>{}, getSortedTopBitsIntegerInputs
<uint64_t>)
256 ->Arg(TestNumInputs
);
258 BENCHMARK_CAPTURE(BM_FindRehash
,
259 unordered_set_top_bits_uint64
,
260 std::unordered_set
<uint64_t, UInt64Hash
>{},
261 getSortedTopBitsIntegerInputs
<uint64_t>)
262 ->Arg(TestNumInputs
);
265 BENCHMARK_CAPTURE(BM_Find
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
266 ->Arg(TestNumInputs
);
268 BENCHMARK_CAPTURE(BM_FindRehash
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
269 ->Arg(TestNumInputs
);
271 // Prefixed String //
273 BM_Find
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
274 ->Arg(TestNumInputs
);
277 BM_FindRehash
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
278 ->Arg(TestNumInputs
);
280 //----------------------------------------------------------------------------//
282 // ---------------------------------------------------------------------------//
284 BENCHMARK_CAPTURE(BM_Rehash
,
285 unordered_set_string_arg
,
286 std::unordered_set
<std::string
, std::hash
<std::string
>, SlowStringEq
>{},
287 getRandomStringInputs
)
288 ->Arg(TestNumInputs
);
290 BENCHMARK_CAPTURE(BM_Rehash
, unordered_set_int_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
291 ->Arg(TestNumInputs
);
293 //----------------------------------------------------------------------------//
295 // ---------------------------------------------------------------------------//
298 BM_Compare_same_container
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
299 ->Arg(TestNumInputs
);
301 BENCHMARK_CAPTURE(BM_Compare_same_container
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
302 ->Arg(TestNumInputs
);
305 BM_Compare_different_containers
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
306 ->Arg(TestNumInputs
);
309 BM_Compare_different_containers
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
310 ->Arg(TestNumInputs
);
312 ///////////////////////////////////////////////////////////////////////////////
313 BENCHMARK_CAPTURE(BM_InsertDuplicate
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
314 ->Arg(TestNumInputs
);
315 BENCHMARK_CAPTURE(BM_InsertDuplicate
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
316 ->Arg(TestNumInputs
);
318 BENCHMARK_CAPTURE(BM_EmplaceDuplicate
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
319 ->Arg(TestNumInputs
);
320 BENCHMARK_CAPTURE(BM_EmplaceDuplicate
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
321 ->Arg(TestNumInputs
);
324 BM_InsertDuplicate
, unordered_set_int_insert_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
325 ->Arg(TestNumInputs
);
327 BM_InsertDuplicate
, unordered_set_string_insert_arg
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
328 ->Arg(TestNumInputs
);
331 BM_EmplaceDuplicate
, unordered_set_int_insert_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<unsigned>)
332 ->Arg(TestNumInputs
);
335 BM_EmplaceDuplicate
, unordered_set_string_arg
, std::unordered_set
<std::string
>{}, getRandomCStringInputs
)
336 ->Arg(TestNumInputs
);