From e0a23ba93d72c3b2c20261c8d4035cb5fa467b34 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 2 Oct 2019 12:52:51 +0800 Subject: [PATCH] Use InfoHash type in queueing operations This avoids redundant type conversions. --- src/base/bittorrent/session.cpp | 59 +++++++++++++++------------- src/base/bittorrent/session.h | 8 ++-- src/gui/transferlistwidget.cpp | 15 ++++--- src/webui/api/torrentscontroller.cpp | 18 +++++++-- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index e2029a1ce..bcccf463f 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef Q_OS_WIN #include @@ -1699,22 +1700,23 @@ bool Session::cancelLoadMetadata(const InfoHash &hash) return true; } -void Session::increaseTorrentsQueuePos(const QStringList &hashes) +void Session::increaseTorrentsQueuePos(const QVector &hashes) { - std::priority_queue, - std::vector>, - std::greater>> torrentQueue; + using ElementType = std::pair; + std::priority_queue + , std::greater> torrentQueue; // Sort torrents by queue position - for (const InfoHash infoHash : hashes) { + for (const InfoHash &infoHash : hashes) { TorrentHandle *const torrent = m_torrents.value(infoHash); if (torrent && !torrent->isSeed()) - torrentQueue.push(qMakePair(torrent->queuePosition(), torrent)); + torrentQueue.emplace(torrent->queuePosition(), torrent); } // Increase torrents queue position (starting with the one in the highest queue position) while (!torrentQueue.empty()) { - TorrentHandle *const torrent = torrentQueue.top().second; + const TorrentHandle *torrent = torrentQueue.top().second; torrentQueuePositionUp(torrent->nativeHandle()); torrentQueue.pop(); } @@ -1722,22 +1724,21 @@ void Session::increaseTorrentsQueuePos(const QStringList &hashes) saveTorrentsQueue(); } -void Session::decreaseTorrentsQueuePos(const QStringList &hashes) +void Session::decreaseTorrentsQueuePos(const QVector &hashes) { - std::priority_queue, - std::vector>, - std::less>> torrentQueue; + using ElementType = std::pair; + std::priority_queue torrentQueue; // Sort torrents by queue position - for (const InfoHash infoHash : hashes) { + for (const InfoHash &infoHash : hashes) { TorrentHandle *const torrent = m_torrents.value(infoHash); if (torrent && !torrent->isSeed()) - torrentQueue.push(qMakePair(torrent->queuePosition(), torrent)); + torrentQueue.emplace(torrent->queuePosition(), torrent); } // Decrease torrents queue position (starting with the one in the lowest queue position) while (!torrentQueue.empty()) { - TorrentHandle *const torrent = torrentQueue.top().second; + const TorrentHandle *torrent = torrentQueue.top().second; torrentQueuePositionDown(torrent->nativeHandle()); torrentQueue.pop(); } @@ -1748,22 +1749,23 @@ void Session::decreaseTorrentsQueuePos(const QStringList &hashes) saveTorrentsQueue(); } -void Session::topTorrentsQueuePos(const QStringList &hashes) +void Session::topTorrentsQueuePos(const QVector &hashes) { - std::priority_queue, - std::vector>, - std::greater>> torrentQueue; + using ElementType = std::pair; + std::priority_queue + , std::greater> torrentQueue; // Sort torrents by queue position - for (const InfoHash infoHash : hashes) { + for (const InfoHash &infoHash : hashes) { TorrentHandle *const torrent = m_torrents.value(infoHash); if (torrent && !torrent->isSeed()) - torrentQueue.push(qMakePair(torrent->queuePosition(), torrent)); + torrentQueue.emplace(torrent->queuePosition(), torrent); } // Top torrents queue position (starting with the one in the highest queue position) while (!torrentQueue.empty()) { - TorrentHandle *const torrent = torrentQueue.top().second; + const TorrentHandle *torrent = torrentQueue.top().second; torrentQueuePositionTop(torrent->nativeHandle()); torrentQueue.pop(); } @@ -1771,22 +1773,23 @@ void Session::topTorrentsQueuePos(const QStringList &hashes) saveTorrentsQueue(); } -void Session::bottomTorrentsQueuePos(const QStringList &hashes) +void Session::bottomTorrentsQueuePos(const QVector &hashes) { - std::priority_queue, - std::vector>, - std::less>> torrentQueue; + using ElementType = std::pair; + std::priority_queue + , std::less> torrentQueue; // Sort torrents by queue position - for (const InfoHash infoHash : hashes) { + for (const InfoHash &infoHash : hashes) { TorrentHandle *const torrent = m_torrents.value(infoHash); if (torrent && !torrent->isSeed()) - torrentQueue.push(qMakePair(torrent->queuePosition(), torrent)); + torrentQueue.emplace(torrent->queuePosition(), torrent); } // Bottom torrents queue position (starting with the one in the lowest queue position) while (!torrentQueue.empty()) { - TorrentHandle *const torrent = torrentQueue.top().second; + const TorrentHandle *torrent = torrentQueue.top().second; torrentQueuePositionBottom(torrent->nativeHandle()); torrentQueue.pop(); } diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 1dfbbfa42..4209be95f 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -412,10 +412,10 @@ namespace BitTorrent bool cancelLoadMetadata(const InfoHash &hash); void recursiveTorrentDownload(const InfoHash &hash); - void increaseTorrentsQueuePos(const QStringList &hashes); - void decreaseTorrentsQueuePos(const QStringList &hashes); - void topTorrentsQueuePos(const QStringList &hashes); - void bottomTorrentsQueuePos(const QStringList &hashes); + void increaseTorrentsQueuePos(const QVector &hashes); + void decreaseTorrentsQueuePos(const QVector &hashes); + void topTorrentsQueuePos(const QVector &hashes); + void bottomTorrentsQueuePos(const QVector &hashes); // TorrentHandle interface void handleTorrentSaveResumeDataRequested(const TorrentHandle *torrent); diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index acf2c193a..3a776effd 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -43,6 +43,7 @@ #include #include +#include "base/bittorrent/infohash.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrenthandle.h" #include "base/bittorrent/trackerentry.h" @@ -75,12 +76,12 @@ namespace { - QStringList extractHashes(const QVector &torrents) + QVector extractHashes(const QVector &torrents) { - QStringList hashes; - for (BitTorrent::TorrentHandle *const torrent : torrents) + QVector hashes; + hashes.reserve(torrents.size()); + for (const BitTorrent::TorrentHandle *torrent : torrents) hashes << torrent->hash(); - return hashes; } @@ -283,10 +284,12 @@ void TransferListWidget::torrentDoubleClicked() QVector TransferListWidget::getSelectedTorrents() const { + const QModelIndexList selectedRows = selectionModel()->selectedRows(); + QVector torrents; - for (const QModelIndex &index : asConst(selectionModel()->selectedRows())) + torrents.reserve(selectedRows.size()); + for (const QModelIndex &index : selectedRows) torrents << m_listModel->torrentHandle(mapToSource(index)); - return torrents; } diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index f75c894f6..22a722743 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -40,6 +40,7 @@ #include #include "base/bittorrent/downloadpriority.h" +#include "base/bittorrent/infohash.h" #include "base/bittorrent/peeraddress.h" #include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" @@ -197,6 +198,15 @@ namespace return {dht, pex, lsd}; } + + QVector toInfoHashes(const QStringList &hashes) + { + QVector infoHashes; + infoHashes.reserve(hashes.size()); + for (const QString &hash : hashes) + infoHashes << hash; + return infoHashes; + } } // Returns all the torrents in JSON format. @@ -901,7 +911,7 @@ void TorrentsController::increasePrioAction() throw APIError(APIErrorType::Conflict, tr("Torrent queueing must be enabled")); const QStringList hashes {params()["hashes"].split('|')}; - BitTorrent::Session::instance()->increaseTorrentsQueuePos(hashes); + BitTorrent::Session::instance()->increaseTorrentsQueuePos(toInfoHashes(hashes)); } void TorrentsController::decreasePrioAction() @@ -912,7 +922,7 @@ void TorrentsController::decreasePrioAction() throw APIError(APIErrorType::Conflict, tr("Torrent queueing must be enabled")); const QStringList hashes {params()["hashes"].split('|')}; - BitTorrent::Session::instance()->decreaseTorrentsQueuePos(hashes); + BitTorrent::Session::instance()->decreaseTorrentsQueuePos(toInfoHashes(hashes)); } void TorrentsController::topPrioAction() @@ -923,7 +933,7 @@ void TorrentsController::topPrioAction() throw APIError(APIErrorType::Conflict, tr("Torrent queueing must be enabled")); const QStringList hashes {params()["hashes"].split('|')}; - BitTorrent::Session::instance()->topTorrentsQueuePos(hashes); + BitTorrent::Session::instance()->topTorrentsQueuePos(toInfoHashes(hashes)); } void TorrentsController::bottomPrioAction() @@ -934,7 +944,7 @@ void TorrentsController::bottomPrioAction() throw APIError(APIErrorType::Conflict, tr("Torrent queueing must be enabled")); const QStringList hashes {params()["hashes"].split('|')}; - BitTorrent::Session::instance()->bottomTorrentsQueuePos(hashes); + BitTorrent::Session::instance()->bottomTorrentsQueuePos(toInfoHashes(hashes)); } void TorrentsController::setLocationAction()