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>
This commit is contained in:
Vasil Dimov 2021-06-19 17:07:01 +02:00 committed by Jon Atack
parent c9e8d8f9b1
commit b1d905c225
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

View File

@ -935,16 +935,18 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
const size_t max_protect_by_network{total_protect_size / 2}; const size_t max_protect_by_network{total_protect_size / 2};
size_t num_protected{0}; size_t num_protected{0};
// Count the number of disadvantaged networks from which we have peers to protect. while (num_protected < max_protect_by_network) {
auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; }); // 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) { if (num_networks == 0) {
break;
}
const size_t disadvantaged_to_protect{max_protect_by_network - num_protected}; 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))}; 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. // Early exit flag if there are no remaining candidates by disadvantaged network.
bool protected_at_least_one{false}; bool protected_at_least_one{false};
for (const Net& n : networks) { for (Net& n : networks) {
if (n.count == 0) continue; if (n.count == 0) continue;
const size_t before = eviction_candidates.size(); const size_t before = eviction_candidates.size();
EraseLastKElements(eviction_candidates, CompareNodeNetworkTime(n.is_local, n.id), 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(); const size_t after = eviction_candidates.size();
if (before > after) { if (before > after) {
protected_at_least_one = true; 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) { if (num_protected >= max_protect_by_network) {
break; break;
} }
n.count -= delta;
} }
} }
if (!protected_at_least_one) { if (!protected_at_least_one) {