From da586828be1213d3a315baa817beafda49b27400 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 30 Oct 2022 15:07:39 +0300 Subject: [PATCH 1/3] Don't perform unnecessary actions with preloading magnets --- src/base/bittorrent/sessionimpl.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 6495a8fcf..6328760c7 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -3678,12 +3678,11 @@ void SessionImpl::setMaxConnectionsPerTorrent(int max) { m_maxConnectionsPerTorrent = max; - // Apply this to all session torrents - for (const lt::torrent_handle &handle : m_nativeSession->get_torrents()) + for (const TorrentImpl *torrent : asConst(m_torrents)) { try { - handle.set_max_connections(max); + torrent->nativeHandle().set_max_connections(max); } catch (const std::exception &) {} } @@ -3702,12 +3701,11 @@ void SessionImpl::setMaxUploadsPerTorrent(int max) { m_maxUploadsPerTorrent = max; - // Apply this to all session torrents - for (const lt::torrent_handle &handle : m_nativeSession->get_torrents()) + for (const TorrentImpl *torrent : asConst(m_torrents)) { try { - handle.set_max_uploads(max); + torrent->nativeHandle().set_max_uploads(max); } catch (const std::exception &) {} } @@ -4318,8 +4316,14 @@ void SessionImpl::setReannounceWhenAddressChangedEnabled(const bool enabled) void SessionImpl::reannounceToAllTrackers() const { - for (const lt::torrent_handle &torrent : m_nativeSession->get_torrents()) - torrent.force_reannounce(0, -1, lt::torrent_handle::ignore_min_interval); + for (const TorrentImpl *torrent : asConst(m_torrents)) + { + try + { + torrent->nativeHandle().force_reannounce(0, -1, lt::torrent_handle::ignore_min_interval); + } + catch (const std::exception &) {} + } } int SessionImpl::stopTrackerTimeout() const From 0b70ccf9e90af5bd979deafd20e3c8ea1dbfa7d8 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 3 Nov 2022 13:29:48 +0300 Subject: [PATCH 2/3] Cache torrent handles of preloading magnets --- src/base/bittorrent/sessionimpl.cpp | 21 +++++++++++---------- src/base/bittorrent/sessionimpl.h | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 6328760c7..2143fec14 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2293,7 +2293,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) if (downloadedMetadataIter == m_downloadedMetadata.end()) return false; - const lt::torrent_handle nativeHandle = m_nativeSession->find_torrent(id); + const lt::torrent_handle nativeHandle = downloadedMetadataIter.value(); #ifdef QBT_USES_LIBTORRENT2 const InfoHash infoHash {nativeHandle.info_hashes()}; if (infoHash.isHybrid()) @@ -2301,7 +2301,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) // if magnet link was hybrid initially then it is indexed also by v1 info hash // so we need to remove both entries const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); - m_downloadedMetadata.remove((altID == *downloadedMetadataIter) ? id : altID); + m_downloadedMetadata.remove((altID == downloadedMetadataIter.key()) ? id : altID); } #endif m_downloadedMetadata.erase(downloadedMetadataIter); @@ -2360,8 +2360,8 @@ void SessionImpl::decreaseTorrentsQueuePos(const QVector &ids) torrentQueue.pop(); } - for (auto i = m_downloadedMetadata.cbegin(); i != m_downloadedMetadata.cend(); ++i) - torrentQueuePositionBottom(m_nativeSession->find_torrent(*i)); + for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) + torrentQueuePositionBottom(torrentHandle); saveTorrentsQueue(); } @@ -2415,8 +2415,8 @@ void SessionImpl::bottomTorrentsQueuePos(const QVector &ids) torrentQueue.pop(); } - for (auto i = m_downloadedMetadata.cbegin(); i != m_downloadedMetadata.cend(); ++i) - torrentQueuePositionBottom(m_nativeSession->find_torrent(*i)); + for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) + torrentQueuePositionBottom(torrentHandle); saveTorrentsQueue(); } @@ -2833,16 +2833,17 @@ bool SessionImpl::downloadMetadata(const MagnetUri &magnetUri) // Adding torrent to libtorrent session lt::error_code ec; - lt::torrent_handle h = m_nativeSession->add_torrent(p, ec); - if (ec) return false; + lt::torrent_handle torrentHandle = m_nativeSession->add_torrent(p, ec); + if (ec) + return false; // waiting for metadata... - m_downloadedMetadata.insert(id); + m_downloadedMetadata.insert(id, torrentHandle); if (infoHash.isHybrid()) { // index hybrid magnet links by both v1 and v2 info hashes const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); - m_downloadedMetadata.insert(altID); + m_downloadedMetadata.insert(altID, torrentHandle); } ++m_extraLimit; adjustLimits(); diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index d7a747f61..6dc42b42c 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -694,7 +694,7 @@ namespace BitTorrent ResumeDataStorage *m_resumeDataStorage = nullptr; FileSearcher *m_fileSearcher = nullptr; - QSet m_downloadedMetadata; + QHash m_downloadedMetadata; QHash m_torrents; QHash m_hybridTorrentsByAltID; From 6aee7f95b7fd181eaeb49cc45606a4b0964fef6c Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 3 Nov 2022 14:19:17 +0300 Subject: [PATCH 3/3] Add torrent for preloading magnet asynchronously --- src/base/bittorrent/sessionimpl.cpp | 59 +++++++++++++++++++---------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 2143fec14..fee77216f 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2832,21 +2832,8 @@ bool SessionImpl::downloadMetadata(const MagnetUri &magnetUri) #endif // Adding torrent to libtorrent session - lt::error_code ec; - lt::torrent_handle torrentHandle = m_nativeSession->add_torrent(p, ec); - if (ec) - return false; - - // waiting for metadata... - m_downloadedMetadata.insert(id, torrentHandle); - if (infoHash.isHybrid()) - { - // index hybrid magnet links by both v1 and v2 info hashes - const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); - m_downloadedMetadata.insert(altID, torrentHandle); - } - ++m_extraLimit; - adjustLimits(); + m_nativeSession->async_add_torrent(p); + m_downloadedMetadata.insert(id, {}); return true; } @@ -5066,18 +5053,36 @@ void SessionImpl::handleAddTorrentAlerts(const std::vector &alerts) #else const InfoHash infoHash {(hasMetadata ? params.ti->info_hash() : params.info_hash)}; #endif - m_loadingTorrents.remove(TorrentID::fromInfoHash(infoHash)); + if (const auto loadingTorrentsIter = m_loadingTorrents.find(TorrentID::fromInfoHash(infoHash)) + ; loadingTorrentsIter != m_loadingTorrents.end()) + { + m_loadingTorrents.erase(loadingTorrentsIter); + } + else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(TorrentID::fromInfoHash(infoHash)) + ; downloadedMetadataIter != m_downloadedMetadata.end()) + { + m_downloadedMetadata.erase(downloadedMetadataIter); + if (infoHash.isHybrid()) + { + // index hybrid magnet links by both v1 and v2 info hashes + const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); + m_downloadedMetadata.remove(altID); + } + } return; } + #ifdef QBT_USES_LIBTORRENT2 - const auto torrentID = TorrentID::fromInfoHash(alert->handle.info_hashes()); + const InfoHash infoHash {alert->handle.info_hashes()}; #else - const auto torrentID = TorrentID::fromInfoHash(alert->handle.info_hash()); + const InfoHash infoHash {alert->handle.info_hash()}; #endif - const auto loadingTorrentsIter = m_loadingTorrents.find(torrentID); - if (loadingTorrentsIter != m_loadingTorrents.end()) + const auto torrentID = TorrentID::fromInfoHash(infoHash); + + if (const auto loadingTorrentsIter = m_loadingTorrents.find(torrentID) + ; loadingTorrentsIter != m_loadingTorrents.end()) { LoadTorrentParams params = loadingTorrentsIter.value(); m_loadingTorrents.erase(loadingTorrentsIter); @@ -5085,6 +5090,20 @@ void SessionImpl::handleAddTorrentAlerts(const std::vector &alerts) Torrent *torrent = createTorrent(alert->handle, params); loadedTorrents.append(torrent); } + else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(torrentID) + ; downloadedMetadataIter != m_downloadedMetadata.end()) + { + ++m_extraLimit; + adjustLimits(); + + downloadedMetadataIter.value() = alert->handle; + if (infoHash.isHybrid()) + { + // index hybrid magnet links by both v1 and v2 info hashes + const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); + m_downloadedMetadata[altID] = alert->handle; + } + } } if (!loadedTorrents.isEmpty())