From ff99e5ac9a30a720cc23c6fccd3037a212ad1506 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 24 Jan 2022 10:41:25 +0800 Subject: [PATCH] Speed up piece relevance calculation For ~800 pieces, this roughly cuts the run time (of this function) in half. --- src/base/bittorrent/peerinfo.cpp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/base/bittorrent/peerinfo.cpp b/src/base/bittorrent/peerinfo.cpp index 8d8c05f2a..835c85c08 100644 --- a/src/base/bittorrent/peerinfo.cpp +++ b/src/base/bittorrent/peerinfo.cpp @@ -229,25 +229,16 @@ QString PeerInfo::connectionType() const void PeerInfo::calcRelevance(const Torrent *torrent) { const QBitArray allPieces = torrent->pieces(); - const QBitArray peerPieces = pieces(); - - int localMissing = 0; - int remoteHaves = 0; - - for (int i = 0; i < allPieces.size(); ++i) + const int localMissing = allPieces.count(false); + if (localMissing <= 0) { - if (!allPieces[i]) - { - ++localMissing; - if (peerPieces[i]) - ++remoteHaves; - } + m_relevance = 0; + return; } - if (localMissing == 0) - m_relevance = 0.0; - else - m_relevance = static_cast(remoteHaves) / localMissing; + const QBitArray peerPieces = pieces(); + const int remoteHaves = (peerPieces & (~allPieces)).count(true); + m_relevance = static_cast(remoteHaves) / localMissing; } qreal PeerInfo::relevance() const