nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

commit 4376f59efcb5b4ab7e2be9bbed13386256683f7b
parent 9db5a26b9c2c97bf0da10fb9bf0e0f6deed0acec
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  8 Nov 2022 18:00:09 -0800

perf: dont create intermediate vecs when matching subs

Avoid creatingd intermediate vectors when matching subscriptions. We can
just iterate over the hashmap directly.

Diffstat:
Msrc/conn.rs | 17++++-------------
Msrc/server.rs | 7+++++--
2 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/conn.rs b/src/conn.rs @@ -2,7 +2,6 @@ use crate::close::Close; use crate::error::Error; use crate::error::Result; -use crate::event::Event; use crate::subscription::Subscription; use std::collections::HashMap; @@ -43,6 +42,10 @@ impl ClientConn { } } + pub fn subscriptions(&self) -> &HashMap<String, Subscription> { + &self.subscriptions + } + /// Get a short prefix of the client's unique identifier, suitable /// for logging. #[must_use] @@ -55,18 +58,6 @@ impl ClientConn { &self.client_ip } - /// Find all matching subscriptions. - #[must_use] - pub fn get_matching_subscriptions(&self, e: &Event) -> Vec<&str> { - let mut v: Vec<&str> = vec![]; - for (id, sub) in &self.subscriptions { - if sub.interested_in_event(e) { - v.push(id); - } - } - v - } - /// Add a new subscription for this connection. /// # Errors /// diff --git a/src/server.rs b/src/server.rs @@ -499,8 +499,11 @@ async fn nostr_server( Ok(global_event) = bcast_rx.recv() => { // an event has been broadcast to all clients // first check if there is a subscription for this event. - let matching_subs = conn.get_matching_subscriptions(&global_event); - for s in matching_subs { + for (s, sub) in conn.subscriptions() { + if !sub.interested_in_event(&global_event) { + continue; + } + // TODO: serialize at broadcast time, instead of // once for each consumer. if let Ok(event_str) = serde_json::to_string(&global_event) {