From b3fb6bd99041ad26768800f0e624e23b85add362 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 12 Mar 2022 21:40:58 +0800 Subject: [PATCH] Avoid redundant hashing The return type of `std::hash()` is larger (or equal) than what `qHash()` requires so we can omit hashing it again. --- src/base/bittorrent/infohash.cpp | 2 +- src/base/bittorrent/ltqhash.h | 12 ++++-------- src/base/digest32.h | 14 +++++++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/base/bittorrent/infohash.cpp b/src/base/bittorrent/infohash.cpp index 2f5943fe8..7fa78b233 100644 --- a/src/base/bittorrent/infohash.cpp +++ b/src/base/bittorrent/infohash.cpp @@ -96,7 +96,7 @@ std::size_t BitTorrent::qHash(const BitTorrent::TorrentID &key, const std::size_ uint BitTorrent::qHash(const BitTorrent::TorrentID &key, const uint seed) #endif { - return ::qHash(std::hash()(key), seed); + return ::qHash(static_cast(key), seed); } bool BitTorrent::operator==(const BitTorrent::InfoHash &left, const BitTorrent::InfoHash &right) diff --git a/src/base/bittorrent/ltqhash.h b/src/base/bittorrent/ltqhash.h index a86da8a6a..b8a929b32 100644 --- a/src/base/bittorrent/ltqhash.h +++ b/src/base/bittorrent/ltqhash.h @@ -28,28 +28,24 @@ #pragma once +#include + +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #include #include -#include #include -// From https://doc.qt.io/qt-6/qhash.html#the-hashing-function: -// A hashing function for a key type K may be provided in two different ways. -// The first way is by having an overload of qHash() in K's namespace. namespace libtorrent { namespace aux { template -#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) - std::size_t qHash(const strong_typedef &key, const std::size_t seed = 0) -#else uint qHash(const strong_typedef &key, const uint seed = 0) -#endif { return ::qHash((std::hash> {})(key), seed); } } } +#endif diff --git a/src/base/digest32.h b/src/base/digest32.h index ae67458eb..111f82e60 100644 --- a/src/base/digest32.h +++ b/src/base/digest32.h @@ -131,12 +131,16 @@ bool operator<(const Digest32 &left, const Digest32 &right) < static_cast::UnderlyingType>(right); } -template #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) +template std::size_t qHash(const Digest32 &key, const std::size_t seed = 0) -#else -uint qHash(const Digest32 &key, const uint seed = 0) -#endif { - return ::qHash(std::hash::UnderlyingType>()(key), seed); + return ::qHash(static_cast::UnderlyingType>(key), seed); } +#else +template +uint qHash(const Digest32 &key, const uint seed = 0) +{ + return static_cast((std::hash::UnderlyingType> {})(key)) ^ seed; +} +#endif