Skip to content

Commit

Permalink
Cache full expression in Resource tree
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHecart committed Dec 18, 2024
1 parent 608b0eb commit 052a751
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 149 deletions.
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/dispatcher/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl FaceState {
}

pub(crate) fn update_interceptors_caches(&self, res: &mut Arc<Resource>) {
if let Ok(expr) = KeyExpr::try_from(res.expr()) {
if let Ok(expr) = KeyExpr::try_from(res.expr_to_string()) {
if let Some(interceptor) = self.in_interceptors.as_ref() {
let cache = interceptor.compute_keyexpr_cache(&expr);
get_mut_unchecked(
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/dispatcher/interests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub(crate) fn declare_interest(
let wtables = zwrite!(tables_ref.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/dispatcher/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) fn declare_subscription(
let wtables = zwrite!(tables.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/dispatcher/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub(crate) fn declare_queryable(
let wtables = zwrite!(tables.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down
30 changes: 17 additions & 13 deletions zenoh/src/net/routing/dispatcher/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl ResourceContext {

pub struct Resource {
pub(crate) parent: Option<Arc<Resource>>,
pub(crate) expr: String,
pub(crate) suffix: String,
pub(crate) nonwild_prefix: Option<(Arc<Resource>, String)>,
pub(crate) children: HashMap<String, Arc<Resource>>,
Expand Down Expand Up @@ -211,6 +212,7 @@ impl Resource {

Resource {
parent: Some(parent.clone()),
expr: parent.expr.clone() + suffix,
suffix: String::from(suffix),
nonwild_prefix,
children: HashMap::new(),
Expand All @@ -219,11 +221,12 @@ impl Resource {
}
}

pub fn expr(&self) -> String {
match &self.parent {
Some(parent) => parent.expr() + &self.suffix,
None => String::from(""),
}
pub fn expr_to_string(&self) -> String {
self.expr.clone()
}

pub fn expr(&self) -> &str {
&self.expr
}

#[inline(always)]
Expand Down Expand Up @@ -253,7 +256,7 @@ impl Resource {
if !nonwild_prefix.expr().is_empty() {
(Some(nonwild_prefix.clone()), wildsuffix.clone())
} else {
(None, res.expr())
(None, res.expr().to_string())
}
}
}
Expand Down Expand Up @@ -295,6 +298,7 @@ impl Resource {
pub fn root() -> Arc<Resource> {
Arc::new(Resource {
parent: None,
expr: String::from(""),
suffix: String::from(""),
nonwild_prefix: None,
children: HashMap::new(),
Expand Down Expand Up @@ -345,7 +349,7 @@ impl Resource {

#[cfg(test)]
pub fn print_tree(from: &Arc<Resource>) -> String {
let mut result = from.expr();
let mut result = from.expr().to_string();
result.push('\n');
for child in from.children.values() {
result.push_str(&Resource::print_tree(child));
Expand Down Expand Up @@ -511,10 +515,10 @@ impl Resource {
ext_nodeid: ext::NodeIdType::DEFAULT,
body: DeclareBody::DeclareKeyExpr(DeclareKeyExpr {
id: expr_id,
wire_expr: nonwild_prefix.expr().into(),
wire_expr: nonwild_prefix.expr_to_string().into(),
}),
},
nonwild_prefix.expr(),
nonwild_prefix.expr_to_string(),
));
face.update_interceptors_caches(&mut nonwild_prefix);
WireExpr {
Expand All @@ -523,7 +527,7 @@ impl Resource {
mapping: Mapping::Sender,
}
} else {
res.expr().into()
res.expr_to_string().into()
}
}
None => wildsuffix.into(),
Expand Down Expand Up @@ -701,7 +705,7 @@ pub(crate) fn register_expr(
{
Some(mut prefix) => match face.remote_mappings.get(&expr_id) {
Some(res) => {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
if res.expr() != fullexpr {
tracing::error!(
Expand All @@ -722,7 +726,7 @@ pub(crate) fn register_expr(
let wtables = zwrite!(tables.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down Expand Up @@ -786,7 +790,7 @@ pub(crate) fn register_expr_interest(
let wtables = zwrite!(tables.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/dispatcher/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> RoutingExpr<'a> {
#[inline]
pub(crate) fn full_expr(&mut self) -> &str {
if self.full.is_none() {
self.full = Some(self.prefix.expr() + self.suffix);
self.full = Some(self.prefix.expr_to_string() + self.suffix);
}
self.full.as_ref().unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/net/routing/dispatcher/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn declare_token(
let wtables = zwrite!(tables.tables);
(res.unwrap(), wtables)
} else {
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down Expand Up @@ -123,7 +123,7 @@ pub(crate) fn undeclare_token(
None => {
// Here we create a Resource that will immediately be removed after treatment
// TODO this could be improved
let mut fullexpr = prefix.expr();
let mut fullexpr = prefix.expr_to_string();
fullexpr.push_str(expr.wire_expr.suffix.as_ref());
let mut matches = keyexpr::new(fullexpr.as_str())
.map(|ke| Resource::get_matches(&rtables, ke))
Expand Down
10 changes: 7 additions & 3 deletions zenoh/src/net/routing/hat/client/interests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ pub(super) fn interests_new_face(tables: &mut Tables, face: &mut Arc<FaceState>)
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
},
res.as_ref().map(|res| res.expr()).unwrap_or_default(),
res.as_ref()
.map(|res| res.expr_to_string())
.unwrap_or_default(),
));
}
}
Expand Down Expand Up @@ -146,7 +148,9 @@ impl HatInterestTrait for HatCode {
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
},
res.as_ref().map(|res| res.expr()).unwrap_or_default(),
res.as_ref()
.map(|res| res.expr_to_string())
.unwrap_or_default(),
));
}

Expand Down Expand Up @@ -223,7 +227,7 @@ impl HatInterestTrait for HatCode {
local_interest
.res
.as_ref()
.map(|res| res.expr())
.map(|res| res.expr_to_string())
.unwrap_or_default(),
));
get_mut_unchecked(dst_face).local_interests.remove(&id);
Expand Down
6 changes: 3 additions & 3 deletions zenoh/src/net/routing/hat/client/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn propagate_simple_subscription_to(
wire_expr: key_expr,
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ fn propagate_forget_simple_subscription(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -214,7 +214,7 @@ pub(super) fn undeclare_simple_subscription(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions zenoh/src/net/routing/hat/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn propagate_simple_queryable(
ext_info: info,
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ fn propagate_forget_simple_queryable(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ pub(super) fn undeclare_simple_queryable(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down
16 changes: 8 additions & 8 deletions zenoh/src/net/routing/hat/client/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn propagate_simple_token_to(
wire_expr: key_expr,
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ fn declare_simple_token(
ext_nodeid: ext::NodeIdType::default(),
body: DeclareBody::DeclareToken(DeclareToken { id, wire_expr }),
},
res.expr(),
res.expr_to_string(),
),
);
return;
Expand Down Expand Up @@ -185,7 +185,7 @@ fn propagate_forget_simple_token(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
} else if face_hat!(face)
Expand All @@ -210,7 +210,7 @@ fn propagate_forget_simple_token(
},
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -253,7 +253,7 @@ pub(super) fn undeclare_simple_token(
ext_wire_expr: WireExprType::null(),
}),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -343,7 +343,7 @@ pub(crate) fn declare_token_interest(
ext_nodeid: ext::NodeIdType::DEFAULT,
body: DeclareBody::DeclareToken(DeclareToken { id, wire_expr }),
},
res.expr(),
res.expr_to_string(),
),
);
}
Expand Down Expand Up @@ -372,7 +372,7 @@ pub(crate) fn declare_token_interest(
wire_expr,
}),
},
res.expr(),
res.expr_to_string(),
),
)
}
Expand Down Expand Up @@ -400,7 +400,7 @@ pub(crate) fn declare_token_interest(
ext_nodeid: ext::NodeIdType::DEFAULT,
body: DeclareBody::DeclareToken(DeclareToken { id, wire_expr }),
},
token.expr(),
token.expr_to_string(),
),
);
}
Expand Down
Loading

0 comments on commit 052a751

Please sign in to comment.