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

This commit is contained in:
tearfur 2023-06-30 02:13:25 +08:00 committed by Charles Kerr
parent 3b59729e07
commit 2941005e34
4 changed files with 23 additions and 23 deletions

View File

@ -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_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
@ -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);
}
}

View File

@ -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)

View File

@ -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<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)
{
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<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)
{
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<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)
{
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<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)
{
tr_torrentSetQueuePosition(tor, UINT_MAX);

View File

@ -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<tr_torrent_id_t>(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);
}