mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-29 13:58:51 +03:00
Merge pull request #16247 from Chocobo1/count_bits
Speed up piece relevance calculation
This commit is contained in:
commit
9553afc3c2
2 changed files with 9 additions and 21 deletions
|
@ -39,8 +39,8 @@ using namespace BitTorrent;
|
|||
|
||||
PeerInfo::PeerInfo(const Torrent *torrent, const lt::peer_info &nativeInfo)
|
||||
: m_nativeInfo(nativeInfo)
|
||||
, m_relevance(calcRelevance(torrent))
|
||||
{
|
||||
calcRelevance(torrent);
|
||||
determineFlags();
|
||||
}
|
||||
|
||||
|
@ -226,28 +226,16 @@ QString PeerInfo::connectionType() const
|
|||
: QLatin1String {"Web"};
|
||||
}
|
||||
|
||||
void PeerInfo::calcRelevance(const Torrent *torrent)
|
||||
qreal PeerInfo::calcRelevance(const Torrent *torrent) const
|
||||
{
|
||||
const QBitArray allPieces = torrent->pieces();
|
||||
const int localMissing = allPieces.count(false);
|
||||
if (localMissing <= 0)
|
||||
return 0;
|
||||
|
||||
const QBitArray peerPieces = pieces();
|
||||
|
||||
int localMissing = 0;
|
||||
int remoteHaves = 0;
|
||||
|
||||
for (int i = 0; i < allPieces.size(); ++i)
|
||||
{
|
||||
if (!allPieces[i])
|
||||
{
|
||||
++localMissing;
|
||||
if (peerPieces[i])
|
||||
++remoteHaves;
|
||||
}
|
||||
}
|
||||
|
||||
if (localMissing == 0)
|
||||
m_relevance = 0.0;
|
||||
else
|
||||
m_relevance = static_cast<qreal>(remoteHaves) / localMissing;
|
||||
const int remoteHaves = (peerPieces & (~allPieces)).count(true);
|
||||
return static_cast<qreal>(remoteHaves) / localMissing;
|
||||
}
|
||||
|
||||
qreal PeerInfo::relevance() const
|
||||
|
|
|
@ -92,7 +92,7 @@ namespace BitTorrent
|
|||
int downloadingPieceIndex() const;
|
||||
|
||||
private:
|
||||
void calcRelevance(const Torrent *torrent);
|
||||
qreal calcRelevance(const Torrent *torrent) const;
|
||||
void determineFlags();
|
||||
|
||||
lt::peer_info m_nativeInfo = {};
|
||||
|
|
Loading…
Reference in a new issue