diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-06-19 17:07:01 +0200 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2021-07-08 12:28:40 +0200 |
commit | b1d905c225e87a4a289c0cd3593c6c21cea3fba7 (patch) | |
tree | 1e351bba3f84d5ca0f8f795304da3a44fe29abb7 /src | |
parent | c9e8d8f9b168dec2bc7b845da38449e96708cf8e (diff) |
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 <jon@atack.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 16 |
1 files 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<NodeEvictionCandidate>& 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<size_t>(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<NodeEvictionCandidate>& 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) { |