From 251894540d25c1429077e1657be9579c0f1bbe95 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 25 Nov 2022 20:52:30 +0200 Subject: [PATCH] Split mul31_unrolled_hash into mul31_x4_hash and mul31_x8_hash in benchmarks --- benchmark/unordered.cpp | 33 ++++++++++++++++++--------------- benchmark/unordered_flat.cpp | 34 ++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/benchmark/unordered.cpp b/benchmark/unordered.cpp index 7519b50..b7e2a80 100644 --- a/benchmark/unordered.cpp +++ b/benchmark/unordered.cpp @@ -26,10 +26,8 @@ // mul31_hash -class mul31_hash +struct mul31_hash { -public: - std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT { char const * p = st.data(); @@ -50,18 +48,20 @@ public: } }; -// mul31_unrolled_hash +// mul31_x4_hash -template struct mul31_unrolled_hash_impl; - -template<> struct mul31_unrolled_hash_impl<32> +struct mul31_x4_hash { std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT { char const * p = st.data(); std::size_t n = st.size(); +#if SIZE_MAX > UINT32_MAX + std::size_t h = 0xCBF29CE484222325ull; +#else std::size_t h = 0x811C9DC5u; +#endif while( n >= 4 ) { @@ -87,14 +87,16 @@ template<> struct mul31_unrolled_hash_impl<32> } }; -template<> struct mul31_unrolled_hash_impl<64> +// mul31_x8_hash + +struct mul31_x8_hash { std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT { char const * p = st.data(); std::size_t n = st.size(); - std::size_t h = 0xCBF29CE484222325ull; + boost::uint64_t h = 0xCBF29CE484222325ull; while( n >= 8 ) { @@ -120,12 +122,10 @@ template<> struct mul31_unrolled_hash_impl<64> --n; } - return h; + return static_cast( h ); } }; -struct mul31_unrolled_hash: mul31_unrolled_hash_impl< std::numeric_limits::digits > {}; - // fnv1a_hash template struct fnv1a_hash_impl; @@ -363,7 +363,8 @@ int main() std::puts( "Hash speed test:\n" ); test_hash_speed( N * 16, v ); - test_hash_speed( N * 16, v ); + test_hash_speed( N * 16, v ); + test_hash_speed( N * 16, v ); test_hash_speed( N * 16, v ); test_hash_speed >( N * 16, v ); test_hash_speed >( N * 16, v ); @@ -396,7 +397,8 @@ int main() } test_hash_collision( N * 16, v, n ); - test_hash_collision( N * 16, v, n ); + test_hash_collision( N * 16, v, n ); + test_hash_collision( N * 16, v, n ); test_hash_collision( N * 16, v, n ); test_hash_collision >( N * 16, v, n ); test_hash_collision >( N * 16, v, n ); @@ -418,7 +420,8 @@ int main() std::puts( "Container speed test:\n" ); test_container_speed( N, v ); - test_container_speed( N, v ); + test_container_speed( N, v ); + test_container_speed( N, v ); test_container_speed( N, v ); test_container_speed >( N, v ); test_container_speed >( N, v ); diff --git a/benchmark/unordered_flat.cpp b/benchmark/unordered_flat.cpp index 81f365e..395279a 100644 --- a/benchmark/unordered_flat.cpp +++ b/benchmark/unordered_flat.cpp @@ -212,10 +212,8 @@ template BOOST_NOINLINE void test( char const* label ) // mul31_hash -class mul31_hash +struct mul31_hash { -public: - // not avalanching std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT @@ -238,18 +236,22 @@ public: } }; -// mul31_unrolled_hash +// mul31_x4_hash -template struct mul31_unrolled_hash_impl; - -template<> struct mul31_unrolled_hash_impl<32> +struct mul31_x4_hash { + // not avalanching + std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT { char const * p = st.data(); std::size_t n = st.size(); +#if SIZE_MAX > UINT32_MAX + std::size_t h = 0xCBF29CE484222325ull; +#else std::size_t h = 0x811C9DC5u; +#endif while( n >= 4 ) { @@ -275,14 +277,18 @@ template<> struct mul31_unrolled_hash_impl<32> } }; -template<> struct mul31_unrolled_hash_impl<64> +// mul31_x8_hash + +struct mul31_x8_hash { + // not avalanching + std::size_t operator()( std::string const& st ) const BOOST_NOEXCEPT { char const * p = st.data(); std::size_t n = st.size(); - std::size_t h = 0xCBF29CE484222325ull; + boost::uint64_t h = 0xCBF29CE484222325ull; while( n >= 8 ) { @@ -308,15 +314,10 @@ template<> struct mul31_unrolled_hash_impl<64> --n; } - return h; + return static_cast( h ); } }; -struct mul31_unrolled_hash: mul31_unrolled_hash_impl< std::numeric_limits::digits > -{ - // not avalanching -}; - // fnv1a_hash template struct fnv1a_hash_impl; @@ -437,7 +438,8 @@ int main() test< boost::hash >( "boost::hash" ); test< std_hash >( "std::hash" ); test< mul31_hash >( "mul31_hash" ); - test< mul31_unrolled_hash >( "mul31_unrolled_hash" ); + test< mul31_x4_hash >( "mul31_x4_hash" ); + test< mul31_x8_hash >( "mul31_x8_hash" ); test< fnv1a_hash >( "fnv1a_hash" ); #ifdef HAVE_ABSEIL