Skip to content

Commit a777948

Browse files
committed
better benchmark. It's now the same speed as abseil
1 parent 92a803a commit a777948

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

include/ankerl/unordered_dense.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ struct tuple_hash_helper {
334334
// If it isn't an integral we need to hash it.
335335
template <typename Arg>
336336
[[nodiscard]] constexpr static auto to64(Arg const& arg) -> uint64_t {
337-
if constexpr (std::is_integral_v<Arg>) {
337+
if constexpr (std::is_integral_v<Arg> || std::is_enum_v<Arg>) {
338338
return static_cast<uint64_t>(arg);
339339
} else {
340340
return hash<Arg>{}(arg);

scripts/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
cmd_and_dir = [
99
# needs honggfuzz installed
10-
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_release'],
10+
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/clang_cpp17_debug'],
1111
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/clang_cpp17_release'],
12+
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_release'],
1213
['env', 'CXX_LD=mold', 'CXX=ccache hfuzz-clang++ -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/hfuzz-clang_cpp17_release'],
13-
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/clang_cpp17_debug'],
1414
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_debug'],
1515

1616
# 32bit. Install lib32-clang

test/unit/tuple_hash.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,26 @@ TEST_CASE("tuple_hash_with_stringview") {
4545
REQUIRE(h1 != h2);
4646
}
4747

48+
// #include <absl/hash/hash.h>
49+
4850
TEST_CASE("bench_tuple_hash" * doctest::test_suite("bench")) {
49-
using T = std::tuple<char, int, uint16_t, std::byte, uint64_t>;
51+
using T = std::tuple<uint8_t, int, uint16_t, uint64_t>;
52+
53+
auto vecs = std::vector<T>(100);
54+
auto rng = ankerl::nanobench::Rng(123);
55+
for (auto& v : vecs) {
56+
std::get<0>(v) = static_cast<uint8_t>(rng());
57+
std::get<1>(v) = static_cast<int>(rng());
58+
std::get<2>(v) = static_cast<uint16_t>(rng());
59+
std::get<3>(v) = static_cast<uint64_t>(rng());
60+
}
5061

51-
auto h = uint64_t{};
52-
auto t = std::tuple<char, int, uint16_t, std::byte, uint64_t>{};
53-
ankerl::nanobench::Bench().run("ankerl hash", [&] {
54-
h += ankerl::unordered_dense::hash<T>{}(t);
55-
++std::get<4>(t);
62+
uint64_t h = 0;
63+
ankerl::nanobench::Bench().batch(vecs.size()).run("ankerl hash", [&] {
64+
for (auto const& v : vecs) {
65+
h += ankerl::unordered_dense::hash<T>{}(v);
66+
// h += absl::Hash<T>{}(v);
67+
}
5668
});
69+
ankerl::nanobench::doNotOptimizeAway(h);
5770
}

0 commit comments

Comments
 (0)