From 71174edf72d6cb0c39b8ba6df1d5ac8ed80bec03 Mon Sep 17 00:00:00 2001 From: Prince Gupta <34717789+jagannatharjun@users.noreply.github.com> Date: Tue, 1 Feb 2022 10:59:54 +0530 Subject: [PATCH] Optimize completed files handling PR #16329. Co-authored-by: Vladimir Golovnev (Glassez) --- src/base/bittorrent/torrentimpl.cpp | 24 +++++++++++++++++------- src/base/bittorrent/torrentimpl.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 74ffbc4ae..7068083df 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -273,13 +273,16 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti); Q_ASSERT(m_filePaths.isEmpty()); + Q_ASSERT(m_indexMap.isEmpty()); const int filesCount = m_torrentInfo.filesCount(); m_filePaths.reserve(filesCount); + m_indexMap.reserve(filesCount); const std::shared_ptr currentInfo = m_nativeHandle.torrent_file(); const lt::file_storage &fileStorage = currentInfo->files(); for (int i = 0; i < filesCount; ++i) { const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(i); + m_indexMap[nativeIndex] = i; const QString filePath = Utils::Fs::toUniformPath(QString::fromStdString(fileStorage.file_path(nativeIndex))); m_filePaths.append(filePath); } @@ -1500,14 +1503,22 @@ void TorrentImpl::fileSearchFinished(const QString &savePath, const QStringList void TorrentImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames) { + Q_ASSERT(m_filePaths.isEmpty()); + Q_ASSERT(m_indexMap.isEmpty()); + lt::add_torrent_params &p = m_ltAddTorrentParams; const std::shared_ptr metadata = std::const_pointer_cast(m_nativeHandle.torrent_file()); m_torrentInfo = TorrentInfo(*metadata); m_filePaths = fileNames; + m_indexMap.reserve(filesCount()); const auto nativeIndexes = m_torrentInfo.nativeIndexes(); for (int i = 0; i < fileNames.size(); ++i) - p.renamed_files[nativeIndexes[i]] = fileNames[i].toStdString(); + { + const auto nativeIndex = nativeIndexes.at(i); + m_indexMap[nativeIndex] = i; + p.renamed_files[nativeIndex] = fileNames[i].toStdString(); + } p.save_path = Utils::Fs::toNativePath(savePath).toStdString(); p.ti = metadata; @@ -1862,7 +1873,7 @@ void TorrentImpl::handleFastResumeRejectedAlert(const lt::fastresume_rejected_al void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); + const int fileIndex = m_indexMap.value(p->index, -1); Q_ASSERT(fileIndex >= 0); // Remove empty leftover folders @@ -1908,7 +1919,7 @@ void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); + const int fileIndex = m_indexMap.value(p->index, -1); Q_ASSERT(fileIndex >= 0); LogMsg(tr("File rename failed. Torrent: \"%1\", file: \"%2\", reason: \"%3\"") @@ -1923,12 +1934,11 @@ void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); - Q_ASSERT(fileIndex >= 0); - - qDebug("A file completed download in torrent \"%s\"", qUtf8Printable(name())); if (m_session->isAppendExtensionEnabled()) { + const int fileIndex = m_indexMap.value(p->index, -1); + Q_ASSERT(fileIndex >= 0); + QString name = filePath(fileIndex); if (name.endsWith(QB_EXT)) { diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index d419514db..adbb8d5c1 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -286,6 +286,7 @@ namespace BitTorrent TorrentState m_state = TorrentState::Unknown; TorrentInfo m_torrentInfo; QStringList m_filePaths; + QHash m_indexMap; SpeedMonitor m_speedMonitor; InfoHash m_infoHash;