Preserve allocated buffer capacity

PR #22138.
This commit is contained in:
Chocobo1 2025-01-12 21:01:39 +08:00 committed by GitHub
parent 45b7947cd0
commit 82d90e599c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 9 deletions

View file

@ -544,6 +544,8 @@ SessionImpl::SessionImpl(QObject *parent)
m_asyncWorker->setMaxThreadCount(1); m_asyncWorker->setMaxThreadCount(1);
m_asyncWorker->setObjectName("SessionImpl m_asyncWorker"); m_asyncWorker->setObjectName("SessionImpl m_asyncWorker");
m_alerts.reserve(1024);
if (port() < 0) if (port() < 0)
m_port = Utils::Random::rand(1024, 65535); m_port = Utils::Random::rand(1024, 65535);
if (sslPort() < 0) if (sslPort() < 0)
@ -3187,10 +3189,10 @@ void SessionImpl::saveResumeData()
break; break;
} }
const std::vector<lt::alert *> alerts = getPendingAlerts(waitTime); fetchPendingAlerts(waitTime);
bool hasWantedAlert = false; bool hasWantedAlert = false;
for (const lt::alert *alert : alerts) for (const lt::alert *alert : m_alerts)
{ {
if (const int alertType = alert->type(); if (const int alertType = alert->type();
(alertType == lt::save_resume_data_alert::alert_type) || (alertType == lt::save_resume_data_failed_alert::alert_type) (alertType == lt::save_resume_data_alert::alert_type) || (alertType == lt::save_resume_data_failed_alert::alert_type)
@ -5603,14 +5605,13 @@ void SessionImpl::handleIPFilterError()
emit IPFilterParsed(true, 0); emit IPFilterParsed(true, 0);
} }
std::vector<lt::alert *> SessionImpl::getPendingAlerts(const lt::time_duration time) const void SessionImpl::fetchPendingAlerts(const lt::time_duration time)
{ {
if (time > lt::time_duration::zero()) if (time > lt::time_duration::zero())
m_nativeSession->wait_for_alert(time); m_nativeSession->wait_for_alert(time);
std::vector<lt::alert *> alerts; m_alerts.clear();
m_nativeSession->pop_alerts(&alerts); m_nativeSession->pop_alerts(&m_alerts);
return alerts;
} }
TorrentContentLayout SessionImpl::torrentContentLayout() const TorrentContentLayout SessionImpl::torrentContentLayout() const
@ -5626,7 +5627,7 @@ void SessionImpl::setTorrentContentLayout(const TorrentContentLayout value)
// Read alerts sent by libtorrent session // Read alerts sent by libtorrent session
void SessionImpl::readAlerts() void SessionImpl::readAlerts()
{ {
const std::vector<lt::alert *> alerts = getPendingAlerts(); fetchPendingAlerts();
Q_ASSERT(m_loadedTorrents.isEmpty()); Q_ASSERT(m_loadedTorrents.isEmpty());
Q_ASSERT(m_receivedAddTorrentAlertsCount == 0); Q_ASSERT(m_receivedAddTorrentAlertsCount == 0);
@ -5634,7 +5635,7 @@ void SessionImpl::readAlerts()
if (!isRestored()) if (!isRestored())
m_loadedTorrents.reserve(MAX_PROCESSING_RESUMEDATA_COUNT); m_loadedTorrents.reserve(MAX_PROCESSING_RESUMEDATA_COUNT);
for (const lt::alert *a : alerts) for (const lt::alert *a : m_alerts)
handleAlert(a); handleAlert(a);
if (m_receivedAddTorrentAlertsCount > 0) if (m_receivedAddTorrentAlertsCount > 0)

View file

@ -604,7 +604,7 @@ namespace BitTorrent
void populateAdditionalTrackersFromURL(); void populateAdditionalTrackersFromURL();
std::vector<lt::alert *> getPendingAlerts(lt::time_duration time = lt::time_duration::zero()) const; void fetchPendingAlerts(lt::time_duration time = lt::time_duration::zero());
void moveTorrentStorage(const MoveStorageJob &job) const; void moveTorrentStorage(const MoveStorageJob &job) const;
void handleMoveTorrentStorageJobFinished(const Path &newPath); void handleMoveTorrentStorageJobFinished(const Path &newPath);
@ -811,6 +811,7 @@ namespace BitTorrent
QMap<QString, CategoryOptions> m_categories; QMap<QString, CategoryOptions> m_categories;
TagSet m_tags; TagSet m_tags;
std::vector<lt::alert *> m_alerts; // make it a class variable so it can preserve its allocated `capacity`
qsizetype m_receivedAddTorrentAlertsCount = 0; qsizetype m_receivedAddTorrentAlertsCount = 0;
QList<Torrent *> m_loadedTorrents; QList<Torrent *> m_loadedTorrents;