1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
15 #include <unordered_set>
18 #include "benchmark/benchmark.h"
20 #include "ContainerBenchmarks.h"
21 #include "GenerateInput.h"
22 #include "test_macros.h"
24 using namespace ContainerBenchmarks
;
26 constexpr std::size_t TestNumInputs
= 1024;
28 // The purpose of this hash function is to NOT be implemented as the identity function,
29 // which is how std::hash is implemented for smaller integral types.
30 struct NonIdentityScalarHash
: std::hash
<unsigned long long> {};
32 // The sole purpose of this comparator is to be used in BM_Rehash, where
33 // we need something slow enough to be easily noticable in benchmark results.
34 // The default implementation of operator== for strings seems to be a little
35 // too fast for that specific benchmark to reliably show a noticeable
36 // improvement, but unoptimized bytewise comparison fits just right.
37 // Early return is there just for convenience, since we only compare strings
38 // of equal length in BM_Rehash.
40 SlowStringEq() = default;
41 inline TEST_ALWAYS_INLINE
bool operator()(const std::string
& lhs
, const std::string
& rhs
) const {
42 if (lhs
.size() != rhs
.size())
46 for (size_t i
= 0; i
< lhs
.size(); ++i
) {
47 eq
&= lhs
[i
] == rhs
[i
];
53 //----------------------------------------------------------------------------//
55 // ---------------------------------------------------------------------------//
57 // Sorted Ascending //
59 BM_InsertValue
, unordered_set_uint32
, std::unordered_set
<uint32_t>{}, getRandomIntegerInputs
<uint32_t>)
63 BM_InsertValue
, unordered_set_uint32_sorted
, std::unordered_set
<uint32_t>{}, getSortedIntegerInputs
<uint32_t>)
67 BENCHMARK_CAPTURE(BM_InsertValue
,
68 unordered_set_top_bits_uint32
,
69 std::unordered_set
<uint32_t>{},
70 getSortedTopBitsIntegerInputs
<uint32_t>)
73 BENCHMARK_CAPTURE(BM_InsertValueRehash
,
74 unordered_set_top_bits_uint32
,
75 std::unordered_set
<uint32_t, NonIdentityScalarHash
>{},
76 getSortedTopBitsIntegerInputs
<uint32_t>)
80 BENCHMARK_CAPTURE(BM_InsertValue
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
83 BENCHMARK_CAPTURE(BM_InsertValueRehash
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
88 BM_InsertValue
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
91 BENCHMARK_CAPTURE(BM_InsertValueRehash
,
92 unordered_set_prefixed_string
,
93 std::unordered_set
<std::string
>{},
94 getPrefixedRandomStringInputs
)
97 //----------------------------------------------------------------------------//
99 // ---------------------------------------------------------------------------//
103 BM_Find
, unordered_set_random_uint64
, std::unordered_set
<uint64_t>{}, getRandomIntegerInputs
<uint64_t>)
104 ->Arg(TestNumInputs
);
106 BENCHMARK_CAPTURE(BM_FindRehash
,
107 unordered_set_random_uint64
,
108 std::unordered_set
<uint64_t, NonIdentityScalarHash
>{},
109 getRandomIntegerInputs
<uint64_t>)
110 ->Arg(TestNumInputs
);
114 BM_Find
, unordered_set_sorted_uint64
, std::unordered_set
<uint64_t>{}, getSortedIntegerInputs
<uint64_t>)
115 ->Arg(TestNumInputs
);
117 BENCHMARK_CAPTURE(BM_FindRehash
,
118 unordered_set_sorted_uint64
,
119 std::unordered_set
<uint64_t, NonIdentityScalarHash
>{},
120 getSortedIntegerInputs
<uint64_t>)
121 ->Arg(TestNumInputs
);
124 #ifndef TEST_HAS_NO_INT128
125 BENCHMARK_CAPTURE(BM_Find
,
126 unordered_set_sorted_uint128
,
127 std::unordered_set
<__uint128_t
>{},
128 getSortedTopBitsIntegerInputs
<__uint128_t
>)
129 ->Arg(TestNumInputs
);
131 BENCHMARK_CAPTURE(BM_FindRehash
,
132 unordered_set_sorted_uint128
,
133 std::unordered_set
<__uint128_t
>{},
134 getSortedTopBitsIntegerInputs
<__uint128_t
>)
135 ->Arg(TestNumInputs
);
140 BM_Find
, unordered_set_sorted_uint32
, std::unordered_set
<uint32_t>{}, getSortedIntegerInputs
<uint32_t>)
141 ->Arg(TestNumInputs
);
143 BENCHMARK_CAPTURE(BM_FindRehash
,
144 unordered_set_sorted_uint32
,
145 std::unordered_set
<uint32_t, NonIdentityScalarHash
>{},
146 getSortedIntegerInputs
<uint32_t>)
147 ->Arg(TestNumInputs
);
149 // Sorted Ascending //
151 BM_Find
, unordered_set_sorted_large_uint64
, std::unordered_set
<uint64_t>{}, getSortedLargeIntegerInputs
<uint64_t>)
152 ->Arg(TestNumInputs
);
154 BENCHMARK_CAPTURE(BM_FindRehash
,
155 unordered_set_sorted_large_uint64
,
156 std::unordered_set
<uint64_t, NonIdentityScalarHash
>{},
157 getSortedLargeIntegerInputs
<uint64_t>)
158 ->Arg(TestNumInputs
);
162 BM_Find
, unordered_set_top_bits_uint64
, std::unordered_set
<uint64_t>{}, getSortedTopBitsIntegerInputs
<uint64_t>)
163 ->Arg(TestNumInputs
);
165 BENCHMARK_CAPTURE(BM_FindRehash
,
166 unordered_set_top_bits_uint64
,
167 std::unordered_set
<uint64_t, NonIdentityScalarHash
>{},
168 getSortedTopBitsIntegerInputs
<uint64_t>)
169 ->Arg(TestNumInputs
);
172 BENCHMARK_CAPTURE(BM_Find
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
173 ->Arg(TestNumInputs
);
175 BENCHMARK_CAPTURE(BM_FindRehash
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
176 ->Arg(TestNumInputs
);
178 // Prefixed String //
180 BM_Find
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
181 ->Arg(TestNumInputs
);
184 BM_FindRehash
, unordered_set_prefixed_string
, std::unordered_set
<std::string
>{}, getPrefixedRandomStringInputs
)
185 ->Arg(TestNumInputs
);
187 //----------------------------------------------------------------------------//
189 // ---------------------------------------------------------------------------//
191 BENCHMARK_CAPTURE(BM_Rehash
,
192 unordered_set_string_arg
,
193 std::unordered_set
<std::string
, std::hash
<std::string
>, SlowStringEq
>{},
194 getRandomStringInputs
)
195 ->Arg(TestNumInputs
);
197 BENCHMARK_CAPTURE(BM_Rehash
, unordered_set_int_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
198 ->Arg(TestNumInputs
);
200 //----------------------------------------------------------------------------//
202 // ---------------------------------------------------------------------------//
205 BM_Compare_same_container
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
206 ->Arg(TestNumInputs
);
208 BENCHMARK_CAPTURE(BM_Compare_same_container
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
209 ->Arg(TestNumInputs
);
212 BM_Compare_different_containers
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
213 ->Arg(TestNumInputs
);
216 BM_Compare_different_containers
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
217 ->Arg(TestNumInputs
);
219 ///////////////////////////////////////////////////////////////////////////////
220 BENCHMARK_CAPTURE(BM_InsertDuplicate
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
221 ->Arg(TestNumInputs
);
222 BENCHMARK_CAPTURE(BM_InsertDuplicate
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
223 ->Arg(TestNumInputs
);
225 BENCHMARK_CAPTURE(BM_EmplaceDuplicate
, unordered_set_int
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
226 ->Arg(TestNumInputs
);
227 BENCHMARK_CAPTURE(BM_EmplaceDuplicate
, unordered_set_string
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
228 ->Arg(TestNumInputs
);
231 BM_InsertDuplicate
, unordered_set_int_insert_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<int>)
232 ->Arg(TestNumInputs
);
234 BM_InsertDuplicate
, unordered_set_string_insert_arg
, std::unordered_set
<std::string
>{}, getRandomStringInputs
)
235 ->Arg(TestNumInputs
);
238 BM_EmplaceDuplicate
, unordered_set_int_insert_arg
, std::unordered_set
<int>{}, getRandomIntegerInputs
<unsigned>)
239 ->Arg(TestNumInputs
);
242 BM_EmplaceDuplicate
, unordered_set_string_arg
, std::unordered_set
<std::string
>{}, getRandomCStringInputs
)
243 ->Arg(TestNumInputs
);