Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle/node: avoid unnecessary allocations in `Emitter::emit_all`
◌ CI pending Defelo committed 3 months ago
commit 589925e3a3d792b321661a2e3f33b1f38be15063
parent 8b1d475170accf4288cfe39286b9c959618cf736
1 pending (1 total) View logs
2 files changed +10 -14
modified crates/radicle-protocol/src/service.rs
@@ -1198,16 +1198,14 @@ where
                    rid,
                    updated: updated.clone(),
                });
-
                self.emitter.emit_all(
-
                    canonical
-
                        .into_iter()
-
                        .map(|(refname, target)| Event::CanonicalRefUpdated {
+
                self.emitter
+
                    .emit_all(canonical.into_iter().map(|(refname, target)| {
+
                        Event::CanonicalRefUpdated {
                            rid,
                            refname,
                            target,
-
                        })
-
                        .collect(),
-
                );
+
                        }
+
                    }));

                // Announce our new inventory if this fetch was a full clone.
                // Only update and announce inventory for public repositories.
modified crates/radicle/src/node/events.rs
@@ -228,15 +228,13 @@ impl<T: Clone> Emitter<T> {
    /// Emit a batch of events to subscribers and drop those who can't receive
    /// them.
    /// 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.
        #[allow(clippy::unwrap_used)]
-
        self.subscribers.lock().unwrap().retain(|s| {
-
            events
-
                .clone()
-
                .into_iter()
-
                .all(|event| s.try_send(event).is_ok())
-
        });
+
        let mut subscribers = self.subscribers.lock().unwrap();
+
        for event in events {
+
            subscribers.retain(|s| s.try_send(event.clone()).is_ok());
+
        }
    }

    /// Subscribe to events stream.