perf: reduce copying in `enforceSwarmPeerLimit()` (#5731)

* reduce copying in `enforceSwarmPeerLimit()`

* clearer comparator naming
This commit is contained in:
tearfur 2023-07-05 23:10:04 +08:00 committed by Charles Kerr
parent 14e31babbb
commit de26f38c77
1 changed files with 17 additions and 6 deletions

View File

@ -2062,7 +2062,12 @@ constexpr struct
{
return compare(a, b) < 0;
}
} ComparePeerByActivity{};
} ComparePeerByMostActive{};
constexpr auto ComparePeerByLeastActive = [](tr_peer const* a, tr_peer const* b)
{
return ComparePeerByMostActive(b, a);
};
[[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec)
{
@ -2091,15 +2096,21 @@ void closeBadPeers(tr_swarm* s, time_t const now_sec)
void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max)
{
// do we have too many peers?
if (auto const n = swarm->peerCount(); n <= max)
auto const n = swarm->peerCount();
if (n <= max)
{
return;
}
// 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::for_each(std::begin(peers) + max, std::end(peers), closePeer);
auto peers = std::vector<tr_peerMsgs*>{ n - max };
std::partial_sort_copy(
std::begin(swarm->peers),
std::end(swarm->peers),
std::begin(peers),
std::end(peers),
ComparePeerByLeastActive);
std::for_each(std::begin(peers), std::end(peers), closePeer);
}
void enforceSessionPeerLimit(tr_session* session)
@ -2122,7 +2133,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), ComparePeerByMostActive);
std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
}
}