From 8b1d475170accf4288cfe39286b9c959618cf736 Mon Sep 17 00:00:00 2001 From: Defelo Date: Sat, 24 Jan 2026 17:24:40 +0100 Subject: [PATCH] protocol: batch inventory removals and events in `sync_routing` The previous implementation would remove routing entries one at a time. Improve the performance of this by batching the removal and events emission. --- crates/radicle-protocol/src/service.rs | 31 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/radicle-protocol/src/service.rs b/crates/radicle-protocol/src/service.rs index 42fc27d2..9baf4d9c 100644 --- a/crates/radicle-protocol/src/service.rs +++ b/crates/radicle-protocol/src/service.rs @@ -2097,6 +2097,7 @@ where ) -> Result { let mut synced = SyncedRouting::default(); let included = inventory.into_iter().collect::>(); + let mut events = Vec::new(); for (rid, result) in self.db @@ -2106,7 +2107,7 @@ where match result { InsertResult::SeedAdded => { debug!(target: "service", "Routing table updated for {rid} with seed {from}"); - self.emitter.emit(Event::SeedDiscovered { rid, nid: from }); + events.push(Event::SeedDiscovered { rid, nid: from }); if self .policies @@ -2124,14 +2125,26 @@ where InsertResult::NotUpdated => {} } } - for rid in self.db.routing().get_inventory(&from)?.into_iter() { - if !included.contains(&rid) { - if self.db.routing_mut().remove_inventory(&rid, &from)? { - synced.removed.push(rid); - self.emitter.emit(Event::SeedDropped { rid, nid: from }); - } - } - } + + synced.removed.extend( + self.db + .routing() + .get_inventory(&from)? + .into_iter() + .filter(|rid| !included.contains(rid)), + ); + self.db + .routing_mut() + .remove_inventories(&synced.removed, &from)?; + events.extend( + synced + .removed + .iter() + .map(|&rid| Event::SeedDropped { rid, nid: from }), + ); + + self.emitter.emit_all(events); + Ok(synced) }