Skip to content

Commit 202378f

Browse files
committed
gossipsub: in-place negative-score peer removal
This change improves peer removal during `heartbeat` by switching from a two-pass remove logic to an in-place `retain` with very small size intermediate variable `removed_peers_count`. Signed-off-by: Onur Özkan <work@onurozkan.dev>
1 parent ac1404e commit 202378f

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,11 +2135,11 @@ where
21352135
let mesh_n_high = self.config.mesh_n_high_for_topic(topic_hash);
21362136
let mesh_outbound_min = self.config.mesh_outbound_min_for_topic(topic_hash);
21372137

2138-
// drop all peers with negative score, without PX
2139-
// if there is at some point a stable retain method for BTreeSet the following can be
2140-
// written more efficiently with retain.
2141-
let mut to_remove_peers = Vec::new();
2142-
for peer_id in peers.iter() {
2138+
#[cfg(feature = "metrics")]
2139+
let mut removed_peers_count = 0;
2140+
2141+
// Drop all peers with negative score, without PX
2142+
peers.retain(|peer_id| {
21432143
let peer_score = scores.get(peer_id).map(|r| r.score).unwrap_or_default();
21442144

21452145
// Record the score per mesh
@@ -2159,17 +2159,20 @@ where
21592159
let current_topic = to_prune.entry(*peer_id).or_insert_with(Vec::new);
21602160
current_topic.push(topic_hash.clone());
21612161
no_px.insert(*peer_id);
2162-
to_remove_peers.push(*peer_id);
2162+
2163+
#[cfg(feature = "metrics")]
2164+
{
2165+
removed_peers_count += 1;
2166+
}
2167+
2168+
return false;
21632169
}
2164-
}
2170+
true
2171+
});
21652172

21662173
#[cfg(feature = "metrics")]
21672174
if let Some(m) = self.metrics.as_mut() {
2168-
m.peers_removed(topic_hash, Churn::BadScore, to_remove_peers.len())
2169-
}
2170-
2171-
for peer_id in to_remove_peers {
2172-
peers.remove(&peer_id);
2175+
m.peers_removed(topic_hash, Churn::BadScore, removed_peers_count)
21732176
}
21742177

21752178
// too little peers - add some

0 commit comments

Comments
 (0)