From 183bd4b36984780f61d566805122c9433cbbf4ab Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Thu, 23 Jan 2025 16:38:37 +0100 Subject: [PATCH] refactor: use HashSet instead of Vec for network links Links are used a lot for `contains` operation. --- examples/examples/z_pub_thr.rs | 1 + .../net/routing/hat/linkstate_peer/network.rs | 23 ++++++++------- zenoh/src/net/routing/hat/p2p_peer/gossip.rs | 11 +++---- zenoh/src/net/routing/hat/router/mod.rs | 10 ++++--- zenoh/src/net/routing/hat/router/network.rs | 29 +++++++++---------- zenoh/src/net/routing/hat/router/pubsub.rs | 2 +- zenoh/src/net/routing/hat/router/queries.rs | 4 +-- zenoh/src/net/routing/hat/router/token.rs | 7 +++-- 8 files changed, 47 insertions(+), 40 deletions(-) diff --git a/examples/examples/z_pub_thr.rs b/examples/examples/z_pub_thr.rs index dc18715e2a..3051bcbc1b 100644 --- a/examples/examples/z_pub_thr.rs +++ b/examples/examples/z_pub_thr.rs @@ -21,6 +21,7 @@ use zenoh::{ Wait, }; use zenoh_examples::CommonArgs; +use zenoh_ext::z_serialize; fn main() { // initiate logging diff --git a/zenoh/src/net/routing/hat/linkstate_peer/network.rs b/zenoh/src/net/routing/hat/linkstate_peer/network.rs index 9eca0d100e..a2cac6da17 100644 --- a/zenoh/src/net/routing/hat/linkstate_peer/network.rs +++ b/zenoh/src/net/routing/hat/linkstate_peer/network.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; // // Copyright (c) 2023 ZettaScale Technology // @@ -52,7 +53,7 @@ pub(super) struct Node { pub(super) whatami: Option, pub(super) locators: Option>, pub(super) sn: u64, - pub(super) links: Vec, + pub(super) links: HashSet, } impl std::fmt::Debug for Node { @@ -149,7 +150,7 @@ impl Network { whatami: Some(runtime.whatami()), locators: None, sn: 1, - links: vec![], + links: HashSet::new(), }); Network { name, @@ -424,7 +425,7 @@ impl Network { let link_states = link_states .into_iter() .map(|(zid, wai, locs, sn, links)| { - let links: Vec = links + let links: HashSet = links .iter() .filter_map(|l| { if let Some(zid) = src_link.get_zid(l) { @@ -574,7 +575,7 @@ impl Network { } }, ) - .collect::, NodeIndex, bool)>>(); + .collect::, NodeIndex, bool)>>(); // Add/remove edges from graph let mut reintroduced_nodes = vec![]; @@ -596,11 +597,11 @@ impl Network { whatami: None, locators: None, sn: 0, - links: vec![], + links: HashSet::new(), }; tracing::debug!("{} Add node (reintroduced) {}", self.name, link.clone()); let idx = self.add_node(node); - reintroduced_nodes.push((vec![], idx, true)); + reintroduced_nodes.push((HashSet::new(), idx, true)); } } let mut edges = vec![]; @@ -626,7 +627,7 @@ impl Network { let link_states = link_states .into_iter() .filter(|ls| !removed.iter().any(|(idx, _)| idx == &ls.1)) - .collect::, NodeIndex, bool)>>(); + .collect::, NodeIndex, bool)>>(); if !self.autoconnect.is_empty() { // Connect discovered peers @@ -665,8 +666,8 @@ impl Network { #[allow(clippy::type_complexity)] // This is only used here if !link_states.is_empty() { let (new_idxs, updated_idxs): ( - Vec<(Vec, NodeIndex, bool)>, - Vec<(Vec, NodeIndex, bool)>, + Vec<(HashSet, NodeIndex, bool)>, + Vec<(HashSet, NodeIndex, bool)>, ) = link_states.into_iter().partition(|(_, _, new)| *new); for link in self.links.values() { let new_idxs = new_idxs @@ -742,7 +743,7 @@ impl Network { whatami: Some(whatami), locators: None, sn: 0, - links: vec![], + links: HashSet::new(), }), true, ) @@ -752,7 +753,7 @@ impl Network { tracing::trace!("Update edge (link) {} {}", self.graph[self.idx].zid, zid); self.update_edge(self.idx, idx); } - self.graph[self.idx].links.push(zid); + self.graph[self.idx].links.insert(zid); self.graph[self.idx].sn += 1; // Send updated self linkstate on all existing links except new one diff --git a/zenoh/src/net/routing/hat/p2p_peer/gossip.rs b/zenoh/src/net/routing/hat/p2p_peer/gossip.rs index b0b382eefc..ef5aa4d0ee 100644 --- a/zenoh/src/net/routing/hat/p2p_peer/gossip.rs +++ b/zenoh/src/net/routing/hat/p2p_peer/gossip.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; // // Copyright (c) 2023 ZettaScale Technology // @@ -47,7 +48,7 @@ pub(super) struct Node { pub(super) whatami: Option, pub(super) locators: Option>, pub(super) sn: u64, - pub(super) links: Vec, + pub(super) links: HashSet, } impl std::fmt::Debug for Node { @@ -125,7 +126,7 @@ impl Network { whatami: Some(runtime.whatami()), locators: None, sn: 1, - links: vec![], + links: HashSet::new(), }); Network { name, @@ -355,7 +356,7 @@ impl Network { let link_states = link_states .into_iter() .map(|(zid, wai, locs, sn, links)| { - let links: Vec = links + let links: HashSet = links .iter() .filter_map(|l| { if let Some(zid) = src_link.get_zid(l) { @@ -497,13 +498,13 @@ impl Network { whatami: Some(whatami), locators: None, sn: 0, - links: vec![], + links: HashSet::new(), }), true, ) } }; - self.graph[self.idx].links.push(zid); + self.graph[self.idx].links.insert(zid); self.graph[self.idx].sn += 1; // Send updated self linkstate on all existing links except new one diff --git a/zenoh/src/net/routing/hat/router/mod.rs b/zenoh/src/net/routing/hat/router/mod.rs index 1ff8d858d2..7f875bd2e4 100644 --- a/zenoh/src/net/routing/hat/router/mod.rs +++ b/zenoh/src/net/routing/hat/router/mod.rs @@ -224,7 +224,8 @@ impl HatTables { .as_ref() .unwrap() .get_links(peer) - .iter() + .into_iter() + .flatten() .filter(move |nid| { if let Some(node) = self.routers_net.as_ref().unwrap().get_node(nid) { node.whatami.unwrap_or(WhatAmI::Router) == WhatAmI::Router @@ -269,7 +270,7 @@ impl HatTables { } #[inline] - fn failover_brokering_to(source_links: &[ZenohIdProto], dest: ZenohIdProto) -> bool { + fn failover_brokering_to(source_links: &HashSet, dest: ZenohIdProto) -> bool { // if source_links is empty then gossip is probably disabled in source peer !source_links.is_empty() && !source_links.contains(&dest) } @@ -281,8 +282,9 @@ impl HatTables { .linkstatepeers_net .as_ref() .map(|net| { - let links = net.get_links(peer1); - let res = HatTables::failover_brokering_to(links, peer2); + let res = net + .get_links(peer1) + .is_some_and(|links| HatTables::failover_brokering_to(links, peer2)); tracing::trace!("failover_brokering {} {} : {}", peer1, peer2, res); res }) diff --git a/zenoh/src/net/routing/hat/router/network.rs b/zenoh/src/net/routing/hat/router/network.rs index 103332955b..2ad8635be1 100644 --- a/zenoh/src/net/routing/hat/router/network.rs +++ b/zenoh/src/net/routing/hat/router/network.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; // // Copyright (c) 2023 ZettaScale Technology // @@ -52,7 +53,7 @@ pub(super) struct Node { pub(super) whatami: Option, pub(super) locators: Option>, pub(super) sn: u64, - pub(super) links: Vec, + pub(super) links: HashSet, } impl std::fmt::Debug for Node { @@ -149,7 +150,7 @@ impl Network { whatami: Some(runtime.whatami()), locators: None, sn: 1, - links: vec![], + links: HashSet::new(), }); Network { name, @@ -428,7 +429,7 @@ impl Network { let link_states = link_states .into_iter() .map(|(zid, wai, locs, sn, links)| { - let links: Vec = links + let links: HashSet = links .iter() .filter_map(|l| { if let Some(zid) = src_link.get_zid(l) { @@ -578,7 +579,7 @@ impl Network { } }, ) - .collect::, NodeIndex, bool)>>(); + .collect::, NodeIndex, bool)>>(); // Add/remove edges from graph let mut reintroduced_nodes = vec![]; @@ -600,11 +601,11 @@ impl Network { whatami: None, locators: None, sn: 0, - links: vec![], + links: HashSet::new(), }; tracing::debug!("{} Add node (reintroduced) {}", self.name, link.clone()); let idx = self.add_node(node); - reintroduced_nodes.push((vec![], idx, true)); + reintroduced_nodes.push((HashSet::new(), idx, true)); } } let mut edges = vec![]; @@ -630,7 +631,7 @@ impl Network { let link_states = link_states .into_iter() .filter(|ls| !removed.iter().any(|(idx, _)| idx == &ls.1)) - .collect::, NodeIndex, bool)>>(); + .collect::, NodeIndex, bool)>>(); if !self.autoconnect.is_empty() { // Connect discovered peers @@ -669,8 +670,8 @@ impl Network { #[allow(clippy::type_complexity)] // This is only used here if !link_states.is_empty() { let (new_idxs, updated_idxs): ( - Vec<(Vec, NodeIndex, bool)>, - Vec<(Vec, NodeIndex, bool)>, + Vec<(HashSet, NodeIndex, bool)>, + Vec<(HashSet, NodeIndex, bool)>, ) = link_states.into_iter().partition(|(_, _, new)| *new); for link in self.links.values() { let new_idxs = new_idxs @@ -746,7 +747,7 @@ impl Network { whatami: Some(whatami), locators: None, sn: 0, - links: vec![], + links: HashSet::new(), }), true, ) @@ -756,7 +757,7 @@ impl Network { tracing::trace!("Update edge (link) {} {}", self.graph[self.idx].zid, zid); self.update_edge(self.idx, idx); } - self.graph[self.idx].links.push(zid); + self.graph[self.idx].links.insert(zid); self.graph[self.idx].sn += 1; // Send updated self linkstate on all existing links except new one @@ -1015,10 +1016,8 @@ impl Network { } #[inline] - pub(super) fn get_links(&self, node: ZenohIdProto) -> &[ZenohIdProto] { - self.get_node(&node) - .map(|node| &node.links[..]) - .unwrap_or_default() + pub(super) fn get_links(&self, node: ZenohIdProto) -> Option<&HashSet> { + Some(&self.get_node(&node)?.links) } } diff --git a/zenoh/src/net/routing/hat/router/pubsub.rs b/zenoh/src/net/routing/hat/router/pubsub.rs index 4a77a850f1..044b0114dd 100644 --- a/zenoh/src/net/routing/hat/router/pubsub.rs +++ b/zenoh/src/net/routing/hat/router/pubsub.rs @@ -843,7 +843,7 @@ pub(super) fn pubsub_tree_change( pub(super) fn pubsub_linkstate_change( tables: &mut Tables, zid: &ZenohIdProto, - links: &[ZenohIdProto], + links: &HashSet, send_declare: &mut SendDeclare, ) { if let Some(mut src_face) = tables.get_face(zid).cloned() { diff --git a/zenoh/src/net/routing/hat/router/queries.rs b/zenoh/src/net/routing/hat/router/queries.rs index 6dbdc8ac23..0b1e37d5f9 100644 --- a/zenoh/src/net/routing/hat/router/queries.rs +++ b/zenoh/src/net/routing/hat/router/queries.rs @@ -13,7 +13,7 @@ // use std::{ borrow::Cow, - collections::HashMap, + collections::{HashMap, HashSet}, sync::{atomic::Ordering, Arc}, }; @@ -947,7 +947,7 @@ pub(super) fn queries_remove_node( pub(super) fn queries_linkstate_change( tables: &mut Tables, zid: &ZenohIdProto, - links: &[ZenohIdProto], + links: &HashSet, send_declare: &mut SendDeclare, ) { if let Some(mut src_face) = tables.get_face(zid).cloned() { diff --git a/zenoh/src/net/routing/hat/router/token.rs b/zenoh/src/net/routing/hat/router/token.rs index 0c6f432215..6147029529 100644 --- a/zenoh/src/net/routing/hat/router/token.rs +++ b/zenoh/src/net/routing/hat/router/token.rs @@ -12,7 +12,10 @@ // ZettaScale Zenoh Team, // -use std::sync::{atomic::Ordering, Arc}; +use std::{ + collections::HashSet, + sync::{atomic::Ordering, Arc}, +}; use petgraph::graph::NodeIndex; use zenoh_protocol::{ @@ -888,7 +891,7 @@ pub(super) fn token_tree_change( pub(super) fn token_linkstate_change( tables: &mut Tables, zid: &ZenohIdProto, - links: &[ZenohIdProto], + links: &HashSet, send_declare: &mut SendDeclare, ) { if let Some(mut src_face) = tables.get_face(zid).cloned() {