fix: always sort peer candidates by score (#7199)

This commit is contained in:
Yat Ho 2024-10-28 08:54:09 +08:00 committed by GitHub
parent 13987e7996
commit d5d950e1cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 9 deletions

View File

@ -2608,15 +2608,13 @@ void get_peer_candidates(size_t global_peer_limit, tr_torrents& torrents, tr_pee
} }
// only keep the best `max` candidates // only keep the best `max` candidates
if (static auto constexpr Max = tr_peerMgr::OutboundCandidates::requested_inline_size; Max < std::size(candidates)) auto const n_keep = std::min(tr_peerMgr::OutboundCandidates::requested_inline_size, std::size(candidates));
{
std::partial_sort( std::partial_sort(
std::begin(candidates), std::begin(candidates),
std::begin(candidates) + Max, std::begin(candidates) + n_keep,
std::end(candidates), std::end(candidates),
[](auto const& a, auto const& b) { return a.score < b.score; }); [](auto const& a, auto const& b) { return a.score < b.score; });
candidates.resize(Max); candidates.resize(n_keep);
}
// put the best candidates at the end of the list // put the best candidates at the end of the list
for (auto it = std::crbegin(candidates), end = std::crend(candidates); it != end; ++it) for (auto it = std::crbegin(candidates), end = std::crend(candidates); it != end; ++it)