From 2941005e34489dcfa118ea115acceffc7077a1fc Mon Sep 17 00:00:00 2001 From: tearfur <46261767+tearfur@users.noreply.github.com> Date: Fri, 30 Jun 2023 02:13:25 +0800 Subject: [PATCH] perf: convert comparator functors to static constexpr (#5687) --- libtransmission/peer-mgr.cc | 14 +++++++------- libtransmission/rpcimpl.cc | 8 ++++---- libtransmission/torrent.cc | 12 ++++++------ libtransmission/torrents.cc | 12 ++++++------ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 82ed63fd9..68b8ab869 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -1241,7 +1241,7 @@ namespace get_peers_helpers { /* better goes first */ -struct CompareAtomsByUsefulness +constexpr struct { [[nodiscard]] constexpr static int compare(peer_atom const& a, peer_atom const& b) noexcept // <=> { @@ -1272,7 +1272,7 @@ struct CompareAtomsByUsefulness { return compare(*a, *b) < 0; } -}; +} CompareAtomsByUsefulness{}; [[nodiscard]] bool isAtomInteresting(tr_torrent const* tor, peer_atom const& atom) { @@ -1337,7 +1337,7 @@ std::vector tr_peerMgrGetPeers(tr_torrent const* tor, uint8_t address_ty } } - std::sort(std::begin(atoms), std::end(atoms), CompareAtomsByUsefulness{}); + std::sort(std::begin(atoms), std::end(atoms), CompareAtomsByUsefulness); // add the first N of them into our return list @@ -2034,7 +2034,7 @@ void closePeer(tr_peer* peer) peer->swarm->removePeer(peer); } -struct ComparePeerByActivity +constexpr struct { [[nodiscard]] constexpr static int compare(tr_peer const* a, tr_peer const* b) // <=> { @@ -2062,7 +2062,7 @@ struct ComparePeerByActivity { return compare(a, b) < 0; } -}; +} ComparePeerByActivity{}; [[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec) { @@ -2098,7 +2098,7 @@ void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max) // close all but the `max` most active auto peers = swarm->peers; - std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity{}); + std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity); std::for_each(std::begin(peers) + max, std::end(peers), closePeer); } @@ -2122,7 +2122,7 @@ void enforceSessionPeerLimit(tr_session* session) TR_ASSERT(tr_peerMsgs::size() == std::size(peers)); if (std::size(peers) > max) { - std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity{}); + std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity); std::for_each(std::begin(peers) + max, std::end(peers), closePeer); } } diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index 7ebdb470e..f694710aa 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -197,18 +197,18 @@ char const* queueMoveBottom(tr_session* session, tr_variant* args_in, tr_variant return nullptr; } -struct CompareTorrentByQueuePosition +constexpr struct { constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const { return a->queuePosition < b->queuePosition; } -}; +} CompareTorrentByQueuePosition{}; char const* torrentStart(tr_session* session, tr_variant* args_in, tr_variant* /*args_out*/, tr_rpc_idle_data* /*idle_data*/) { auto torrents = getTorrents(session, args_in); - std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { if (!tor->isRunning) @@ -224,7 +224,7 @@ char const* torrentStart(tr_session* session, tr_variant* args_in, tr_variant* / char const* torrentStartNow(tr_session* session, tr_variant* args_in, tr_variant* /*args_out*/, tr_rpc_idle_data* /*idle_data*/) { auto torrents = getTorrents(session, args_in); - std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { if (!tor->isRunning) diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index e13fc12d5..82fa99f9d 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -548,13 +548,13 @@ namespace { namespace queue_helpers { -struct CompareTorrentByQueuePosition +constexpr struct { constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const noexcept { return a->queuePosition < b->queuePosition; } -}; +} CompareTorrentByQueuePosition{}; #ifdef TR_ENABLE_ASSERTS bool queueIsSequenced(tr_session const* session) @@ -624,7 +624,7 @@ void tr_torrentsQueueMoveTop(tr_torrent* const* torrents_in, size_t torrent_coun using namespace queue_helpers; auto torrents = std::vector(torrents_in, torrents_in + torrent_count); - std::sort(std::rbegin(torrents), std::rend(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::rbegin(torrents), std::rend(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { tr_torrentSetQueuePosition(tor, 0); @@ -636,7 +636,7 @@ void tr_torrentsQueueMoveUp(tr_torrent* const* torrents_in, size_t torrent_count using namespace queue_helpers; auto torrents = std::vector(torrents_in, torrents_in + torrent_count); - std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { if (tor->queuePosition > 0) @@ -651,7 +651,7 @@ void tr_torrentsQueueMoveDown(tr_torrent* const* torrents_in, size_t torrent_cou using namespace queue_helpers; auto torrents = std::vector(torrents_in, torrents_in + torrent_count); - std::sort(std::rbegin(torrents), std::rend(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::rbegin(torrents), std::rend(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { if (tor->queuePosition < UINT_MAX) @@ -666,7 +666,7 @@ void tr_torrentsQueueMoveBottom(tr_torrent* const* torrents_in, size_t torrent_c using namespace queue_helpers; auto torrents = std::vector(torrents_in, torrents_in + torrent_count); - std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition{}); + std::sort(std::begin(torrents), std::end(torrents), CompareTorrentByQueuePosition); for (auto* tor : torrents) { tr_torrentSetQueuePosition(tor, UINT_MAX); diff --git a/libtransmission/torrents.cc b/libtransmission/torrents.cc index 886d1f29a..5ff85e722 100644 --- a/libtransmission/torrents.cc +++ b/libtransmission/torrents.cc @@ -17,7 +17,7 @@ namespace { -struct CompareTorrentByHash +constexpr struct { bool operator()(tr_sha1_digest_t const& a, tr_sha1_digest_t const& b) const { @@ -38,7 +38,7 @@ struct CompareTorrentByHash { return (*this)(a, b->infoHash()); } -}; +} CompareTorrentByHash{}; } // namespace @@ -50,13 +50,13 @@ tr_torrent* tr_torrents::get(std::string_view magnet_link) tr_torrent* tr_torrents::get(tr_sha1_digest_t const& hash) { - auto [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), hash, CompareTorrentByHash{}); + auto [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), hash, CompareTorrentByHash); return begin == end ? nullptr : *begin; } tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const { - auto [begin, end] = std::equal_range(std::cbegin(by_hash_), std::cend(by_hash_), hash, CompareTorrentByHash{}); + auto [begin, end] = std::equal_range(std::cbegin(by_hash_), std::cend(by_hash_), hash, CompareTorrentByHash); return begin == end ? nullptr : *begin; } @@ -64,7 +64,7 @@ tr_torrent_id_t tr_torrents::add(tr_torrent* tor) { auto const id = static_cast(std::size(by_id_)); by_id_.push_back(tor); - by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}), tor); + by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash), tor); return id; } @@ -74,7 +74,7 @@ void tr_torrents::remove(tr_torrent const* tor, time_t current_time) TR_ASSERT(get(tor->id()) == tor); by_id_[tor->id()] = nullptr; - auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}); + auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash); by_hash_.erase(begin, end); removed_.emplace_back(tor->id(), current_time); }