Skip to content

Commit

Permalink
adding benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubDoka committed Dec 28, 2023
1 parent 5860200 commit adaaf7a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
perf.*
35 changes: 12 additions & 23 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use supported_protocols::SupportedProtocols;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, DialUpgradeError, FullyNegotiatedInbound,
FullyNegotiatedOutbound, ListenUpgradeError, ProtocolSupport, ProtocolsAdded, ProtocolsChange,
ProtocolsRemoved, UpgradeInfoSend,
UpgradeInfoSend,
};
use crate::stream::ActiveStreamCounter;
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
Expand Down Expand Up @@ -453,13 +453,14 @@ where
temp_protocols,
);

if changes.is_empty() {
return Poll::Pending;
if !changes.is_empty() {
for change in changes {
handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(change));
}
continue;
}

for change in changes {
handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(change));
}
return Poll::Pending;
}
}

Expand Down Expand Up @@ -874,22 +875,6 @@ mod tests {
#[ignore]
fn repoll_with_active_protocols() {
fn run_benchmark(protcol_count: usize, iters: usize) {
// 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(
StreamMuxerBox::new(PendingStreamMuxer),
ConfigurableProtocolConnectionHandler::default(),
Expand All @@ -898,7 +883,10 @@ mod tests {
Duration::ZERO,
);

connection.handler.listen_on(&protocols[..protcol_count]);
let protocols = (0..protcol_count)
.map(|i| &*format!("/protocol-ffffffff/{}", i).leak())
.collect::<Vec<_>>();
connection.handler.listen_on(&protocols);

let now = Instant::now();
for _ in 0..iters {
Expand All @@ -913,6 +901,7 @@ mod tests {
run_benchmark(4, iters);
run_benchmark(10, iters);
run_benchmark(20, iters);
run_benchmark(1000, iters / 10);
}

#[test]
Expand Down
19 changes: 9 additions & 10 deletions swarm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ use crate::StreamProtocol;
use core::slice;
use libp2p_core::Multiaddr;
use std::collections::{HashMap, HashSet};
use std::ops::Not;
use std::{error, fmt, io, task::Context, task::Poll, time::Duration};

/// A handler for a set of protocols used on a connection with a remote.
Expand Down Expand Up @@ -336,15 +335,15 @@ impl<'a> ProtocolsChange<'a> {
///
/// Returns `None` if the change is a no-op, i.e. `to_add` is a subset of `existing_protocols`.
pub(crate) fn add(
protocols: HashSet<StreamProtocol>,
remote_supported_protocols: &HashSet<StreamProtocol>,
to_add: HashSet<StreamProtocol>,
existing_protocols: &HashSet<StreamProtocol>,
temp_protocols: &'a mut Vec<StreamProtocol>,
) -> Option<Self> {
temp_protocols.clear();
temp_protocols.extend(
protocols
to_add
.into_iter()
.filter(|i| !remote_supported_protocols.contains(i)),
.filter(|i| !existing_protocols.contains(i)),
);

if temp_protocols.is_empty() {
Expand All @@ -356,19 +355,19 @@ impl<'a> ProtocolsChange<'a> {
}))
}

/// Compute the [`ProtocolsChange`] that results from removing `to_remove` from `existing_protocols`.
/// Compute the [`ProtocolsChange`] that results from removing `to_remove` from `existing_protocols`. Removes the protocols from `existing_protocols`.
///
/// Returns `None` if the change is a no-op, i.e. none of the protocols in `to_remove` are in `existing_protocols`.
pub(crate) fn remove(
protocols: HashSet<StreamProtocol>,
remote_supported_protocols: &HashSet<StreamProtocol>,
to_remove: HashSet<StreamProtocol>,
existing_protocols: &mut HashSet<StreamProtocol>,
temp_protocols: &'a mut Vec<StreamProtocol>,
) -> Option<Self> {
temp_protocols.clear();
temp_protocols.extend(
protocols
to_remove
.into_iter()
.filter_map(|i| remote_supported_protocols.take(&i)),
.filter_map(|i| existing_protocols.take(&i)),
);

if temp_protocols.is_empty() {
Expand Down

0 comments on commit adaaf7a

Please sign in to comment.