Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid temp allocations in get_best_key #1730

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions zenoh/src/net/routing/dispatcher/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,41 +533,68 @@ impl Resource {

#[inline]
pub fn get_best_key<'a>(prefix: &Arc<Resource>, suffix: &'a str, sid: usize) -> WireExpr<'a> {
fn get_best_key_<'a>(
fn get_best_child_key<'a>(
prefix: &Arc<Resource>,
suffix: &'a str,
sid: usize,
checkclildren: bool,
) -> WireExpr<'a> {
if checkclildren && !suffix.is_empty() {
) -> Option<WireExpr<'a>> {
if !suffix.is_empty() {
let (chunk, rest) = suffix.split_at(suffix.find('/').unwrap_or(suffix.len()));
if let Some(child) = prefix.children.get(chunk) {
return get_best_key_(child, rest, sid, true);
if let Some(key) = get_best_child_key(child, rest, sid) {
return Some(key);
}
}
}
if let Some(ctx) = prefix.session_ctxs.get(&sid) {
if let Some(expr_id) = ctx.remote_expr_id {
return WireExpr {
return Some(WireExpr {
scope: expr_id,
suffix: suffix.into(),
mapping: Mapping::Receiver,
};
});
} else if let Some(expr_id) = ctx.local_expr_id {
return WireExpr {
return Some(WireExpr {
scope: expr_id,
suffix: suffix.into(),
mapping: Mapping::Sender,
};
});
}
}
match &prefix.parent {
None
}
fn get_best_parent_key<'a>(
parent: Option<&Arc<Resource>>,
prefix: &Arc<Resource>,
suffix: &'a str,
sid: usize,
) -> WireExpr<'a> {
match parent {
Some(parent) => {
get_best_key_(parent, &[&prefix.suffix, suffix].concat(), sid, false).to_owned()
if let Some(ctx) = parent.session_ctxs.get(&sid) {
if let Some(expr_id) = ctx.remote_expr_id {
return WireExpr {
scope: expr_id,
suffix: (prefix.expr()[parent.expr().len()..].to_string() + suffix)
.into(),
mapping: Mapping::Receiver,
};
} else if let Some(expr_id) = ctx.local_expr_id {
return WireExpr {
scope: expr_id,
suffix: (prefix.expr()[parent.expr().len()..].to_string() + suffix)
.into(),
mapping: Mapping::Sender,
};
}
}
get_best_parent_key(parent.parent.as_ref(), prefix, suffix, sid).to_owned()
}
None => suffix.into(),
None => (prefix.expr().to_string() + suffix).into(),
}
}
get_best_key_(prefix, suffix, sid, true)
get_best_child_key(prefix, suffix, sid)
.unwrap_or_else(|| get_best_parent_key(prefix.parent.as_ref(), prefix, suffix, sid))
}

pub fn get_matches(tables: &Tables, key_expr: &keyexpr) -> Vec<Weak<Resource>> {
Expand Down
Loading