Skip to content

Commit

Permalink
better benchmark. It's now the same speed as abseil
Browse files Browse the repository at this point in the history
  • Loading branch information
martinus committed Dec 20, 2023
1 parent 92a803a commit 4a88cb0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/ankerl/unordered_dense.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ struct tuple_hash_helper {
// If it isn't an integral we need to hash it.
template <typename Arg>
[[nodiscard]] constexpr static auto to64(Arg const& arg) -> uint64_t {
if constexpr (std::is_integral_v<Arg>) {
if constexpr (std::is_integral_v<Arg> || std::is_enum_v<Arg>) {
return static_cast<uint64_t>(arg);
} else {
return hash<Arg>{}(arg);
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

cmd_and_dir = [
# needs honggfuzz installed
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_release'],
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/clang_cpp17_debug'],
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/clang_cpp17_release'],
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'release', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_release'],
['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'],
['env', 'CXX_LD=mold', 'CXX=ccache clang++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/clang_cpp17_debug'],
['env', 'CXX_LD=mold', 'CXX=ccache g++', 'meson', 'setup', '--buildtype', 'debug', '-Dcpp_std=c++17', 'builddir/gcc_cpp17_debug'],

# 32bit. Install lib32-clang
Expand Down
25 changes: 19 additions & 6 deletions test/unit/tuple_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,26 @@ TEST_CASE("tuple_hash_with_stringview") {
REQUIRE(h1 != h2);
}

// #include <absl/hash/hash.h>

TEST_CASE("bench_tuple_hash" * doctest::test_suite("bench")) {
using T = std::tuple<char, int, uint16_t, std::byte, uint64_t>;
using T = std::tuple<uint8_t, int, uint16_t, uint64_t>;

auto vecs = std::vector<T>(100);
auto rng = ankerl::nanobench::Rng(123);
for (auto& v : vecs) {
std::get<0>(v) = static_cast<uint8_t>(rng());
std::get<1>(v) = static_cast<int>(rng());
std::get<2>(v) = static_cast<uint16_t>(rng());
std::get<3>(v) = static_cast<uint64_t>(rng());
}

auto h = uint64_t{};
auto t = std::tuple<char, int, uint16_t, std::byte, uint64_t>{};
ankerl::nanobench::Bench().run("ankerl hash", [&] {
h += ankerl::unordered_dense::hash<T>{}(t);
++std::get<4>(t);
uint64_t h = 0;
ankerl::nanobench::Bench().batch(vecs.size()).run("ankerl hash", [&] {
for (auto const& v : vecs) {
h += ankerl::unordered_dense::hash<T>{}(v);
// h += absl::Hash<T>{}(v);
}
});
ankerl::nanobench::doNotOptimizeAway(h);
}

0 comments on commit 4a88cb0

Please sign in to comment.