Skip to content

Commit 8567782

Browse files
committed
chmux: minor performance improvements
1 parent d2a46a4 commit 8567782

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

remoc/src/chmux/msg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ impl MultiplexMsg {
311311
let first = flags & MSG_PORT_DATA_FLAG_FIRST != 0;
312312
let last = flags & MSG_PORT_DATA_FLAG_LAST != 0;
313313
let wait = flags & MSG_PORT_DATA_FLAG_WAIT != 0;
314-
let mut ids = (flags & MSG_PORT_DATA_FLAG_IDS != 0).then_some(Vec::new());
315-
let mut ports = Vec::new();
314+
let mut ids = (flags & MSG_PORT_DATA_FLAG_IDS != 0).then_some(Vec::with_capacity(16));
315+
let mut ports = Vec::with_capacity(16);
316316
loop {
317317
match reader.read_u32::<LE>() {
318318
Ok(p) => ports.push(p),
@@ -340,7 +340,7 @@ impl MultiplexMsg {
340340
}
341341

342342
pub(crate) fn to_vec(&self) -> Vec<u8> {
343-
let mut data = Vec::new();
343+
let mut data = Vec::with_capacity(MAX_MSG_LENGTH);
344344
self.write(&mut data).expect("message serialization failed");
345345
data
346346
}

remoc/src/chmux/mux.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use tokio::{
2323
sync::{mpsc, mpsc::Permit, oneshot},
2424
try_join,
2525
};
26+
use tokio_util::sync::ReusableBoxFuture;
2627

2728
use super::{
2829
AnyStorage, Cfg, ChMuxError, PROTOCOL_VERSION, PROTOCOL_VERSION_PORT_ID, PortReq,
@@ -535,7 +536,7 @@ where
535536
}
536537
}
537538

538-
let mut next_ping = get_next_ping(ping_interval).fuse().boxed();
539+
let mut next_ping = ReusableBoxFuture::new(get_next_ping(ping_interval));
539540
let mut need_flush = false;
540541

541542
loop {
@@ -555,7 +556,7 @@ where
555556
break;
556557
}
557558

558-
next_ping = get_next_ping(ping_interval).fuse().boxed();
559+
next_ping.set(get_next_ping(ping_interval));
559560
need_flush = true;
560561
}
561562
None => break,
@@ -564,7 +565,7 @@ where
564565

565566
() = &mut next_ping => {
566567
Self::feed_msg(TransportMsg::new(MultiplexMsg::Ping), sink).await?;
567-
next_ping = get_next_ping(ping_interval).fuse().boxed();
568+
next_ping.set(get_next_ping(ping_interval));
568569
need_flush = true;
569570
}
570571

@@ -595,7 +596,8 @@ where
595596
}
596597
}
597598

598-
let mut next_timeout = get_connection_timeout(connection_timeout).fuse().boxed();
599+
let mut next_timeout = ReusableBoxFuture::new(get_connection_timeout(connection_timeout));
600+
599601
while let Ok(tx_permit) = tx.reserve().await {
600602
tokio::select! {
601603
biased;
@@ -608,7 +610,7 @@ where
608610
break;
609611
}
610612

611-
next_timeout = get_connection_timeout(connection_timeout).fuse().boxed();
613+
next_timeout.set(get_connection_timeout(connection_timeout));
612614
},
613615

614616
() = &mut next_timeout => return Err(ChMuxError::Timeout),

0 commit comments

Comments
 (0)