From 10499dffe913d2e4c15c703fd2e1660ab65327d8 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Tue, 5 Nov 2024 16:43:43 +0300 Subject: [PATCH] Optimize conversion of time points from libtorrent to Qt clocks Obtain current date time of Qt and libtorrent clocks only once for processing entire current libtorrent alerts bunch. PR #21764. --- src/base/bittorrent/sessionimpl.cpp | 11 +++++++++++ src/base/bittorrent/sessionimpl.h | 5 +++++ src/base/bittorrent/torrentimpl.cpp | 21 ++++++++++----------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index e962a125e..f09d9338b 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -5496,6 +5496,11 @@ void SessionImpl::setTorrentContentLayout(const TorrentContentLayout value) // Read alerts sent by libtorrent session void SessionImpl::readAlerts() { + // cache current datetime of Qt and libtorrent clocks in order + // to optimize conversion of time points from lt to Qt clocks + m_ltNow = lt::clock_type::now(); + m_qNow = QDateTime::currentDateTime(); + const std::vector alerts = getPendingAlerts(); Q_ASSERT(m_loadedTorrents.isEmpty()); @@ -6359,3 +6364,9 @@ void SessionImpl::handleRemovedTorrent(const TorrentID &torrentID, const QString m_removingTorrents.erase(removingTorrentDataIter); } + +QDateTime SessionImpl::fromLTTimePoint32(const libtorrent::time_point32 &timePoint) const +{ + const auto secsSinceNow = lt::duration_cast(timePoint - m_ltNow + lt::milliseconds(500)).count(); + return m_qNow.addSecs(secsSinceNow); +} diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index b387b52a1..ef0c6b401 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -475,6 +475,8 @@ namespace BitTorrent void addMappedPorts(const QSet &ports); void removeMappedPorts(const QSet &ports); + QDateTime fromLTTimePoint32(const lt::time_point32 &timePoint) const; + template void invoke(Func &&func) { @@ -820,6 +822,9 @@ namespace BitTorrent QList m_pendingFinishedTorrents; + QDateTime m_qNow; + lt::clock_type::time_point m_ltNow; + friend void Session::initInstance(); friend void Session::freeInstance(); friend Session *Session::instance(); diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 9cd370839..3ed0d53f4 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -92,22 +92,15 @@ namespace return entry; } - QDateTime fromLTTimePoint32(const lt::time_point32 &timePoint) - { - const auto ltNow = lt::clock_type::now(); - const auto qNow = QDateTime::currentDateTime(); - const auto secsSinceNow = lt::duration_cast(timePoint - ltNow + lt::milliseconds(500)).count(); - - return qNow.addSecs(secsSinceNow); - } - QString toString(const lt::tcp::endpoint <TCPEndpoint) { return QString::fromStdString((std::stringstream() << ltTCPEndpoint).str()); } + template void updateTrackerEntryStatus(TrackerEntryStatus &trackerEntryStatus, const lt::announce_entry &nativeEntry - , const QSet &btProtocols, const QHash> &updateInfo) + , const QSet &btProtocols, const QHash> &updateInfo + , const FromLTTimePoint32Func &fromLTTimePoint32) { Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url)); @@ -1759,7 +1752,13 @@ TrackerEntryStatus TorrentImpl::updateTrackerEntryStatus(const lt::announce_entr #else const QSet btProtocols {1}; #endif - ::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo); + + const auto fromLTTimePoint32 = [this](const lt::time_point32 &timePoint) + { + return m_session->fromLTTimePoint32(timePoint); + }; + ::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo, fromLTTimePoint32); + return *it; }