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
9 changed files with 115 additions and 93 deletions

View File

@@ -322,6 +322,11 @@ TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession
{
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.
// Otherwise it should be initialized in "Metadata received" handler.
m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti);
@@ -365,6 +370,12 @@ TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession
m_urlSeeds.append(QString::fromStdString(urlSeed));
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())
updateProgress();
@@ -408,27 +419,17 @@ QString TorrentImpl::name() const
QDateTime TorrentImpl::creationDate() const
{
if (!hasMetadata())
return {};
const std::time_t date = nativeTorrentInfo()->creation_date();
return ((date != 0) ? QDateTime::fromSecsSinceEpoch(date) : QDateTime());
return m_creationDate;
}
QString TorrentImpl::creator() const
{
if (!hasMetadata())
return {};
return QString::fromStdString(nativeTorrentInfo()->creator());
return m_creator;
}
QString TorrentImpl::comment() const
{
if (!hasMetadata())
return {};
return QString::fromStdString(nativeTorrentInfo()->comment());
return m_comment;
}
bool TorrentImpl::isPrivate() const
@@ -957,7 +958,52 @@ void TorrentImpl::removeAllTags()
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
@@ -1276,16 +1322,6 @@ qlonglong TorrentImpl::totalUpload() const
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
{
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);
}
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
{
return m_downloadLimit;;
@@ -2646,6 +2643,12 @@ void TorrentImpl::updateStatus(const lt::torrent_status &nativeStatus)
if (m_nativeStatus.num_pieces != oldStatus.num_pieces)
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();
m_payloadRateMonitor.addSample({nativeStatus.download_payload_rate