Cache native torrent info to avoid extra blocking calls

This commit is contained in:
Vladimir Golovnev (Glassez) 2022-04-25 09:25:45 +03:00
parent c740d105c9
commit 1069bea273
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
2 changed files with 19 additions and 7 deletions

View file

@ -840,7 +840,7 @@ Path TorrentImpl::filePath(const int index) const
Path TorrentImpl::actualFilePath(const int index) const Path TorrentImpl::actualFilePath(const int index) const
{ {
const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index); 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 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; m_trackerPeerCounts[trackerUrl][endpoint] = count;
} }
std::shared_ptr<const libtorrent::torrent_info> 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) void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathList &fileNames)
{ {
Q_ASSERT(m_filePaths.isEmpty()); Q_ASSERT(m_filePaths.isEmpty());
lt::add_torrent_params &p = m_ltAddTorrentParams; lt::add_torrent_params &p = m_ltAddTorrentParams;
const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file()); const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(nativeTorrentInfo());
m_torrentInfo = TorrentInfo(*metadata); m_torrentInfo = TorrentInfo(*metadata);
m_filePriorities.reserve(filesCount()); m_filePriorities.reserve(filesCount());
m_completedFiles.resize(filesCount()); m_completedFiles.resize(filesCount());
@ -1625,7 +1632,7 @@ void TorrentImpl::resume(const TorrentOperatingMode mode)
{ {
m_hasMissingFiles = false; m_hasMissingFiles = false;
m_isStopped = false; m_isStopped = false;
m_ltAddTorrentParams.ti = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file()); m_ltAddTorrentParams.ti = std::const_pointer_cast<lt::torrent_info>(nativeTorrentInfo());
reload(); reload();
return; return;
} }
@ -1688,7 +1695,7 @@ void TorrentImpl::handleMoveStorageJobFinished(const Path &path, const bool hasO
// it can be moved to the proper location // it can be moved to the proper location
m_hasMissingFiles = false; m_hasMissingFiles = false;
m_ltAddTorrentParams.save_path = m_nativeStatus.save_path; m_ltAddTorrentParams.save_path = m_nativeStatus.save_path;
m_ltAddTorrentParams.ti = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file()); m_ltAddTorrentParams.ti = std::const_pointer_cast<lt::torrent_info>(nativeTorrentInfo());
reload(); reload();
} }
@ -1787,7 +1794,7 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p)
m_ltAddTorrentParams.have_pieces.clear(); m_ltAddTorrentParams.have_pieces.clear();
m_ltAddTorrentParams.verified_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; const auto &renamedFiles = m_ltAddTorrentParams.renamed_files;
PathList filePaths = metadata.filePaths(); PathList filePaths = metadata.filePaths();
@ -2059,8 +2066,9 @@ void TorrentImpl::handleAlert(const lt::alert *a)
void TorrentImpl::manageIncompleteFiles() void TorrentImpl::manageIncompleteFiles()
{ {
const std::shared_ptr<const lt::torrent_info> nativeInfo = nativeTorrentInfo();
const lt::file_storage &nativeFiles = nativeInfo->files();
const bool isAppendExtensionEnabled = m_session->isAppendExtensionEnabled(); const bool isAppendExtensionEnabled = m_session->isAppendExtensionEnabled();
const lt::file_storage &nativeFiles = m_nativeHandle.torrent_file()->files();
for (int i = 0; i < filesCount(); ++i) for (int i = 0; i < filesCount(); ++i)
{ {

View file

@ -30,11 +30,13 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <memory>
#include <libtorrent/add_torrent_params.hpp> #include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/fwd.hpp> #include <libtorrent/fwd.hpp>
#include <libtorrent/socket.hpp> #include <libtorrent/socket.hpp>
#include <libtorrent/torrent_handle.hpp> #include <libtorrent/torrent_handle.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/torrent_status.hpp> #include <libtorrent/torrent_status.hpp>
#include <QBitArray> #include <QBitArray>
@ -246,6 +248,8 @@ namespace BitTorrent
private: private:
using EventTrigger = std::function<void ()>; using EventTrigger = std::function<void ()>;
std::shared_ptr<const lt::torrent_info> nativeTorrentInfo() const;
void updateStatus(const lt::torrent_status &nativeStatus); void updateStatus(const lt::torrent_status &nativeStatus);
void updateState(); void updateState();
@ -282,7 +286,7 @@ namespace BitTorrent
Session *const m_session; Session *const m_session;
lt::session *m_nativeSession; lt::session *m_nativeSession;
lt::torrent_handle m_nativeHandle; lt::torrent_handle m_nativeHandle;
lt::torrent_status m_nativeStatus; mutable lt::torrent_status m_nativeStatus;
TorrentState m_state = TorrentState::Unknown; TorrentState m_state = TorrentState::Unknown;
TorrentInfo m_torrentInfo; TorrentInfo m_torrentInfo;
PathList m_filePaths; PathList m_filePaths;