From 1069bea2732cc5f13f7ca7ff0464c0211790f0ac Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 25 Apr 2022 09:25:45 +0300 Subject: [PATCH] Cache native torrent info to avoid extra blocking calls --- src/base/bittorrent/torrentimpl.cpp | 20 ++++++++++++++------ src/base/bittorrent/torrentimpl.h | 6 +++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 2dba2e98e..68e78703b 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -840,7 +840,7 @@ Path TorrentImpl::filePath(const int index) const Path TorrentImpl::actualFilePath(const int index) const { const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index); - return Path(m_nativeHandle.torrent_file()->files().file_path(nativeIndex)); + return Path(nativeTorrentInfo()->files().file_path(nativeIndex)); } qlonglong TorrentImpl::fileSize(const int index) const @@ -1514,13 +1514,20 @@ void TorrentImpl::updatePeerCount(const QString &trackerUrl, const lt::tcp::endp m_trackerPeerCounts[trackerUrl][endpoint] = count; } +std::shared_ptr TorrentImpl::nativeTorrentInfo() const +{ + if (m_nativeStatus.torrent_file.expired()) + m_nativeStatus.torrent_file = m_nativeHandle.torrent_file(); + return m_nativeStatus.torrent_file.lock(); +} + void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathList &fileNames) { Q_ASSERT(m_filePaths.isEmpty()); lt::add_torrent_params &p = m_ltAddTorrentParams; - const std::shared_ptr metadata = std::const_pointer_cast(m_nativeHandle.torrent_file()); + const std::shared_ptr metadata = std::const_pointer_cast(nativeTorrentInfo()); m_torrentInfo = TorrentInfo(*metadata); m_filePriorities.reserve(filesCount()); m_completedFiles.resize(filesCount()); @@ -1625,7 +1632,7 @@ void TorrentImpl::resume(const TorrentOperatingMode mode) { m_hasMissingFiles = false; m_isStopped = false; - m_ltAddTorrentParams.ti = std::const_pointer_cast(m_nativeHandle.torrent_file()); + m_ltAddTorrentParams.ti = std::const_pointer_cast(nativeTorrentInfo()); reload(); return; } @@ -1688,7 +1695,7 @@ void TorrentImpl::handleMoveStorageJobFinished(const Path &path, const bool hasO // it can be moved to the proper location m_hasMissingFiles = false; m_ltAddTorrentParams.save_path = m_nativeStatus.save_path; - m_ltAddTorrentParams.ti = std::const_pointer_cast(m_nativeHandle.torrent_file()); + m_ltAddTorrentParams.ti = std::const_pointer_cast(nativeTorrentInfo()); reload(); } @@ -1787,7 +1794,7 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p) m_ltAddTorrentParams.have_pieces.clear(); m_ltAddTorrentParams.verified_pieces.clear(); - TorrentInfo metadata = TorrentInfo(*m_nativeHandle.torrent_file()); + TorrentInfo metadata = TorrentInfo(*nativeTorrentInfo()); const auto &renamedFiles = m_ltAddTorrentParams.renamed_files; PathList filePaths = metadata.filePaths(); @@ -2059,8 +2066,9 @@ void TorrentImpl::handleAlert(const lt::alert *a) void TorrentImpl::manageIncompleteFiles() { + const std::shared_ptr nativeInfo = nativeTorrentInfo(); + const lt::file_storage &nativeFiles = nativeInfo->files(); const bool isAppendExtensionEnabled = m_session->isAppendExtensionEnabled(); - const lt::file_storage &nativeFiles = m_nativeHandle.torrent_file()->files(); for (int i = 0; i < filesCount(); ++i) { diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index 38da742d4..af6a297cf 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -30,11 +30,13 @@ #pragma once #include +#include #include #include #include #include +#include #include #include @@ -246,6 +248,8 @@ namespace BitTorrent private: using EventTrigger = std::function; + std::shared_ptr nativeTorrentInfo() const; + void updateStatus(const lt::torrent_status &nativeStatus); void updateState(); @@ -282,7 +286,7 @@ namespace BitTorrent Session *const m_session; lt::session *m_nativeSession; lt::torrent_handle m_nativeHandle; - lt::torrent_status m_nativeStatus; + mutable lt::torrent_status m_nativeStatus; TorrentState m_state = TorrentState::Unknown; TorrentInfo m_torrentInfo; PathList m_filePaths;