Avoid repeatedly creating the same QDateTime values

PR #21904.
This commit is contained in:
Vladimir Golovnev 2024-11-26 09:04:59 +03:00 committed by GitHub
parent e1bd1038c0
commit 15ea836bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 115 additions and 93 deletions

View file

@ -1610,7 +1610,7 @@ void SessionImpl::endStartup(ResumeSessionContext *context)
reannounceToAllTrackers(); reannounceToAllTrackers();
} }
m_wakeupCheckTimestamp = QDateTime::currentDateTime(); m_wakeupCheckTimestamp = now;
}); });
m_wakeupCheckTimestamp = QDateTime::currentDateTime(); m_wakeupCheckTimestamp = QDateTime::currentDateTime();
m_wakeupCheckTimer->start(30s); m_wakeupCheckTimer->start(30s);
@ -6396,7 +6396,7 @@ void SessionImpl::handleRemovedTorrent(const TorrentID &torrentID, const QString
m_removingTorrents.erase(removingTorrentDataIter); m_removingTorrents.erase(removingTorrentDataIter);
} }
QDateTime SessionImpl::fromLTTimePoint32(const libtorrent::time_point32 &timePoint) const QDateTime SessionImpl::fromLTTimePoint32(const lt::time_point32 &timePoint) const
{ {
const auto secsSinceNow = lt::duration_cast<lt::seconds>(timePoint - m_ltNow + lt::milliseconds(500)).count(); const auto secsSinceNow = lt::duration_cast<lt::seconds>(timePoint - m_ltNow + lt::milliseconds(500)).count();
return m_qNow.addSecs(secsSinceNow); return m_qNow.addSecs(secsSinceNow);

View file

@ -215,7 +215,15 @@ namespace BitTorrent
virtual int piecesCount() const = 0; virtual int piecesCount() const = 0;
virtual int piecesHave() const = 0; virtual int piecesHave() const = 0;
virtual qreal progress() const = 0; virtual qreal progress() const = 0;
virtual QDateTime addedTime() const = 0; virtual QDateTime addedTime() const = 0;
virtual QDateTime completedTime() const = 0;
virtual QDateTime lastSeenComplete() const = 0;
virtual qlonglong activeTime() const = 0;
virtual qlonglong finishedTime() const = 0;
virtual qlonglong timeSinceUpload() const = 0;
virtual qlonglong timeSinceDownload() const = 0;
virtual qlonglong timeSinceActivity() const = 0;
// Share limits // Share limits
virtual qreal ratioLimit() const = 0; virtual qreal ratioLimit() const = 0;
@ -254,8 +262,6 @@ namespace BitTorrent
virtual QString error() const = 0; virtual QString error() const = 0;
virtual qlonglong totalDownload() const = 0; virtual qlonglong totalDownload() const = 0;
virtual qlonglong totalUpload() const = 0; virtual qlonglong totalUpload() const = 0;
virtual qlonglong activeTime() const = 0;
virtual qlonglong finishedTime() const = 0;
virtual qlonglong eta() const = 0; virtual qlonglong eta() const = 0;
virtual int seedsCount() const = 0; virtual int seedsCount() const = 0;
virtual int peersCount() const = 0; virtual int peersCount() const = 0;
@ -263,11 +269,6 @@ namespace BitTorrent
virtual int totalSeedsCount() const = 0; virtual int totalSeedsCount() const = 0;
virtual int totalPeersCount() const = 0; virtual int totalPeersCount() const = 0;
virtual int totalLeechersCount() const = 0; virtual int totalLeechersCount() const = 0;
virtual QDateTime lastSeenComplete() const = 0;
virtual QDateTime completedTime() const = 0;
virtual qlonglong timeSinceUpload() const = 0;
virtual qlonglong timeSinceDownload() const = 0;
virtual qlonglong timeSinceActivity() const = 0;
virtual int downloadLimit() const = 0; virtual int downloadLimit() const = 0;
virtual int uploadLimit() const = 0; virtual int uploadLimit() const = 0;
virtual bool superSeeding() const = 0; virtual bool superSeeding() const = 0;

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -35,9 +35,7 @@
#include <libtorrent/write_resume_data.hpp> #include <libtorrent/write_resume_data.hpp>
#include <QByteArray> #include <QByteArray>
#include <QDateTime>
#include <QRegularExpression> #include <QRegularExpression>
#include <QString>
#include <QUrl> #include <QUrl>
#include "base/global.h" #include "base/global.h"
@ -147,7 +145,13 @@ BitTorrent::TorrentDescriptor::TorrentDescriptor(lt::add_torrent_params ltAddTor
: m_ltAddTorrentParams {std::move(ltAddTorrentParams)} : m_ltAddTorrentParams {std::move(ltAddTorrentParams)}
{ {
if (m_ltAddTorrentParams.ti && m_ltAddTorrentParams.ti->is_valid()) if (m_ltAddTorrentParams.ti && m_ltAddTorrentParams.ti->is_valid())
{
m_info.emplace(*m_ltAddTorrentParams.ti); m_info.emplace(*m_ltAddTorrentParams.ti);
if (m_ltAddTorrentParams.ti->creation_date() > 0)
m_creationDate = QDateTime::fromSecsSinceEpoch(m_ltAddTorrentParams.ti->creation_date());
m_creator = QString::fromStdString(m_ltAddTorrentParams.ti->creator());
m_comment = QString::fromStdString(m_ltAddTorrentParams.ti->comment());
}
} }
BitTorrent::InfoHash BitTorrent::TorrentDescriptor::infoHash() const BitTorrent::InfoHash BitTorrent::TorrentDescriptor::infoHash() const
@ -166,18 +170,17 @@ QString BitTorrent::TorrentDescriptor::name() const
QDateTime BitTorrent::TorrentDescriptor::creationDate() const QDateTime BitTorrent::TorrentDescriptor::creationDate() const
{ {
return ((m_ltAddTorrentParams.ti->creation_date() != 0) return m_creationDate;
? QDateTime::fromSecsSinceEpoch(m_ltAddTorrentParams.ti->creation_date()) : QDateTime());
} }
QString BitTorrent::TorrentDescriptor::creator() const QString BitTorrent::TorrentDescriptor::creator() const
{ {
return QString::fromStdString(m_ltAddTorrentParams.ti->creator()); return m_creator;
} }
QString BitTorrent::TorrentDescriptor::comment() const QString BitTorrent::TorrentDescriptor::comment() const
{ {
return QString::fromStdString(m_ltAddTorrentParams.ti->comment()); return m_comment;
} }
const std::optional<BitTorrent::TorrentInfo> &BitTorrent::TorrentDescriptor::info() const const std::optional<BitTorrent::TorrentInfo> &BitTorrent::TorrentDescriptor::info() const

View file

@ -33,15 +33,15 @@
#include <libtorrent/add_torrent_params.hpp> #include <libtorrent/add_torrent_params.hpp>
#include <QtContainerFwd> #include <QtContainerFwd>
#include <QDateTime>
#include <QMetaType> #include <QMetaType>
#include <QString>
#include "base/3rdparty/expected.hpp" #include "base/3rdparty/expected.hpp"
#include "base/path.h" #include "base/path.h"
#include "torrentinfo.h" #include "torrentinfo.h"
class QByteArray; class QByteArray;
class QDateTime;
class QString;
class QUrl; class QUrl;
namespace BitTorrent namespace BitTorrent
@ -77,6 +77,9 @@ namespace BitTorrent
lt::add_torrent_params m_ltAddTorrentParams; lt::add_torrent_params m_ltAddTorrentParams;
std::optional<TorrentInfo> m_info; std::optional<TorrentInfo> m_info;
QDateTime m_creationDate;
QString m_creator;
QString m_comment;
}; };
} }

