From d5d950e1cfff51be2834d904f6cafa0b778fe638 Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Mon, 28 Oct 2024 08:54:09 +0800 Subject: [PATCH] fix: always sort peer candidates by score (#7199) --- libtransmission/peer-mgr.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 9b53ac40c..159e9ffa3 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -2608,15 +2608,13 @@ void get_peer_candidates(size_t global_peer_limit, tr_torrents& torrents, tr_pee } // only keep the best `max` candidates - if (static auto constexpr Max = tr_peerMgr::OutboundCandidates::requested_inline_size; Max < std::size(candidates)) - { - std::partial_sort( - std::begin(candidates), - std::begin(candidates) + Max, - std::end(candidates), - [](auto const& a, auto const& b) { return a.score < b.score; }); - candidates.resize(Max); - } + auto const n_keep = std::min(tr_peerMgr::OutboundCandidates::requested_inline_size, std::size(candidates)); + std::partial_sort( + std::begin(candidates), + std::begin(candidates) + n_keep, + std::end(candidates), + [](auto const& a, auto const& b) { return a.score < b.score; }); + candidates.resize(n_keep); // put the best candidates at the end of the list for (auto it = std::crbegin(candidates), end = std::crend(candidates); it != end; ++it)