Turn a SmallVec into VecDeque for performances (#6091)

* Turn a SmallVec into VecDeque for performances

* Fix the other SmallVecs
This commit is contained in:
Pierre Krieger
2020-05-20 21:12:49 +02:00
committed by GitHub
parent 717fa95bc7
commit c8339f9694
4 changed files with 58 additions and 59 deletions
@@ -30,7 +30,7 @@ use libp2p::swarm::{
};
use log::{debug, error};
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, error, fmt, io, mem, time::Duration};
use std::{borrow::Cow, collections::VecDeque, error, fmt, io, mem, time::Duration};
use std::{pin::Pin, task::{Context, Poll}};
/// Implements the `IntoProtocolsHandler` trait of libp2p.
@@ -117,7 +117,7 @@ impl IntoProtocolsHandler for LegacyProtoHandlerProto {
substreams: SmallVec::new(),
init_deadline: Delay::new(Duration::from_secs(20))
},
events_queue: SmallVec::new(),
events_queue: VecDeque::new(),
}
}
}
@@ -142,7 +142,7 @@ pub struct LegacyProtoHandler {
///
/// This queue must only ever be modified to insert elements at the back, or remove the first
/// element.
events_queue: SmallVec<[ProtocolsHandlerEvent<RegisteredProtocol, (), LegacyProtoHandlerOut, ConnectionKillError>; 16]>,
events_queue: VecDeque<ProtocolsHandlerEvent<RegisteredProtocol, (), LegacyProtoHandlerOut, ConnectionKillError>>,
}
/// State of the handler.
@@ -277,7 +277,7 @@ impl LegacyProtoHandler {
ProtocolState::Init { substreams: incoming, .. } => {
if incoming.is_empty() {
if let ConnectedPoint::Dialer { .. } = self.endpoint {
self.events_queue.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
self.events_queue.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(self.protocol.clone()),
info: (),
});
@@ -290,7 +290,7 @@ impl LegacyProtoHandler {
version: incoming[0].protocol_version(),
endpoint: self.endpoint.clone()
};
self.events_queue.push(ProtocolsHandlerEvent::Custom(event));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(event));
ProtocolState::Normal {
substreams: incoming.into_iter().collect(),
shutdown: SmallVec::new()
@@ -488,7 +488,7 @@ impl LegacyProtoHandler {
version: substream.protocol_version(),
endpoint: self.endpoint.clone()
};
self.events_queue.push(ProtocolsHandlerEvent::Custom(event));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(event));
ProtocolState::Normal {
substreams: smallvec![substream],
shutdown: SmallVec::new()
@@ -565,7 +565,7 @@ impl ProtocolsHandler for LegacyProtoHandler {
_ => false,
};
self.events_queue.push(ProtocolsHandlerEvent::Custom(LegacyProtoHandlerOut::ProtocolError {
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(LegacyProtoHandlerOut::ProtocolError {
is_severe,
error: Box::new(err),
}));
@@ -587,8 +587,7 @@ impl ProtocolsHandler for LegacyProtoHandler {
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>
> {
// Flush the events queue if necessary.
if !self.events_queue.is_empty() {
let event = self.events_queue.remove(0);
if let Some(event) = self.events_queue.pop_front() {
return Poll::Ready(event)
}
@@ -37,8 +37,7 @@ use libp2p::swarm::{
NegotiatedSubstream,
};
use log::{error, warn};
use smallvec::SmallVec;
use std::{borrow::Cow, fmt, pin::Pin, task::{Context, Poll}};
use std::{borrow::Cow, collections::VecDeque, fmt, pin::Pin, task::{Context, Poll}};
/// Implements the `IntoProtocolsHandler` trait of libp2p.
///
@@ -70,7 +69,7 @@ pub struct NotifsInHandler {
///
/// This queue is only ever modified to insert elements at the back, or remove the first
/// element.
events_queue: SmallVec<[ProtocolsHandlerEvent<DeniedUpgrade, (), NotifsInHandlerOut, void::Void>; 16]>,
events_queue: VecDeque<ProtocolsHandlerEvent<DeniedUpgrade, (), NotifsInHandlerOut, void::Void>>,
}
/// Event that can be received by a `NotifsInHandler`.
@@ -130,7 +129,7 @@ impl IntoProtocolsHandler for NotifsInHandlerProto {
in_protocol: self.in_protocol,
substream: None,
pending_accept_refuses: 0,
events_queue: SmallVec::new(),
events_queue: VecDeque::new(),
}
}
}
@@ -160,7 +159,7 @@ impl ProtocolsHandler for NotifsInHandler {
) {
// If a substream already exists, we drop it and replace it with the new incoming one.
if self.substream.is_some() {
self.events_queue.push(ProtocolsHandlerEvent::Custom(NotifsInHandlerOut::Closed));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(NotifsInHandlerOut::Closed));
}
// Note that we drop the existing substream, which will send an equivalent to a TCP "RST"
@@ -171,7 +170,7 @@ impl ProtocolsHandler for NotifsInHandler {
// and we can't close "more" than that anyway.
self.substream = Some(proto);
self.events_queue.push(ProtocolsHandlerEvent::Custom(NotifsInHandlerOut::OpenRequest(msg)));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(NotifsInHandlerOut::OpenRequest(msg)));
self.pending_accept_refuses = self.pending_accept_refuses
.checked_add(1)
.unwrap_or_else(|| {
@@ -233,8 +232,7 @@ impl ProtocolsHandler for NotifsInHandler {
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>
> {
// Flush the events queue if necessary.
if !self.events_queue.is_empty() {
let event = self.events_queue.remove(0);
if let Some(event) = self.events_queue.pop_front() {
return Poll::Ready(event)
}
@@ -35,8 +35,7 @@ use libp2p::swarm::{
};
use log::{debug, warn, error};
use prometheus_endpoint::Histogram;
use smallvec::SmallVec;
use std::{borrow::Cow, fmt, mem, pin::Pin, task::{Context, Poll}, time::Duration};
use std::{borrow::Cow, collections::VecDeque, fmt, mem, pin::Pin, task::{Context, Poll}, time::Duration};
use wasm_timer::Instant;
/// Maximum duration to open a substream and receive the handshake message. After that, we
@@ -85,7 +84,7 @@ impl IntoProtocolsHandler for NotifsOutHandlerProto {
when_connection_open: Instant::now(),
queue_size_report: self.queue_size_report,
state: State::Disabled,
events_queue: SmallVec::new(),
events_queue: VecDeque::new(),
peer_id: peer_id.clone(),
}
}
@@ -116,7 +115,7 @@ pub struct NotifsOutHandler {
///
/// This queue must only ever be modified to insert elements at the back, or remove the first
/// element.
events_queue: SmallVec<[ProtocolsHandlerEvent<NotificationsOut, (), NotifsOutHandlerOut, void::Void>; 16]>,
events_queue: VecDeque<ProtocolsHandlerEvent<NotificationsOut, (), NotifsOutHandlerOut, void::Void>>,
/// Who we are connected to.
peer_id: PeerId,
@@ -247,7 +246,7 @@ impl ProtocolsHandler for NotifsOutHandler {
match mem::replace(&mut self.state, State::Poisoned) {
State::Opening { initial_message } => {
let ev = NotifsOutHandlerOut::Open { handshake: handshake_msg };
self.events_queue.push(ProtocolsHandlerEvent::Custom(ev));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(ev));
self.state = State::Open { substream, initial_message };
},
// If the handler was disabled while we were negotiating the protocol, immediately
@@ -267,7 +266,7 @@ impl ProtocolsHandler for NotifsOutHandler {
match mem::replace(&mut self.state, State::Poisoned) {
State::Disabled => {
let proto = NotificationsOut::new(self.protocol_name.clone(), initial_message.clone());
self.events_queue.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
self.events_queue.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(proto).with_timeout(OPEN_TIMEOUT),
info: (),
});
@@ -287,7 +286,7 @@ impl ProtocolsHandler for NotifsOutHandler {
}
let proto = NotificationsOut::new(self.protocol_name.clone(), initial_message.clone());
self.events_queue.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
self.events_queue.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(proto).with_timeout(OPEN_TIMEOUT),
info: (),
});
@@ -347,7 +346,7 @@ impl ProtocolsHandler for NotifsOutHandler {
State::Opening { .. } => {
self.state = State::Refused;
let ev = NotifsOutHandlerOut::Refused;
self.events_queue.push(ProtocolsHandlerEvent::Custom(ev));
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(ev));
},
State::DisabledOpening => self.state = State::Disabled,
State::Poisoned => error!("☎️ Notifications handler in a poisoned state"),
@@ -371,9 +370,8 @@ impl ProtocolsHandler for NotifsOutHandler {
cx: &mut Context,
) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>> {
// Flush the events queue if necessary.
if !self.events_queue.is_empty() {
let event = self.events_queue.remove(0);
return Poll::Ready(event);
if let Some(event) = self.events_queue.pop_front() {
return Poll::Ready(event)
}
match &mut self.state {
@@ -385,7 +383,7 @@ impl ProtocolsHandler for NotifsOutHandler {
let initial_message = mem::replace(initial_message, Vec::new());
self.state = State::Opening { initial_message: initial_message.clone() };
let proto = NotificationsOut::new(self.protocol_name.clone(), initial_message);
self.events_queue.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
self.events_queue.push_back(ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(proto).with_timeout(OPEN_TIMEOUT),
info: (),
});