From b1d905c225e87a4a289c0cd3593c6c21cea3fba7 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Sat, 19 Jun 2021 17:07:01 +0200 Subject: [PATCH] p2p: earlier continuation when no remaining eviction candidates in ProtectEvictionCandidatesByRatio(). With this change, `if (n.count == 0) continue;` will be true if a network had candidates protected in the first iterations and has no candidates remaining to be protected in later iterations. Co-authored-by: Jon Atack --- src/net.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 9567e2b0cf..f7d46881ee 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -935,16 +935,18 @@ void ProtectEvictionCandidatesByRatio(std::vector& evicti const size_t max_protect_by_network{total_protect_size / 2}; size_t num_protected{0}; - // Count the number of disadvantaged networks from which we have peers to protect. - auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; }); - - while (num_networks != 0 && num_protected < max_protect_by_network) { + while (num_protected < max_protect_by_network) { + // Count the number of disadvantaged networks from which we have peers to protect. + auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; }); + if (num_networks == 0) { + break; + } const size_t disadvantaged_to_protect{max_protect_by_network - num_protected}; const size_t protect_per_network{std::max(disadvantaged_to_protect / num_networks, static_cast(1))}; // Early exit flag if there are no remaining candidates by disadvantaged network. bool protected_at_least_one{false}; - for (const Net& n : networks) { + for (Net& n : networks) { if (n.count == 0) continue; const size_t before = eviction_candidates.size(); EraseLastKElements(eviction_candidates, CompareNodeNetworkTime(n.is_local, n.id), @@ -954,10 +956,12 @@ void ProtectEvictionCandidatesByRatio(std::vector& evicti const size_t after = eviction_candidates.size(); if (before > after) { protected_at_least_one = true; - num_protected += before - after; + const size_t delta{before - after}; + num_protected += delta; if (num_protected >= max_protect_by_network) { break; } + n.count -= delta; } } if (!protected_at_least_one) {