Skip to content

Commit b1d905c

Browse files
vasildjonatack
andcommitted
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 <[email protected]>
1 parent c9e8d8f commit b1d905c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/net.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -935,16 +935,18 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
935935
const size_t max_protect_by_network{total_protect_size / 2};
936936
size_t num_protected{0};
937937

938-
// Count the number of disadvantaged networks from which we have peers to protect.
939-
auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; });
940-
941-
while (num_networks != 0 && num_protected < max_protect_by_network) {
938+
while (num_protected < max_protect_by_network) {
939+
// Count the number of disadvantaged networks from which we have peers to protect.
940+
auto num_networks = std::count_if(networks.begin(), networks.end(), [](const Net& n) { return n.count; });
941+
if (num_networks == 0) {
942+
break;
943+
}
942944
const size_t disadvantaged_to_protect{max_protect_by_network - num_protected};
943945
const size_t protect_per_network{std::max(disadvantaged_to_protect / num_networks, static_cast<size_t>(1))};
944946
// Early exit flag if there are no remaining candidates by disadvantaged network.
945947
bool protected_at_least_one{false};
946948

947-
for (const Net& n : networks) {
949+
for (Net& n : networks) {
948950
if (n.count == 0) continue;
949951
const size_t before = eviction_candidates.size();
950952
EraseLastKElements(eviction_candidates, CompareNodeNetworkTime(n.is_local, n.id),
@@ -954,10 +956,12 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
954956
const size_t after = eviction_candidates.size();
955957
if (before > after) {
956958
protected_at_least_one = true;
957-
num_protected += before - after;
959+
const size_t delta{before - after};
960+
num_protected += delta;
958961
if (num_protected >= max_protect_by_network) {
959962
break;
960963
}
964+
n.count -= delta;
961965
}
962966
}
963967
if (!protected_at_least_one) {

0 commit comments

Comments
 (0)