Browse Source

Merge pull request #16639 from Chocobo1/hash

Avoid redundant hashing
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
baebabbfd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/base/bittorrent/infohash.cpp
  2. 7
      src/base/bittorrent/infohash.h
  3. 9
      src/base/bittorrent/ltqhash.h
  4. 7
      src/base/bittorrent/peeraddress.cpp
  5. 7
      src/base/bittorrent/peeraddress.h
  6. 4
      src/base/bittorrent/torrent.cpp
  7. 7
      src/base/bittorrent/torrent.h
  8. 4
      src/base/bittorrent/tracker.cpp
  9. 7
      src/base/bittorrent/tracker.h
  10. 7
      src/base/bittorrent/trackerentry.cpp
  11. 6
      src/base/bittorrent/trackerentry.h
  12. 13
      src/base/digest32.h
  13. 7
      src/base/net/downloadmanager.cpp
  14. 7
      src/base/net/downloadmanager.h
  15. 4
      src/base/path.cpp
  16. 7
      src/base/path.h
  17. 10
      src/gui/properties/peerlistwidget.cpp

6
src/base/bittorrent/infohash.cpp

@ -90,9 +90,13 @@ BitTorrent::TorrentID BitTorrent::TorrentID::fromInfoHash(const BitTorrent::Info
return infoHash.toTorrentID(); return infoHash.toTorrentID();
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t BitTorrent::qHash(const BitTorrent::TorrentID &key, const std::size_t seed)
#else
uint BitTorrent::qHash(const BitTorrent::TorrentID &key, const uint seed) uint BitTorrent::qHash(const BitTorrent::TorrentID &key, const uint seed)
#endif
{ {
return ::qHash(std::hash<TorrentID::UnderlyingType>()(key), seed); return ::qHash(static_cast<TorrentID::BaseType>(key), seed);
} }
bool BitTorrent::operator==(const BitTorrent::InfoHash &left, const BitTorrent::InfoHash &right) bool BitTorrent::operator==(const BitTorrent::InfoHash &left, const BitTorrent::InfoHash &right)

7
src/base/bittorrent/infohash.h

@ -32,6 +32,7 @@
#include <libtorrent/info_hash.hpp> #include <libtorrent/info_hash.hpp>
#endif #endif
#include <QtGlobal>
#include <QHash> #include <QHash>
#include <QMetaType> #include <QMetaType>
@ -81,7 +82,11 @@ namespace BitTorrent
WrappedType m_nativeHash; WrappedType m_nativeHash;
}; };
uint qHash(const TorrentID &key, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const TorrentID &key, std::size_t seed = 0);
#else
uint qHash(const TorrentID &key, uint seed = 0);
#endif
bool operator==(const InfoHash &left, const InfoHash &right); bool operator==(const InfoHash &left, const InfoHash &right);
bool operator!=(const InfoHash &left, const InfoHash &right); bool operator!=(const InfoHash &left, const InfoHash &right);

9
src/base/bittorrent/ltqhash.h

@ -28,23 +28,24 @@
#pragma once #pragma once
#include <QtGlobal>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <functional> #include <functional>
#include <libtorrent/units.hpp> #include <libtorrent/units.hpp>
#include <QHash> #include <QHash>
// 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 libtorrent
{ {
namespace aux namespace aux
{ {
template <typename T, typename Tag> template <typename T, typename Tag>
uint qHash(const strong_typedef<T, Tag> &key, const uint seed) uint qHash(const strong_typedef<T, Tag> &key, const uint seed = 0)
{ {
return ::qHash((std::hash<strong_typedef<T, Tag>> {})(key), seed); return ::qHash((std::hash<strong_typedef<T, Tag>> {})(key), seed);
} }
} }
} }
#endif

7
src/base/bittorrent/peeraddress.cpp

@ -77,7 +77,14 @@ bool BitTorrent::operator==(const BitTorrent::PeerAddress &left, const BitTorren
return (left.ip == right.ip) && (left.port == right.port); return (left.ip == right.ip) && (left.port == right.port);
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t BitTorrent::qHash(const BitTorrent::PeerAddress &addr, const std::size_t seed)
{
return qHashMulti(seed, addr.ip, addr.port);
}
#else
uint BitTorrent::qHash(const BitTorrent::PeerAddress &addr, const uint seed) uint BitTorrent::qHash(const BitTorrent::PeerAddress &addr, const uint seed)
{ {
return (::qHash(addr.ip, seed) ^ ::qHash(addr.port)); return (::qHash(addr.ip, seed) ^ ::qHash(addr.port));
} }
#endif

7
src/base/bittorrent/peeraddress.h

@ -28,6 +28,7 @@
#pragma once #pragma once
#include <QtGlobal>
#include <QHostAddress> #include <QHostAddress>
class QString; class QString;
@ -44,5 +45,9 @@ namespace BitTorrent
}; };
bool operator==(const PeerAddress &left, const PeerAddress &right); bool operator==(const PeerAddress &left, const PeerAddress &right);
uint qHash(const PeerAddress &addr, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const PeerAddress &addr, std::size_t seed = 0);
#else
uint qHash(const PeerAddress &addr, uint seed = 0);
#endif
} }

