From 24cd7c3611056d476c38c623a001b87ad5bf5faf Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 4 Mar 2020 02:53:31 +0800 Subject: [PATCH] Avoid unnecessary memory writes Before this change, qbt spent ~1% in these two functions, now it only spends about ~0.5% in my naive testing. --- src/base/bittorrent/peerinfo.cpp | 9 ++++----- src/base/bittorrent/torrenthandle.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/base/bittorrent/peerinfo.cpp b/src/base/bittorrent/peerinfo.cpp index 60d0fc4b8..a95a99af8 100644 --- a/src/base/bittorrent/peerinfo.cpp +++ b/src/base/bittorrent/peerinfo.cpp @@ -208,11 +208,10 @@ qlonglong PeerInfo::totalDownload() const QBitArray PeerInfo::pieces() const { QBitArray result(m_nativeInfo.pieces.size()); - - int i = 0; - for (const bool bit : m_nativeInfo.pieces) - result.setBit(i++, bit); - + for (int i = 0; i < result.size(); ++i) { + if (m_nativeInfo.pieces[i]) + result.setBit(i, true); + } return result; } diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 8a8e4fd71..44d48ee36 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1202,10 +1202,10 @@ QVector TorrentHandle::peers() const QBitArray TorrentHandle::pieces() const { QBitArray result(m_nativeStatus.pieces.size()); - - for (int i = 0; i < m_nativeStatus.pieces.size(); ++i) - result.setBit(i, m_nativeStatus.pieces.get_bit(LTPieceIndex {i})); - + for (int i = 0; i < result.size(); ++i) { + if (m_nativeStatus.pieces[LTPieceIndex {i}]) + result.setBit(i, true); + } return result; }