From 3da41aa9a123ed745f137bd47bd03757a57073de Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 9 Jul 2021 23:05:50 +0200 Subject: [PATCH] Improving hash performance for pairs of integral types * spot/misc/hash.hh: Here --- spot/misc/hash.hh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/spot/misc/hash.hh b/spot/misc/hash.hh index 52b603c66..94f603b7c 100644 --- a/spot/misc/hash.hh +++ b/spot/misc/hash.hh @@ -22,6 +22,7 @@ #pragma once +#include #include #include #include @@ -80,11 +81,26 @@ namespace spot template std::size_t operator()(const std::pair &p) const noexcept { - std::hash th; - std::hash uh; - return wang32_hash(static_cast(th(p.first)) ^ - static_cast(uh(p.second))); + if constexpr (std::is_integral::value + && sizeof(T) <= sizeof(std::size_t)/2 + && std::is_integral::value + && sizeof(U) <= sizeof(std::size_t)/2) + { + constexpr unsigned shift = (sizeof(std::size_t)/2)*CHAR_BIT; + std::size_t h = p.first; + h <<= shift; + h += p.second; + return h; + } + else + { + std::hash th; + std::hash uh; + + return wang32_hash(static_cast(th(p.first)) ^ + static_cast(uh(p.second))); + } } };