4
src/base/bittorrent/torrent.cpp

@ -35,7 +35,11 @@
namespace BitTorrent namespace BitTorrent
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const TorrentState key, const std::size_t seed)
#else
uint qHash(const TorrentState key, const uint seed) uint qHash(const TorrentState key, const uint seed)
#endif
{ {
return ::qHash(static_cast<std::underlying_type_t<TorrentState>>(key), seed); return ::qHash(static_cast<std::underlying_type_t<TorrentState>>(key), seed);
} }

7
src/base/bittorrent/torrent.h

@ -29,6 +29,7 @@
#pragma once #pragma once
#include <QtGlobal>
#include <QtContainerFwd> #include <QtContainerFwd>
#include <QMetaType> #include <QMetaType>
#include <QString> #include <QString>
@ -97,7 +98,11 @@ namespace BitTorrent
Error Error
}; };
uint qHash(TorrentState key, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(TorrentState key, std::size_t seed = 0);
#else
uint qHash(TorrentState key, uint seed = 0);
#endif
class Torrent : public AbstractFileStorage class Torrent : public AbstractFileStorage
{ {

4
src/base/bittorrent/tracker.cpp

@ -140,7 +140,11 @@ namespace BitTorrent
return !(left == right); return !(left == right);
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const Peer &key, const std::size_t seed)
#else
uint qHash(const Peer &key, const uint seed) uint qHash(const Peer &key, const uint seed)
#endif
{ {
return qHash(key.uniqueID(), seed); return qHash(key.uniqueID(), seed);
} }

7
src/base/bittorrent/tracker.h

@ -34,6 +34,7 @@
#include <libtorrent/entry.hpp> #include <libtorrent/entry.hpp>
#include <QtGlobal>
#include <QHash> #include <QHash>
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
@ -64,7 +65,11 @@ namespace BitTorrent
bool operator==(const Peer &left, const Peer &right); bool operator==(const Peer &left, const Peer &right);
bool operator!=(const Peer &left, const Peer &right); bool operator!=(const Peer &left, const Peer &right);
uint qHash(const Peer &key, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const Peer &key, std::size_t seed = 0);
#else
uint qHash(const Peer &key, uint seed = 0);
#endif
// *Basic* Bittorrent tracker implementation // *Basic* Bittorrent tracker implementation
// [BEP-3] The BitTorrent Protocol Specification // [BEP-3] The BitTorrent Protocol Specification

7
src/base/bittorrent/trackerentry.cpp

@ -36,7 +36,14 @@ bool BitTorrent::operator==(const TrackerEntry &left, const TrackerEntry &right)
&& QUrl(left.url) == QUrl(right.url)); && QUrl(left.url) == QUrl(right.url));
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t BitTorrent::qHash(const TrackerEntry &key, const std::size_t seed)
{
return qHashMulti(seed, key.url, key.tier);
}
#else
uint BitTorrent::qHash(const TrackerEntry &key, const uint seed) uint BitTorrent::qHash(const TrackerEntry &key, const uint seed)
{ {
return (::qHash(key.url, seed) ^ ::qHash(key.tier)); return (::qHash(key.url, seed) ^ ::qHash(key.tier));
} }
#endif

6
src/base/bittorrent/trackerentry.h

@ -71,5 +71,9 @@ namespace BitTorrent
}; };
bool operator==(const TrackerEntry &left, const TrackerEntry &right); bool operator==(const TrackerEntry &left, const TrackerEntry &right);
uint qHash(const TrackerEntry &key, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const TrackerEntry &key, std::size_t seed = 0);
#else
uint qHash(const TrackerEntry &key, uint seed = 0);
#endif
} }

13
src/base/digest32.h

