mirror of
https://github.com/transmission/transmission
synced 2024-12-24 08:43:27 +00:00
perf: reduce copying in enforceSwarmPeerLimit()
(#5731)
* reduce copying in `enforceSwarmPeerLimit()` * clearer comparator naming
This commit is contained in:
parent
2a9c30a616
commit
6482d78852
1 changed files with 17 additions and 6 deletions
|
@ -2054,7 +2054,12 @@ constexpr struct
|
||||||
{
|
{
|
||||||
return compare(a, b) < 0;
|
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)
|
[[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec)
|
||||||
{
|
{
|
||||||
|
@ -2085,15 +2090,21 @@ void closeBadPeers(tr_swarm* s, time_t const now_sec)
|
||||||
void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max)
|
void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max)
|
||||||
{
|
{
|
||||||
// do we have too many peers?
|
// do we have too many peers?
|
||||||
if (auto const n = swarm->peerCount(); n <= max)
|
auto const n = swarm->peerCount();
|
||||||
|
if (n <= max)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// close all but the `max` most active
|
// close all but the `max` most active
|
||||||
auto peers = swarm->peers;
|
auto peers = std::vector<tr_peerMsgs*>{ n - max };
|
||||||
std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity);
|
std::partial_sort_copy(
|
||||||
std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
|
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)
|
void enforceSessionPeerLimit(tr_session* session)
|
||||||
|
@ -2116,7 +2127,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), ComparePeerByMostActive);
|
||||||
std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
|
std::for_each(std::begin(peers) + max, std::end(peers), closePeer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue