perf: convert comparator functors to static constexpr (#5687)

This commit is contained in:
tearfur 2023-06-30 02:13:25 +08:00 committed by GitHub
parent 2f2ae61d0c
commit b79de0b431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 23 deletions

View File

@ -1235,7 +1235,7 @@ namespace get_peers_helpers
{ {
/* better goes first */ /* better goes first */
struct CompareAtomsByUsefulness constexpr struct
{ {
[[nodiscard]] constexpr static int compare(peer_atom const& a, peer_atom const& b) noexcept // <=> [[nodiscard]] constexpr static int compare(peer_atom const& a, peer_atom const& b) noexcept // <=>
{ {
@ -1266,7 +1266,7 @@ struct CompareAtomsByUsefulness
{ {
return compare(*a, *b) < 0; return compare(*a, *b) < 0;
} }
}; } CompareAtomsByUsefulness{};
[[nodiscard]] bool isAtomInteresting(tr_torrent const* tor, peer_atom const& atom) [[nodiscard]] bool isAtomInteresting(tr_torrent const* tor, peer_atom const& atom)
{ {
@ -1332,7 +1332,7 @@ std::vector<tr_pex> 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 // add the first N of them into our return list
@ -2015,7 +2015,7 @@ void closePeer(tr_peer* peer)
peer->swarm->removePeer(peer); peer->swarm->removePeer(peer);
} }
struct ComparePeerByActivity constexpr struct
{ {
[[nodiscard]] constexpr static int compare(tr_peer const* a, tr_peer const* b) // <=> [[nodiscard]] constexpr static int compare(tr_peer const* a, tr_peer const* b) // <=>
{ {
@ -2043,7 +2043,7 @@ struct ComparePeerByActivity
{ {
return compare(a, b) < 0; return compare(a, b) < 0;
} }
}; } ComparePeerByActivity{};
[[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec) [[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec)
{ {
@ -2081,7 +2081,7 @@ void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max)
// close all but the `max` most active // close all but the `max` most active
auto peers = swarm->peers; 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); std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
} }
@ -2105,7 +2105,7 @@ void enforceSessionPeerLimit(tr_session* session)
TR_ASSERT(tr_peerMsgs::size() == std::size(peers)); TR_ASSERT(tr_peerMsgs::size() == std::size(peers));
if (std::size(peers) > max) 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); std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
} }
} }

View File

@ -197,18 +197,18 @@ char const* queueMoveBottom(tr_session* session, tr_variant* args_in, tr_variant
return nullptr; return nullptr;
} }
struct CompareTorrentByQueuePosition constexpr struct
{ {
constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const
{ {
return a->queuePosition < b->queuePosition; 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*/) 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); 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) for (auto* tor : torrents)
{ {
if (!tor->is_running()) if (!tor->is_running())
@ -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*/) 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); 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) for (auto* tor : torrents)
{ {
if (!tor->is_running()) if (!tor->is_running())

View File

@ -546,13 +546,13 @@ namespace
{ {
namespace queue_helpers namespace queue_helpers
{ {
struct CompareTorrentByQueuePosition constexpr struct
{ {
constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const noexcept constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const noexcept
{ {
return a->queuePosition < b->queuePosition; return a->queuePosition < b->queuePosition;
} }
}; } CompareTorrentByQueuePosition{};
#ifdef TR_ENABLE_ASSERTS #ifdef TR_ENABLE_ASSERTS
bool queueIsSequenced(tr_session const* session) bool queueIsSequenced(tr_session const* session)
@ -622,7 +622,7 @@ void tr_torrentsQueueMoveTop(tr_torrent* const* torrents_in, size_t torrent_coun
using namespace queue_helpers; using namespace queue_helpers;
auto torrents = std::vector<tr_torrent*>(torrents_in, torrents_in + torrent_count); auto torrents = std::vector<tr_torrent*>(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) for (auto* tor : torrents)
{ {
tr_torrentSetQueuePosition(tor, 0); tr_torrentSetQueuePosition(tor, 0);
@ -634,7 +634,7 @@ void tr_torrentsQueueMoveUp(tr_torrent* const* torrents_in, size_t torrent_count
using namespace queue_helpers; using namespace queue_helpers;
auto torrents = std::vector<tr_torrent*>(torrents_in, torrents_in + torrent_count); auto torrents = std::vector<tr_torrent*>(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) for (auto* tor : torrents)
{ {
if (tor->queuePosition > 0) if (tor->queuePosition > 0)
@ -649,7 +649,7 @@ void tr_torrentsQueueMoveDown(tr_torrent* const* torrents_in, size_t torrent_cou
using namespace queue_helpers; using namespace queue_helpers;
auto torrents = std::vector<tr_torrent*>(torrents_in, torrents_in + torrent_count); auto torrents = std::vector<tr_torrent*>(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) for (auto* tor : torrents)
{ {
if (tor->queuePosition < UINT_MAX) if (tor->queuePosition < UINT_MAX)
@ -664,7 +664,7 @@ void tr_torrentsQueueMoveBottom(tr_torrent* const* torrents_in, size_t torrent_c
using namespace queue_helpers; using namespace queue_helpers;
auto torrents = std::vector<tr_torrent*>(torrents_in, torrents_in + torrent_count); auto torrents = std::vector<tr_torrent*>(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) for (auto* tor : torrents)
{ {
tr_torrentSetQueuePosition(tor, UINT_MAX); tr_torrentSetQueuePosition(tor, UINT_MAX);

View File

@ -17,7 +17,7 @@
namespace namespace
{ {
struct CompareTorrentByHash constexpr struct
{ {
bool operator()(tr_sha1_digest_t const& a, tr_sha1_digest_t const& b) const 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->info_hash()); return (*this)(a, b->info_hash());
} }
}; } CompareTorrentByHash{};
} // namespace } // 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) 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; return begin == end ? nullptr : *begin;
} }
tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const 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; return begin == end ? nullptr : *begin;
} }
@ -64,7 +64,7 @@ tr_torrent_id_t tr_torrents::add(tr_torrent* tor)
{ {
auto const id = static_cast<tr_torrent_id_t>(std::size(by_id_)); auto const id = static_cast<tr_torrent_id_t>(std::size(by_id_));
by_id_.push_back(tor); 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; return id;
} }
@ -74,7 +74,7 @@ void tr_torrents::remove(tr_torrent const* tor, time_t current_time)
TR_ASSERT(get(tor->id()) == tor); TR_ASSERT(get(tor->id()) == tor);
by_id_[tor->id()] = nullptr; 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); by_hash_.erase(begin, end);
removed_.emplace_back(tor->id(), current_time); removed_.emplace_back(tor->id(), current_time);
} }