1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-03 05:25:52 +00:00

perf: improve sorting performance in tr_peerMgrGetPeers() (#5792)

This commit is contained in:
tearfur 2023-07-15 21:18:37 +08:00 committed by GitHub
parent 4ea49912e7
commit 01f25b030c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1180,17 +1180,17 @@ std::vector<tr_pex> tr_peerMgrGetPeers(tr_torrent const* tor, uint8_t address_ty
}
}
std::sort(std::begin(infos), std::end(infos), CompareAtomsByUsefulness);
// add the first N of them into our return list
// add the N most useful peers into our return list
auto const n = std::min(std::size(infos), max_peer_count);
auto pex = std::vector<tr_pex>{};
pex.reserve(n);
for (size_t i = 0; i < std::size(infos) && std::size(pex) < n; ++i)
std::partial_sort(std::begin(infos), std::begin(infos) + n, std::end(infos), CompareAtomsByUsefulness);
infos.resize(n);
for (auto const* const info : infos)
{
auto const* const info = infos[i];
auto const& [addr, port] = info->socket_address();
if (addr.type == address_type)