From 02869d7428f093e8b0a63af3ff179cb1b8ff90e7 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 15 Mar 2022 12:33:12 +0800 Subject: [PATCH] Avoid unnecessary lookups Fix up 30319e51e55ed7740b7bdc4b75563f441f9ccb71. PR #16629. --- src/gui/transferlistfilterswidget.cpp | 96 ++++++++++----------------- src/gui/transferlistfilterswidget.h | 6 +- 2 files changed, 39 insertions(+), 63 deletions(-) diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index da0ab15ef..1ecfa6c3e 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -231,42 +231,36 @@ void StatusFilterWidget::populate() const QVector torrents = BitTorrent::Session::instance()->torrents(); for (const BitTorrent::Torrent *torrent : torrents) - { updateTorrentStatus(torrent); - } updateTexts(); } void StatusFilterWidget::updateTorrentStatus(const BitTorrent::Torrent *torrent) { - const auto update = [this, torrent](const TorrentFilter::Type status, const bool insert, int &count) + TorrentFilterBitset &torrentStatus = m_torrentsStatus[torrent]; + + const auto update = [this, &torrentStatus](const TorrentFilter::Type status, const bool needStatus, int &counter) { - const bool contains = m_torrentsStatus.contains(torrent, status); - if (insert && !contains) + const bool hasStatus = torrentStatus[status]; + if (needStatus && !hasStatus) { - ++count; - m_torrentsStatus.insert(torrent, status); + ++counter; + torrentStatus.set(status); } - else if (!insert && contains) + else if (!needStatus && hasStatus) { - --count; - m_torrentsStatus.remove(torrent, status); + --counter; + torrentStatus.reset(status); } }; update(TorrentFilter::Downloading, torrent->isDownloading(), m_nbDownloading); - update(TorrentFilter::Seeding, torrent->isUploading(), m_nbSeeding); - update(TorrentFilter::Completed, torrent->isCompleted(), m_nbCompleted); - update(TorrentFilter::Resumed, torrent->isResumed(), m_nbResumed); - update(TorrentFilter::Paused, torrent->isPaused(), m_nbPaused); - update(TorrentFilter::Active, torrent->isActive(), m_nbActive); - update(TorrentFilter::Inactive, torrent->isInactive(), m_nbInactive); const bool isStalledUploading = (torrent->state() == BitTorrent::TorrentState::StalledUploading); @@ -276,7 +270,6 @@ void StatusFilterWidget::updateTorrentStatus(const BitTorrent::Torrent *torrent) update(TorrentFilter::StalledDownloading, isStalledDownloading, m_nbStalledDownloading); update(TorrentFilter::Checking, torrent->isChecking(), m_nbChecking); - update(TorrentFilter::Errored, torrent->isErrored(), m_nbErrored); m_nbStalled = m_nbStalledUploading + m_nbStalledDownloading; @@ -336,54 +329,33 @@ void StatusFilterWidget::handleNewTorrent(BitTorrent::Torrent *const torrent) void StatusFilterWidget::torrentAboutToBeDeleted(BitTorrent::Torrent *const torrent) { - for (const TorrentFilter::Type status : m_torrentsStatus.values(torrent)) - { - switch (status) - { - case TorrentFilter::Downloading: - --m_nbDownloading; - break; - case TorrentFilter::Seeding: - --m_nbSeeding; - break; - case TorrentFilter::Completed: - --m_nbCompleted; - break; - case TorrentFilter::Resumed: - --m_nbResumed; - break; - case TorrentFilter::Paused: - --m_nbPaused; - break; - case TorrentFilter::Active: - --m_nbActive; - break; - case TorrentFilter::Inactive: - --m_nbInactive; - break; - case TorrentFilter::StalledUploading: - --m_nbStalledUploading; - break; - case TorrentFilter::StalledDownloading: - --m_nbStalledDownloading; - break; - case TorrentFilter::Checking: - --m_nbChecking; - break; - case TorrentFilter::Errored: - --m_nbErrored; - break; - - default: - Q_ASSERT(false); - break; - } - } + const TorrentFilterBitset status = m_torrentsStatus.take(torrent); + + if (status[TorrentFilter::Downloading]) + --m_nbDownloading; + if (status[TorrentFilter::Seeding]) + --m_nbSeeding; + if (status[TorrentFilter::Completed]) + --m_nbCompleted; + if (status[TorrentFilter::Resumed]) + --m_nbResumed; + if (status[TorrentFilter::Paused]) + --m_nbPaused; + if (status[TorrentFilter::Active]) + --m_nbActive; + if (status[TorrentFilter::Inactive]) + --m_nbInactive; + if (status[TorrentFilter::StalledUploading]) + --m_nbStalledUploading; + if (status[TorrentFilter::StalledDownloading]) + --m_nbStalledDownloading; + if (status[TorrentFilter::Checking]) + --m_nbChecking; + if (status[TorrentFilter::Errored]) + --m_nbErrored; m_nbStalled = m_nbStalledUploading + m_nbStalledDownloading; - m_torrentsStatus.remove(torrent); - updateTexts(); } diff --git a/src/gui/transferlistfilterswidget.h b/src/gui/transferlistfilterswidget.h index 6f53071ad..d2c3b1508 100644 --- a/src/gui/transferlistfilterswidget.h +++ b/src/gui/transferlistfilterswidget.h @@ -28,7 +28,10 @@ #pragma once +#include + #include +#include #include #include @@ -96,7 +99,8 @@ private: void updateTorrentStatus(const BitTorrent::Torrent *torrent); void updateTexts(); - QMultiHash m_torrentsStatus; + using TorrentFilterBitset = std::bitset<32>; // approximated size, this should be the number of TorrentFilter::Type elements + QHash m_torrentsStatus; int m_nbDownloading = 0; int m_nbSeeding = 0; int m_nbCompleted = 0;