View file

@ -322,6 +322,11 @@ TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession
{ {
if (m_ltAddTorrentParams.ti) if (m_ltAddTorrentParams.ti)
{ {
if (const std::time_t creationDate = m_ltAddTorrentParams.ti->creation_date(); creationDate > 0)
m_creationDate = QDateTime::fromSecsSinceEpoch(creationDate);
m_creator = QString::fromStdString(m_ltAddTorrentParams.ti->creator());
m_comment = QString::fromStdString(m_ltAddTorrentParams.ti->comment());
// Initialize it only if torrent is added with metadata. // Initialize it only if torrent is added with metadata.
// Otherwise it should be initialized in "Metadata received" handler. // Otherwise it should be initialized in "Metadata received" handler.
m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti); m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti);
@ -365,6 +370,12 @@ TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession
m_urlSeeds.append(QString::fromStdString(urlSeed)); m_urlSeeds.append(QString::fromStdString(urlSeed));
m_nativeStatus = extensionData->status; m_nativeStatus = extensionData->status;
m_addedTime = QDateTime::fromSecsSinceEpoch(m_nativeStatus.added_time);
if (m_nativeStatus.completed_time > 0)
m_completedTime = QDateTime::fromSecsSinceEpoch(m_nativeStatus.completed_time);
if (m_nativeStatus.last_seen_complete > 0)
m_lastSeenComplete = QDateTime::fromSecsSinceEpoch(m_nativeStatus.last_seen_complete);
if (hasMetadata()) if (hasMetadata())
updateProgress(); updateProgress();
@ -408,27 +419,17 @@ QString TorrentImpl::name() const
QDateTime TorrentImpl::creationDate() const QDateTime TorrentImpl::creationDate() const
{ {
if (!hasMetadata()) return m_creationDate;
return {};
const std::time_t date = nativeTorrentInfo()->creation_date();
return ((date != 0) ? QDateTime::fromSecsSinceEpoch(date) : QDateTime());
} }
QString TorrentImpl::creator() const QString TorrentImpl::creator() const
{ {
if (!hasMetadata()) return m_creator;
return {};
return QString::fromStdString(nativeTorrentInfo()->creator());
} }
QString TorrentImpl::comment() const QString TorrentImpl::comment() const
{ {
if (!hasMetadata()) return m_comment;
return {};
return QString::fromStdString(nativeTorrentInfo()->comment());
} }
bool TorrentImpl::isPrivate() const bool TorrentImpl::isPrivate() const
@ -957,7 +958,52 @@ void TorrentImpl::removeAllTags()
QDateTime TorrentImpl::addedTime() const QDateTime TorrentImpl::addedTime() const
{ {
return QDateTime::fromSecsSinceEpoch(m_nativeStatus.added_time); return m_addedTime;
}
QDateTime TorrentImpl::completedTime() const
{
return m_completedTime;
}
QDateTime TorrentImpl::lastSeenComplete() const
{
return m_lastSeenComplete;
}
qlonglong TorrentImpl::activeTime() const
{
return lt::total_seconds(m_nativeStatus.active_duration);
}
qlonglong TorrentImpl::finishedTime() const
{
return lt::total_seconds(m_nativeStatus.finished_duration);
}
qlonglong TorrentImpl::timeSinceUpload() const
{
if (m_nativeStatus.last_upload.time_since_epoch().count() == 0)
return -1;
return lt::total_seconds(lt::clock_type::now() - m_nativeStatus.last_upload);
}
qlonglong TorrentImpl::timeSinceDownload() const
{
if (m_nativeStatus.last_download.time_since_epoch().count() == 0)
return -1;
return lt::total_seconds(lt::clock_type::now() - m_nativeStatus.last_download);
}
qlonglong TorrentImpl::timeSinceActivity() const
{
const qlonglong upTime = timeSinceUpload();
const qlonglong downTime = timeSinceDownload();
return ((upTime < 0) != (downTime < 0))
? std::max(upTime, downTime)
: std::min(upTime, downTime);
} }
qreal TorrentImpl::ratioLimit() const qreal TorrentImpl::ratioLimit() const
@ -1276,16 +1322,6 @@ qlonglong TorrentImpl::totalUpload() const
return m_nativeStatus.all_time_upload; return m_nativeStatus.all_time_upload;
} }
qlonglong TorrentImpl::activeTime() const
{
return lt::total_seconds(m_nativeStatus.active_duration);
}
qlonglong TorrentImpl::finishedTime() const
{
return lt::total_seconds(m_nativeStatus.finished_duration);
}
qlonglong TorrentImpl::eta() const qlonglong TorrentImpl::eta() const
{ {
if (isStopped()) return MAX_ETA; if (isStopped()) return MAX_ETA;
@ -1395,45 +1431,6 @@ int TorrentImpl::totalLeechersCount() const
return (m_nativeStatus.num_incomplete > -1) ? m_nativeStatus.num_incomplete : (m_nativeStatus.list_peers - m_nativeStatus.list_seeds); return (m_nativeStatus.num_incomplete > -1) ? m_nativeStatus.num_incomplete : (m_nativeStatus.list_peers - m_nativeStatus.list_seeds);
} }
QDateTime TorrentImpl::lastSeenComplete() const
{
if (m_nativeStatus.last_seen_complete > 0)
return QDateTime::fromSecsSinceEpoch(m_nativeStatus.last_seen_complete);
else
return {};
}
QDateTime TorrentImpl::completedTime() const
{
if (m_nativeStatus.completed_time > 0)
return QDateTime::fromSecsSinceEpoch(m_nativeStatus.completed_time);
else
return {};
}
qlonglong TorrentImpl::timeSinceUpload() const
{
if (m_nativeStatus.last_upload.time_since_epoch().count() == 0)
return -1;
return lt::total_seconds(lt::clock_type::now() - m_nativeStatus.last_upload);
}
qlonglong TorrentImpl::timeSinceDownload() const
{
if (m_nativeStatus.last_download.time_since_epoch().count() == 0)
return -1;
return lt::total_seconds(lt::clock_type::now() - m_nativeStatus.last_download);
}
qlonglong TorrentImpl::timeSinceActivity() const
{
const qlonglong upTime = timeSinceUpload();
const qlonglong downTime = timeSinceDownload();
return ((upTime < 0) != (downTime < 0))
? std::max(upTime, downTime)
: std::min(upTime, downTime);
}
int TorrentImpl::downloadLimit() const int TorrentImpl::downloadLimit() const
{ {
return m_downloadLimit;; return m_downloadLimit;;
@ -2646,6 +2643,12 @@ void TorrentImpl::updateStatus(const lt::torrent_status &nativeStatus)
if (m_nativeStatus.num_pieces != oldStatus.num_pieces) if (m_nativeStatus.num_pieces != oldStatus.num_pieces)
updateProgress(); updateProgress();
if (m_nativeStatus.completed_time != oldStatus.completed_time)
m_completedTime = (m_nativeStatus.completed_time > 0) ? QDateTime::fromSecsSinceEpoch(m_nativeStatus.completed_time) : QDateTime();
if (m_nativeStatus.last_seen_complete != oldStatus.last_seen_complete)
m_lastSeenComplete = QDateTime::fromSecsSinceEpoch(m_nativeStatus.last_seen_complete);
updateState(); updateState();
m_payloadRateMonitor.addSample({nativeStatus.download_payload_rate m_payloadRateMonitor.addSample({nativeStatus.download_payload_rate

View file

@ -138,7 +138,15 @@ namespace BitTorrent
int piecesCount() const override; int piecesCount() const override;
int piecesHave() const override; int piecesHave() const override;
qreal progress() const override; qreal progress() const override;
QDateTime addedTime() const override; QDateTime addedTime() const override;
QDateTime completedTime() const override;
QDateTime lastSeenComplete() const override;
qlonglong activeTime() const override;
qlonglong finishedTime() const override;
qlonglong timeSinceUpload() const override;
qlonglong timeSinceDownload() const override;
qlonglong timeSinceActivity() const override;
qreal ratioLimit() const override; qreal ratioLimit() const override;
void setRatioLimit(qreal limit) override; void setRatioLimit(qreal limit) override;
@ -181,8 +189,6 @@ namespace BitTorrent
QString error() const override; QString error() const override;
qlonglong totalDownload() const override; qlonglong totalDownload() const override;
qlonglong totalUpload() const override; qlonglong totalUpload() const override;
qlonglong activeTime() const override;
qlonglong finishedTime() const override;
qlonglong eta() const override; qlonglong eta() const override;
QList<qreal> filesProgress() const override; QList<qreal> filesProgress() const override;
int seedsCount() const override; int seedsCount() const override;
@ -191,11 +197,6 @@ namespace BitTorrent
int totalSeedsCount() const override; int totalSeedsCount() const override;
int totalPeersCount() const override; int totalPeersCount() const override;
int totalLeechersCount() const override; int totalLeechersCount() const override;
QDateTime lastSeenComplete() const override;
QDateTime completedTime() const override;
qlonglong timeSinceUpload() const override;
qlonglong timeSinceDownload() const override;
qlonglong timeSinceActivity() const override;
int downloadLimit() const override; int downloadLimit() const override;
int uploadLimit() const override; int uploadLimit() const override;
bool superSeeding() const override; bool superSeeding() const override;
@ -342,6 +343,14 @@ namespace BitTorrent
InfoHash m_infoHash; InfoHash m_infoHash;
QDateTime m_creationDate;
QString m_creator;
QString m_comment;
QDateTime m_addedTime;
QDateTime m_completedTime;
QDateTime m_lastSeenComplete;
// m_moveFinishedTriggers is activated only when the following conditions are met: // m_moveFinishedTriggers is activated only when the following conditions are met:
// all file rename jobs complete, all file move jobs complete // all file rename jobs complete, all file move jobs complete
QQueue<EventTrigger> m_moveFinishedTriggers; QQueue<EventTrigger> m_moveFinishedTriggers;

View file

@ -700,6 +700,7 @@ void RSS::Private::Parser::parseRSSChannel(QXmlStreamReader &xml)
void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml) void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml)
{ {
const auto currentDateTime = QDateTime::currentDateTime();
QVariantHash article; QVariantHash article;
bool doubleContent = false; bool doubleContent = false;
@ -756,7 +757,7 @@ void RSS::Private::Parser::parseAtomArticle(QXmlStreamReader &xml)
{ {
// ATOM uses standard compliant date, don't do fancy stuff // ATOM uses standard compliant date, don't do fancy stuff
const QDateTime articleDate = QDateTime::fromString(xml.readElementText().trimmed(), Qt::ISODate); const QDateTime articleDate = QDateTime::fromString(xml.readElementText().trimmed(), Qt::ISODate);
article[Article::KeyDate] = (articleDate.isValid() ? articleDate : QDateTime::currentDateTime()); article[Article::KeyDate] = (articleDate.isValid() ? articleDate : currentDateTime);
} }
else if (name == u"author") else if (name == u"author")
{ {

View file

@ -106,10 +106,12 @@ void HtmlBrowser::resourceLoaded(QNetworkReply *reply)
atts[QNetworkRequest::HttpStatusCodeAttribute] = 200; atts[QNetworkRequest::HttpStatusCodeAttribute] = 200;
atts[QNetworkRequest::HttpReasonPhraseAttribute] = u"Ok"_s; atts[QNetworkRequest::HttpReasonPhraseAttribute] = u"Ok"_s;
metaData.setAttributes(atts); metaData.setAttributes(atts);
metaData.setLastModified(QDateTime::currentDateTime()); const auto currentDateTime = QDateTime::currentDateTime();
metaData.setExpirationDate(QDateTime::currentDateTime().addDays(1)); metaData.setLastModified(currentDateTime);
metaData.setExpirationDate(currentDateTime.addDays(1));
QIODevice *dev = m_diskCache->prepare(metaData); QIODevice *dev = m_diskCache->prepare(metaData);
if (!dev) return; if (!dev)
return;
QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(32, 32).save(dev, "PNG"); QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(32, 32).save(dev, "PNG");
m_diskCache->insert(dev); m_diskCache->insert(dev);

View file

@ -104,7 +104,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
const qlonglong timeSinceActivity = torrent.timeSinceActivity(); const qlonglong timeSinceActivity = torrent.timeSinceActivity();
return (timeSinceActivity < 0) return (timeSinceActivity < 0)
? Utils::DateTime::toSecsSinceEpoch(torrent.addedTime()) ? Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())
: (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity); : (QDateTime::currentSecsSinceEpoch() - timeSinceActivity);
}; };
return { return {