Skip to content

Commit

Permalink
making the protocol name lengths more realiztic
Browse files Browse the repository at this point in the history
:
  • Loading branch information
jakubDoka committed Dec 25, 2023
1 parent 98b2eb1 commit c271dbd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
Binary file removed perf.data
Binary file not shown.
Binary file removed perf.data.old
Binary file not shown.
Binary file removed perf.data.opt
Binary file not shown.
16 changes: 14 additions & 2 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,20 @@ mod tests {
#[ignore]
fn repoll_with_active_protocols() {
fn run_benchmark(protcol_count: usize, iters: usize) {
const PROTOCOLS: &str =
"/a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s /t /u /v /w /x /y /z";
// we need longer protocol names
macro_rules! protocols {
($($name:ident)*) => {
concat!(
$(
"/",
stringify!($name),
"ffffffffffffffffffffff ",
)*
)
}
}

const PROTOCOLS: &str = protocols!(a b c d e f g h i j k l m n o p q r s t u v);
let protocols = PROTOCOLS.split(' ').collect::<Vec<_>>();

let mut connection = Connection::new(
Expand Down
17 changes: 13 additions & 4 deletions swarm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,31 @@ impl<'a> ProtocolsChange<'a> {
temp_owner: &'a mut Vec<StreamProtocol>,
) -> impl Iterator<Item = Self> {
temp_owner.clear();
existing_protocols.values_mut().for_each(|v| *v = false);
// to avoid looping trough the map to just set the booleans we use the fact that all of
// the booleans in the map have same value
let valid_value = !existing_protocols.values().next().copied().unwrap_or(false);
let mut count = Some(existing_protocols.len());
for new_protocol in new_protocols {
*existing_protocols
.entry(AsStrHashEq(new_protocol))
.or_insert_with_key(|k| {
temp_owner.extend(StreamProtocol::try_from_owned(k.0.as_ref().to_owned()).ok());
count = None;
false
}) = true;
}) = valid_value;
count = count.map(|c| c - 1);
}

if count == Some(0) {
return None.into_iter().chain(None);
}

let added_count = temp_owner.len();
existing_protocols.retain(|k, v| {
if !*v {
if *v != valid_value {
temp_owner.push(StreamProtocol::try_from_owned(k.0.as_ref().to_owned()).unwrap());
}
*v
*v == valid_value
});

let (added, removed) = temp_owner.split_at(added_count);
Expand Down

0 comments on commit c271dbd

Please sign in to comment.