Skip to content

Commit

Permalink
perf: specialize slice extension in Datagram::encode
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 9, 2023
1 parent a354528 commit 25278c8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl Connection {
self.stats.udp_tx.bytes += buf.len() as u64;
return Some(Transmit {
destination,
contents: buf,
contents: buf.freeze(),
ecn: None,
segment_size: None,
src_ip: self.local_ip,
Expand Down Expand Up @@ -836,7 +836,7 @@ impl Connection {

Some(Transmit {
destination: self.path.remote,
contents: buf,
contents: buf.freeze(),
ecn: if self.path.sending_ecn {
Some(EcnCodepoint::Ect0)
} else {
Expand Down
8 changes: 4 additions & 4 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Endpoint {
self.transmits.push_back(Transmit {
destination: remote,
ecn: None,
contents: buf,
contents: buf.freeze(),
segment_size: None,
src_ip: local_ip,
});
Expand Down Expand Up @@ -359,7 +359,7 @@ impl Endpoint {
self.transmits.push_back(Transmit {
destination: addresses.remote,
ecn: None,
contents: buf,
contents: buf.freeze(),
segment_size: None,
src_ip: addresses.local_ip,
});
Expand Down Expand Up @@ -547,7 +547,7 @@ impl Endpoint {
self.transmits.push_back(Transmit {
destination: addresses.remote,
ecn: None,
contents: buf,
contents: buf.freeze(),
segment_size: None,
src_ip: addresses.local_ip,
});
Expand Down Expand Up @@ -703,7 +703,7 @@ impl Endpoint {
self.transmits.push_back(Transmit {
destination: addresses.remote,
ecn: None,
contents: buf,
contents: buf.freeze(),
segment_size: None,
src_ip: addresses.local_ip,
})
Expand Down
6 changes: 3 additions & 3 deletions quinn-proto/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ops::{Range, RangeInclusive},
};

use bytes::{Buf, BufMut, Bytes};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use tinyvec::TinyVec;

use crate::{
Expand Down Expand Up @@ -853,13 +853,13 @@ impl FrameStruct for Datagram {
}

impl Datagram {
pub(crate) fn encode<W: BufMut>(&self, length: bool, out: &mut W) {
pub(crate) fn encode(&self, length: bool, out: &mut BytesMut) {
out.write(Type(*DATAGRAM_TYS.start() | u64::from(length))); // 1 byte
if length {
// Safe to unwrap because we check length sanity before queueing datagrams
out.write(VarInt::from_u64(self.data.len() as u64).unwrap()); // <= 8 bytes
}
out.put_slice(&self.data);
out.extend_from_slice(&self.data);
}

pub(crate) fn size(&self, length: bool) -> usize {
Expand Down
4 changes: 2 additions & 2 deletions quinn-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests;
pub mod transport_parameters;
mod varint;

use bytes::BytesMut;
use bytes::Bytes;
pub use varint::{VarInt, VarIntBoundsExceeded};

mod connection;
Expand Down Expand Up @@ -278,7 +278,7 @@ pub struct Transmit {
/// Explicit congestion notification bits to set on the packet
pub ecn: Option<EcnCodepoint>,
/// Contents of the datagram
pub contents: BytesMut,
pub contents: Bytes,
/// The segment size if this transmission contains multiple datagrams.
/// This is `None` if the transmit only contains a single datagram
pub segment_size: Option<usize>,
Expand Down
17 changes: 11 additions & 6 deletions quinn-proto/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
};

use assert_matches::assert_matches;
use bytes::BytesMut;
use lazy_static::lazy_static;
use rustls::{Certificate, KeyLogFile, PrivateKey};
use tracing::{info_span, trace};
Expand Down Expand Up @@ -131,9 +132,11 @@ impl Pair {
socket.send_to(&x.contents, x.destination).unwrap();
}
if self.server.addr == x.destination {
self.server
.inbound
.push_back((self.time + self.latency, x.ecn, x.contents));
self.server.inbound.push_back((
self.time + self.latency,
x.ecn,
x.contents.as_ref().into(),
));
}
}
}
Expand All @@ -154,9 +157,11 @@ impl Pair {
socket.send_to(&x.contents, x.destination).unwrap();
}
if self.client.addr == x.destination {
self.client
.inbound
.push_back((self.time + self.latency, x.ecn, x.contents));
self.client.inbound.push_back((
self.time + self.latency,
x.ecn,
x.contents.as_ref().into(),
));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions quinn-udp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ log = ["tracing/log"]
maintenance = { status = "experimental" }

[dependencies]
bytes = "1"
libc = "0.2.113"
socket2 = "0.4" # 0.5.1 has an MSRV of 1.63
tracing = "0.1.10"
Expand Down
6 changes: 3 additions & 3 deletions quinn-udp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
time::{Duration, Instant},
};

use bytes::BytesMut;
use bytes::Bytes;
use tracing::warn;

#[cfg(unix)]
Expand Down Expand Up @@ -132,14 +132,14 @@ impl Default for RecvMeta {
}

/// An outgoing packet
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Transmit {
/// The socket this datagram should be sent to
pub destination: SocketAddr,
/// Explicit congestion notification bits to set on the packet
pub ecn: Option<EcnCodepoint>,
/// Contents of the datagram
pub contents: BytesMut,
pub contents: Bytes,
/// The segment size if this transmission contains multiple datagrams.
/// This is `None` if the transmit only contains a single datagram
pub segment_size: Option<usize>,
Expand Down

0 comments on commit 25278c8

Please sign in to comment.