Skip to content

Commit d297e05

Browse files
committed
feat(yamux): Reuse the recv buffer
1 parent 6d8f1d9 commit d297e05

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

p2p/src/network/yamux/p2p_network_yamux_reducer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ impl P2pNetworkYamuxState {
4747

4848
match action {
4949
P2pNetworkYamuxAction::IncomingData { data, addr } => {
50+
// TODO: this may grow over the capacity, if that happens make sure to eventually
51+
// truncate it to a reasonable size.
5052
yamux_state.buffer.extend_from_slice(&data);
5153
let mut offset = 0;
5254
loop {
@@ -141,7 +143,9 @@ impl P2pNetworkYamuxState {
141143
break;
142144
}
143145

144-
yamux_state.buffer = yamux_state.buffer[offset..].to_vec();
146+
let new_len = yamux_state.buffer.len() - offset;
147+
yamux_state.buffer.copy_within(offset.., 0);
148+
yamux_state.buffer.truncate(new_len);
145149

146150
let frame_count = yamux_state.incoming.len();
147151
let dispatcher = state_context.into_dispatcher();

p2p/src/network/yamux/p2p_network_yamux_state.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
use super::super::*;
66

7-
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
7+
#[derive(Serialize, Deserialize, Debug, Clone)]
88
pub struct P2pNetworkYamuxState {
99
pub message_size_limit: Limit<usize>,
1010
pub pending_outgoing_limit: Limit<usize>,
@@ -15,6 +15,20 @@ pub struct P2pNetworkYamuxState {
1515
pub init: bool,
1616
}
1717

18+
impl Default for P2pNetworkYamuxState {
19+
fn default() -> Self {
20+
Self {
21+
message_size_limit: Default::default(),
22+
pending_outgoing_limit: Default::default(),
23+
buffer: Vec::with_capacity(0x40000), // 256kb
24+
incoming: VecDeque::with_capacity(10), // TODO: measure and see what is a good default
25+
streams: Default::default(),
26+
terminated: Default::default(),
27+
init: Default::default(),
28+
}
29+
}
30+
}
31+
1832
impl P2pNetworkYamuxState {
1933
/// Calculates and returns the next available stream ID for outgoing
2034
/// communication.

0 commit comments

Comments
 (0)