@ -30,6 +30,7 @@
#include <libtorrent/sha1_hash.hpp> #include <libtorrent/sha1_hash.hpp>
#include <QtGlobal>
#include <QByteArray> #include <QByteArray>
#include <QHash> #include <QHash>
#include <QSharedData> #include <QSharedData>
@ -130,8 +131,16 @@ bool operator<(const Digest32<N> &left, const Digest32<N> &right)
< static_cast<typename Digest32<N>::UnderlyingType>(right); < static_cast<typename Digest32<N>::UnderlyingType>(right);
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
template <int N> template <int N>
uint qHash(const Digest32<N> &key, const uint seed) std::size_t qHash(const Digest32<N> &key, const std::size_t seed = 0)
{ {
return ::qHash(std::hash<typename Digest32<N>::UnderlyingType>()(key), seed); return ::qHash(static_cast<typename Digest32<N>::UnderlyingType>(key), seed);
} }
#else
template <int N>
uint qHash(const Digest32<N> &key, const uint seed = 0)
{
return static_cast<uint>((std::hash<typename Digest32<N>::UnderlyingType> {})(key)) ^ seed;
}
#endif

7
src/base/net/downloadmanager.cpp

@ -364,10 +364,17 @@ Net::ServiceID Net::ServiceID::fromURL(const QUrl &url)
return {url.host(), url.port(80)}; return {url.host(), url.port(80)};
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t Net::qHash(const ServiceID &serviceID, const std::size_t seed)
{
return qHashMulti(seed, serviceID.hostName, serviceID.port);
}
#else
uint Net::qHash(const ServiceID &serviceID, const uint seed) uint Net::qHash(const ServiceID &serviceID, const uint seed)
{ {
return ::qHash(serviceID.hostName, seed) ^ ::qHash(serviceID.port); return ::qHash(serviceID.hostName, seed) ^ ::qHash(serviceID.port);
} }
#endif
bool Net::operator==(const ServiceID &lhs, const ServiceID &rhs) bool Net::operator==(const ServiceID &lhs, const ServiceID &rhs)
{ {

7
src/base/net/downloadmanager.h

@ -29,6 +29,7 @@
#pragma once #pragma once
#include <QtGlobal>
#include <QHash> #include <QHash>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QObject> #include <QObject>
@ -52,7 +53,11 @@ namespace Net
static ServiceID fromURL(const QUrl &url); static ServiceID fromURL(const QUrl &url);
}; };
uint qHash(const ServiceID &serviceID, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const ServiceID &serviceID, std::size_t seed = 0);
#else
uint qHash(const ServiceID &serviceID, uint seed = 0);
#endif
bool operator==(const ServiceID &lhs, const ServiceID &rhs); bool operator==(const ServiceID &lhs, const ServiceID &rhs);
enum class DownloadStatus enum class DownloadStatus

4
src/base/path.cpp

@ -332,7 +332,11 @@ QDataStream &operator>>(QDataStream &in, Path &path)
return in; return in;
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const Path &key, const std::size_t seed)
#else
uint qHash(const Path &key, const uint seed) uint qHash(const Path &key, const uint seed)
#endif
{ {
return ::qHash(key.data(), seed); return ::qHash(key.data(), seed);
} }

7
src/base/path.h

@ -29,6 +29,7 @@
#pragma once #pragma once
#include <QtGlobal>
#include <QMetaType> #include <QMetaType>
#include <QString> #include <QString>
@ -96,4 +97,8 @@ Path operator+(const Path &lhs, const std::string &rhs);
QDataStream &operator<<(QDataStream &out, const Path &path); QDataStream &operator<<(QDataStream &out, const Path &path);
QDataStream &operator>>(QDataStream &in, Path &path); QDataStream &operator>>(QDataStream &in, Path &path);
uint qHash(const Path &key, uint seed); #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const Path &key, std::size_t seed = 0);
#else
uint qHash(const Path &key, uint seed = 0);
#endif

10
src/gui/properties/peerlistwidget.cpp

@ -30,6 +30,7 @@
#include <algorithm> #include <algorithm>
#include <QtGlobal>
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QHeaderView> #include <QHeaderView>
@ -70,10 +71,17 @@ bool operator==(const PeerEndpoint &left, const PeerEndpoint &right)
return (left.address == right.address) && (left.connectionType == right.connectionType); return (left.address == right.address) && (left.connectionType == right.connectionType);
} }
uint qHash(const PeerEndpoint &peerEndpoint, const uint seed) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
std::size_t qHash(const PeerEndpoint &peerEndpoint, const std::size_t seed = 0)
{
return qHashMulti(seed, peerEndpoint.address, peerEndpoint.connectionType);
}
#else
uint qHash(const PeerEndpoint &peerEndpoint, const uint seed = 0)
{ {
return (qHash(peerEndpoint.address, seed) ^ ::qHash(peerEndpoint.connectionType)); return (qHash(peerEndpoint.address, seed) ^ ::qHash(peerEndpoint.connectionType));
} }
#endif
PeerListWidget::PeerListWidget(PropertiesWidget *parent) PeerListWidget::PeerListWidget(PropertiesWidget *parent)
: QTreeView(parent) : QTreeView(parent)

Loading…
Cancel
Save