radicle/node: avoid unnecessary allocations in `Emitter::emit_all`

This commit is contained in:
Defelo 2026-01-24 17:40:13 +01:00 committed by Fintan Halpenny
parent 8b1d475170
commit 589925e3a3
2 changed files with 10 additions and 14 deletions

View File

@ -1198,16 +1198,14 @@ where
rid, rid,
updated: updated.clone(), updated: updated.clone(),
}); });
self.emitter.emit_all( self.emitter
canonical .emit_all(canonical.into_iter().map(|(refname, target)| {
.into_iter() Event::CanonicalRefUpdated {
.map(|(refname, target)| Event::CanonicalRefUpdated {
rid, rid,
refname, refname,
target, target,
}) }
.collect(), }));
);
// Announce our new inventory if this fetch was a full clone. // Announce our new inventory if this fetch was a full clone.
// Only update and announce inventory for public repositories. // Only update and announce inventory for public repositories.

View File

@ -228,15 +228,13 @@ impl<T: Clone> Emitter<T> {
/// Emit a batch of events to subscribers and drop those who can't receive /// Emit a batch of events to subscribers and drop those who can't receive
/// them. /// them.
/// N.b. subscribers are also dropped if their channel is full. /// N.b. subscribers are also dropped if their channel is full.
pub fn emit_all(&self, events: Vec<T>) { pub fn emit_all(&self, events: impl IntoIterator<Item = T>) {
// SAFETY: We deliberately propagate panics from other threads holding the lock. // SAFETY: We deliberately propagate panics from other threads holding the lock.
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
self.subscribers.lock().unwrap().retain(|s| { let mut subscribers = self.subscribers.lock().unwrap();
events for event in events {
.clone() subscribers.retain(|s| s.try_send(event.clone()).is_ok());
.into_iter() }
.all(|event| s.try_send(event).is_ok())
});
} }
/// Subscribe to events stream. /// Subscribe to events stream.