bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <type_traits>
17 #include <typeindex>
18 #include <utility>
19 #include <vector>
20 
21 #include "absl/base/attributes.h"
22 #include "absl/hash/hash.h"
23 #include "absl/random/random.h"
24 #include "absl/strings/cord.h"
25 #include "absl/strings/cord_test_helpers.h"
26 #include "absl/strings/string_view.h"
27 #include "benchmark/benchmark.h"
28 
29 namespace {
30 
31 using absl::Hash;
32 
33 template <template <typename> class H, typename T>
34 void RunBenchmark(benchmark::State& state, T value) {
35  H<T> h;
36  for (auto _ : state) {
39  }
40 }
41 
42 } // namespace
43 
44 template <typename T>
46 
47 class TypeErasedInterface {
48  public:
49  virtual ~TypeErasedInterface() = default;
50 
51  template <typename H>
53  state = H::combine(std::move(state), std::type_index(typeid(wrapper)));
55  return state;
56  }
57 
58  private:
59  virtual void HashValue(absl::HashState state) const = 0;
60 };
61 
62 template <typename T>
63 struct TypeErasedAbslHash {
64  class Wrapper : public TypeErasedInterface {
65  public:
66  explicit Wrapper(const T& value) : value_(value) {}
67 
68  private:
69  void HashValue(absl::HashState state) const override {
71  }
72 
73  const T& value_;
74  };
75 
76  size_t operator()(const T& value) {
78  }
79 };
80 
81 template <typename FuncType>
82 inline FuncType* ODRUseFunction(FuncType* ptr) {
83  volatile FuncType* dummy = ptr;
84  return dummy;
85 }
86 
89  result.Flatten();
90  return result;
91 }
92 
94  const size_t orig_size = size;
95  std::vector<std::string> chunks;
96  size_t chunk_size = std::max<size_t>(1, size / 10);
97  while (size > chunk_size) {
98  chunks.push_back(std::string(chunk_size, 'a'));
99  size -= chunk_size;
100  }
101  if (size > 0) {
102  chunks.push_back(std::string(size, 'a'));
103  }
105  (void) orig_size;
106  assert(result.size() == orig_size);
107  return result;
108 }
109 
110 // Generates a benchmark and a codegen method for the provided types. The
111 // codegen method provides a well known entrypoint for dumping assembly.
112 #define MAKE_BENCHMARK(hash, name, ...) \
113  namespace { \
114  void BM_##hash##_##name(benchmark::State& state) { \
115  RunBenchmark<hash>(state, __VA_ARGS__); \
116  } \
117  BENCHMARK(BM_##hash##_##name); \
118  } \
119  size_t Codegen##hash##name(const decltype(__VA_ARGS__)& arg); \
120  size_t Codegen##hash##name(const decltype(__VA_ARGS__)& arg) { \
121  return hash<decltype(__VA_ARGS__)>{}(arg); \
122  } \
123  bool absl_hash_test_odr_use##hash##name = \
124  ODRUseFunction(&Codegen##hash##name);
125 
129 MAKE_BENCHMARK(AbslHash, DoubleZero, 0.0);
130 MAKE_BENCHMARK(AbslHash, PairInt32Int32, std::pair<int32_t, int32_t>{});
131 MAKE_BENCHMARK(AbslHash, PairInt64Int64, std::pair<int64_t, int64_t>{});
132 MAKE_BENCHMARK(AbslHash, TupleInt32BoolInt64,
133  std::tuple<int32_t, bool, int64_t>{});
134 MAKE_BENCHMARK(AbslHash, String_0, std::string());
135 MAKE_BENCHMARK(AbslHash, String_10, std::string(10, 'a'));
136 MAKE_BENCHMARK(AbslHash, String_30, std::string(30, 'a'));
137 MAKE_BENCHMARK(AbslHash, String_90, std::string(90, 'a'));
138 MAKE_BENCHMARK(AbslHash, String_200, std::string(200, 'a'));
139 MAKE_BENCHMARK(AbslHash, String_5000, std::string(5000, 'a'));
140 MAKE_BENCHMARK(AbslHash, Cord_Flat_0, absl::Cord());
141 MAKE_BENCHMARK(AbslHash, Cord_Flat_10, FlatCord(10));
142 MAKE_BENCHMARK(AbslHash, Cord_Flat_30, FlatCord(30));
143 MAKE_BENCHMARK(AbslHash, Cord_Flat_90, FlatCord(90));
144 MAKE_BENCHMARK(AbslHash, Cord_Flat_200, FlatCord(200));
145 MAKE_BENCHMARK(AbslHash, Cord_Flat_5000, FlatCord(5000));
146 MAKE_BENCHMARK(AbslHash, Cord_Fragmented_200, FragmentedCord(200));
147 MAKE_BENCHMARK(AbslHash, Cord_Fragmented_5000, FragmentedCord(5000));
148 MAKE_BENCHMARK(AbslHash, VectorInt64_10, std::vector<int64_t>(10));
149 MAKE_BENCHMARK(AbslHash, VectorInt64_100, std::vector<int64_t>(100));
150 MAKE_BENCHMARK(AbslHash, VectorDouble_10, std::vector<double>(10, 1.1));
151 MAKE_BENCHMARK(AbslHash, VectorDouble_100, std::vector<double>(100, 1.1));
152 MAKE_BENCHMARK(AbslHash, PairStringString_0,
153  std::make_pair(std::string(), std::string()));
154 MAKE_BENCHMARK(AbslHash, PairStringString_10,
155  std::make_pair(std::string(10, 'a'), std::string(10, 'b')));
156 MAKE_BENCHMARK(AbslHash, PairStringString_30,
157  std::make_pair(std::string(30, 'a'), std::string(30, 'b')));
158 MAKE_BENCHMARK(AbslHash, PairStringString_90,
159  std::make_pair(std::string(90, 'a'), std::string(90, 'b')));
160 MAKE_BENCHMARK(AbslHash, PairStringString_200,
161  std::make_pair(std::string(200, 'a'), std::string(200, 'b')));
162 MAKE_BENCHMARK(AbslHash, PairStringString_5000,
163  std::make_pair(std::string(5000, 'a'), std::string(5000, 'b')));
164 
167 MAKE_BENCHMARK(TypeErasedAbslHash, PairInt32Int32,
168  std::pair<int32_t, int32_t>{});
169 MAKE_BENCHMARK(TypeErasedAbslHash, PairInt64Int64,
170  std::pair<int64_t, int64_t>{});
171 MAKE_BENCHMARK(TypeErasedAbslHash, TupleInt32BoolInt64,
172  std::tuple<int32_t, bool, int64_t>{});
174 MAKE_BENCHMARK(TypeErasedAbslHash, String_10, std::string(10, 'a'));
175 MAKE_BENCHMARK(TypeErasedAbslHash, String_30, std::string(30, 'a'));
176 MAKE_BENCHMARK(TypeErasedAbslHash, String_90, std::string(90, 'a'));
177 MAKE_BENCHMARK(TypeErasedAbslHash, String_200, std::string(200, 'a'));
178 MAKE_BENCHMARK(TypeErasedAbslHash, String_5000, std::string(5000, 'a'));
179 MAKE_BENCHMARK(TypeErasedAbslHash, VectorDouble_10,
180  std::vector<double>(10, 1.1));
181 MAKE_BENCHMARK(TypeErasedAbslHash, VectorDouble_100,
182  std::vector<double>(100, 1.1));
183 
184 // The latency benchmark attempts to model the speed of the hash function in
185 // production. When a hash function is used for hashtable lookups it is rarely
186 // used to hash N items in a tight loop nor on constant sized strings. Instead,
187 // after hashing there is a potential equality test plus a (usually) large
188 // amount of user code. To simulate this effectively we introduce a data
189 // dependency between elements we hash by using the hash of the Nth element as
190 // the selector of the N+1th element to hash. This isolates the hash function
191 // code much like in production. As a bonus we use the hash to generate strings
192 // of size [1,N] (instead of fixed N) to disable perfect branch predictions in
193 // hash function implementations.
194 namespace {
195 // 16kb fits in L1 cache of most CPUs we care about. Keeping memory latency low
196 // will allow us to attribute most time to CPU which means more accurate
197 // measurements.
198 static constexpr size_t kEntropySize = 16 << 10;
199 static char entropy[kEntropySize + 1024];
200 ABSL_ATTRIBUTE_UNUSED static const bool kInitialized = [] {
202  static_assert(sizeof(entropy) % sizeof(uint64_t) == 0, "");
203  for (int i = 0; i != sizeof(entropy); i += sizeof(uint64_t)) {
204  auto rand = absl::Uniform<uint64_t>(gen);
205  memcpy(&entropy[i], &rand, sizeof(uint64_t));
206  }
207  return true;
208 }();
209 } // namespace
210 
211 template <class T>
212 struct PodRand {
213  static_assert(std::is_pod<T>::value, "");
214  static_assert(kEntropySize + sizeof(T) < sizeof(entropy), "");
215 
216  T Get(size_t i) const {
217  T v;
218  memcpy(&v, &entropy[i % kEntropySize], sizeof(T));
219  return v;
220  }
221 };
222 
223 template <size_t N>
224 struct StringRand {
225  static_assert(kEntropySize + N < sizeof(entropy), "");
226 
227  absl::string_view Get(size_t i) const {
228  // This has a small bias towards small numbers. Because max N is ~200 this
229  // is very small and prefer to be very fast instead of absolutely accurate.
230  // Also we pass N = 2^K+1 so that mod reduces to a bitand.
231  size_t s = (i % (N - 1)) + 1;
232  return {&entropy[i % kEntropySize], s};
233  }
234 };
235 
236 #define MAKE_LATENCY_BENCHMARK(hash, name, ...) \
237  namespace { \
238  void BM_latency_##hash##_##name(benchmark::State& state) { \
239  __VA_ARGS__ r; \
240  hash<decltype(r.Get(0))> h; \
241  size_t i = 871401241; \
242  for (auto _ : state) { \
243  benchmark::DoNotOptimize(i = h(r.Get(i))); \
244  } \
245  } \
246  BENCHMARK(BM_latency_##hash##_##name); \
247  } // namespace
248 
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
absl::Cord
Definition: abseil-cpp/absl/strings/cord.h:150
absl::HashState::Create
static HashState Create(T *state)
Definition: abseil-cpp/absl/hash/hash.h:321
FragmentedCord
absl::Cord FragmentedCord(size_t size)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:93
TypeErasedAbslHash::Wrapper::Wrapper
Wrapper(const T &value)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:66
absl::hash_internal::Hash
Definition: abseil-cpp/absl/hash/internal/hash.h:1221
testing::internal::Int32
TypeWithSize< 4 >::Int Int32
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2159
FlatCord
absl::Cord FlatCord(size_t size)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:87
PodRand
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:281
MAKE_BENCHMARK
#define MAKE_BENCHMARK(hash, name,...)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:112
ABSL_ATTRIBUTE_UNUSED
#define ABSL_ATTRIBUTE_UNUSED
Definition: abseil-cpp/absl/base/attributes.h:550
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::hash_internal::HashStateBase< HashState >::combine
static HashState combine(HashState state, const T &value, const Ts &... values)
Definition: abseil-cpp/absl/hash/internal/hash.h:1226
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
T
#define T(upbtypeconst, upbtype, ctype, default_value)
TypeErasedAbslHash::operator()
size_t operator()(const T &value)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:76
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
TypeErasedInterface
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:48
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
StringRand::Get
absl::string_view Get(size_t i) const
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:227
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
wrapper
grpc_channel_wrapper * wrapper
Definition: src/php/ext/grpc/channel.h:48
TypeErasedAbslHash
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:64
TypeErasedInterface::~TypeErasedInterface
virtual ~TypeErasedInterface()=default
H
#define H(b, c, d)
Definition: md4.c:114
PodRand::Get
T Get(size_t i) const
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:216
TypeErasedAbslHash::Wrapper::value_
const T & value_
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:74
value
const char * value
Definition: hpack_parser_table.cc:165
N
#define N
Definition: sync_test.cc:37
ODRUseFunction
FuncType * ODRUseFunction(FuncType *ptr)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:82
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >
testing::internal::Double
FloatingPoint< double > Double
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:397
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
absl::ABSL_NAMESPACE_BEGIN::dummy
int dummy
Definition: function_type_benchmark.cc:28
MAKE_LATENCY_BENCHMARK
#define MAKE_LATENCY_BENCHMARK(hash, name,...)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:236
absl::MakeFragmentedCord
Cord MakeFragmentedCord(const Container &c)
Definition: abseil-cpp/absl/strings/cord_test_helpers.h:103
testing::internal::Int64
TypeWithSize< 8 >::Int Int64
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2161
TypeErasedAbslHash::Wrapper
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:65
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
TypeErasedInterface::HashValue
virtual void HashValue(absl::HashState state) const =0
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
TypeErasedAbslHash::Wrapper::HashValue
void HashValue(absl::HashState state) const override
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:69
absl::str_format_internal::LengthMod::h
@ h
absl::HashState
Definition: abseil-cpp/absl/hash/hash.h:312
TypeErasedInterface::AbslHashValue
friend H AbslHashValue(H state, const TypeErasedInterface &wrapper)
Definition: bloaty/third_party/abseil-cpp/absl/hash/hash_benchmark.cc:52
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87
absl::Hash
absl::hash_internal::Hash< T > Hash
Definition: abseil-cpp/absl/hash/hash.h:247
StringRand
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:293


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:00