Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use BytesMut for Transmit content #1545

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions quinn-proto/src/connection/datagrams.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use bytes::Bytes;
use bytes::{Bytes, BytesMut};
use thiserror::Error;
use tracing::{debug, trace};

Expand Down Expand Up @@ -127,7 +127,7 @@ impl DatagramState {
Ok(was_empty)
}

pub(super) fn write(&mut self, buf: &mut Vec<u8>, max_size: usize) -> bool {
pub(super) fn write(&mut self, buf: &mut BytesMut, max_size: usize) -> bool {
let datagram = match self.outgoing.pop_front() {
Some(x) => x,
None => return false,
Expand Down
12 changes: 6 additions & 6 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl Connection {
SpaceId::Data,
"PATH_CHALLENGE queued without 1-RTT keys"
);
let mut buf = Vec::with_capacity(self.path.current_mtu() as usize);
let mut buf = BytesMut::with_capacity(self.path.current_mtu() as usize);
let buf_capacity = self.path.current_mtu() as usize;

let mut builder = PacketBuilder::new(
Expand Down 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 @@ -500,7 +500,7 @@ impl Connection {
_ => false,
};

let mut buf = Vec::new();
let mut buf = BytesMut::new();
// Reserving capacity can provide more capacity than we asked for.
// However we are not allowed to write more than MTU size. Therefore
// the maximum capacity is tracked separately.
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 Expand Up @@ -2809,7 +2809,7 @@ impl Connection {
fn populate_packet(
&mut self,
space_id: SpaceId,
buf: &mut Vec<u8>,
buf: &mut BytesMut,
max_size: usize,
) -> SentFrames {
let mut sent = SentFrames::default();
Expand Down Expand Up @@ -2981,7 +2981,7 @@ impl Connection {
receiving_ecn: bool,
sent: &mut SentFrames,
space: &mut PacketSpace,
buf: &mut Vec<u8>,
buf: &mut BytesMut,
stats: &mut ConnectionStats,
) {
debug_assert!(!space.pending_acks.ranges().is_empty());
Expand Down
8 changes: 4 additions & 4 deletions quinn-proto/src/connection/packet_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Instant;

use bytes::Bytes;
use bytes::{Bytes, BytesMut};
use rand::Rng;
use tracing::{trace, trace_span};

Expand Down Expand Up @@ -32,7 +32,7 @@ impl PacketBuilder {
pub(super) fn new(
now: Instant,
space_id: SpaceId,
buffer: &mut Vec<u8>,
buffer: &mut BytesMut,
buffer_capacity: usize,
datagram_start: usize,
ack_eliciting: bool,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl PacketBuilder {
now: Instant,
conn: &mut Connection,
sent: Option<SentFrames>,
buffer: &mut Vec<u8>,
buffer: &mut BytesMut,
) {
let ack_eliciting = self.ack_eliciting;
let exact_number = self.exact_number;
Expand Down Expand Up @@ -210,7 +210,7 @@ impl PacketBuilder {
}

/// Encrypt packet, returning the length of the packet and whether padding was added
pub(super) fn finish(self, conn: &mut Connection, buffer: &mut Vec<u8>) -> (usize, bool) {
pub(super) fn finish(self, conn: &mut Connection, buffer: &mut BytesMut) -> (usize, bool) {
let pad = buffer.len() < self.min_size;
if pad {
trace!("PADDING * {}", self.min_size - buffer.len());
Expand Down
12 changes: 6 additions & 6 deletions quinn-proto/src/connection/streams/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
mem,
};

use bytes::BufMut;
use bytes::{BufMut, BytesMut};
use rustc_hash::FxHashMap;
use tracing::{debug, trace};

Expand Down Expand Up @@ -349,7 +349,7 @@ impl StreamsState {

pub(in crate::connection) fn write_control_frames(
&mut self,
buf: &mut Vec<u8>,
buf: &mut BytesMut,
pending: &mut Retransmits,
retransmits: &mut ThinRetransmits,
stats: &mut FrameStats,
Expand Down Expand Up @@ -475,7 +475,7 @@ impl StreamsState {

pub(crate) fn write_stream_frames(
&mut self,
buf: &mut Vec<u8>,
buf: &mut BytesMut,
max_buf_size: usize,
) -> StreamMetaVec {
let mut stream_frames = StreamMetaVec::new();
Expand Down Expand Up @@ -874,7 +874,7 @@ mod tests {
connection::State as ConnState, connection::Streams, ReadableError, RecvStream, SendStream,
TransportErrorCode, WriteError,
};
use bytes::Bytes;
use bytes::{Bytes, BytesMut};

fn make(side: Side) -> StreamsState {
StreamsState::new(
Expand Down Expand Up @@ -1266,7 +1266,7 @@ mod tests {
high.set_priority(1).unwrap();
high.write(b"high").unwrap();

let mut buf = Vec::with_capacity(40);
let mut buf = BytesMut::with_capacity(40);
let meta = server.write_stream_frames(&mut buf, 40);
assert_eq!(meta[0].id, id_high);
assert_eq!(meta[1].id, id_mid);
Expand Down Expand Up @@ -1325,7 +1325,7 @@ mod tests {
};
high.set_priority(-1).unwrap();

let mut buf = Vec::with_capacity(1000);
let mut buf = BytesMut::with_capacity(1000);
let meta = server.write_stream_frames(&mut buf, 40);
assert_eq!(meta.len(), 1);
assert_eq!(meta[0].id, id_high);
Expand Down
18 changes: 9 additions & 9 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Endpoint {
}
trace!("sending version negotiation");
// Negotiate versions
let mut buf = Vec::<u8>::new();
let mut buf = BytesMut::new();
Header::VersionNegotiate {
random: self.rng.gen::<u8>() | 0x40,
src_cid: dst_cid,
Expand All @@ -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 @@ -340,15 +340,15 @@ impl Endpoint {
"sending stateless reset for {} to {}",
dst_cid, addresses.remote
);
let mut buf = Vec::<u8>::new();
let mut buf = BytesMut::new();
// Resets with at least this much padding can't possibly be distinguished from real packets
const IDEAL_MIN_PADDING_LEN: usize = MIN_PADDING_LEN + MAX_CID_SIZE;
let padding_len = if max_padding_len <= IDEAL_MIN_PADDING_LEN {
max_padding_len
} else {
self.rng.gen_range(IDEAL_MIN_PADDING_LEN..max_padding_len)
};
buf.reserve_exact(padding_len + RESET_TOKEN_SIZE);
buf.reserve(padding_len + RESET_TOKEN_SIZE);
buf.resize(padding_len, 0);
self.rng.fill_bytes(&mut buf[0..padding_len]);
buf[0] = 0b0100_0000 | buf[0] >> 2;
Expand All @@ -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 @@ -538,7 +538,7 @@ impl Endpoint {
version,
};

let mut buf = Vec::new();
let mut buf = BytesMut::new();
let encode = header.encode(&mut buf);
buf.put_slice(&token);
buf.extend_from_slice(&server_config.crypto.retry_tag(version, &dst_cid, &buf));
Expand All @@ -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 @@ -689,7 +689,7 @@ impl Endpoint {
version,
};

let mut buf = Vec::<u8>::new();
let mut buf = BytesMut::new();
let partial_encode = header.encode(&mut buf);
let max_len =
INITIAL_MTU as usize - partial_encode.header_len - crypto.packet.local.tag_len();
Expand All @@ -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
3 changes: 2 additions & 1 deletion quinn-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod tests;
pub mod transport_parameters;
mod varint;

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

mod connection;
Expand Down Expand Up @@ -277,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: Vec<u8>,
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
6 changes: 3 additions & 3 deletions quinn-proto/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub(crate) enum Header {
}

impl Header {
pub(crate) fn encode(&self, w: &mut Vec<u8>) -> PartialEncode {
pub(crate) fn encode(&self, w: &mut BytesMut) -> PartialEncode {
use self::Header::*;
let start = w.len();
match *self {
Expand Down Expand Up @@ -844,7 +844,7 @@ mod tests {

let dcid = ConnectionId::new(&hex!("06b858ec6f80452b"));
let client = initial_keys(Version::V1, &dcid, Side::Client);
let mut buf = Vec::new();
let mut buf = BytesMut::new();
let header = Header::Initial {
number: PacketNumber::U8(0),
src_cid: ConnectionId::new(&[]),
Expand Down Expand Up @@ -875,7 +875,7 @@ mod tests {

let server = initial_keys(Version::V1, &dcid, Side::Server);
let supported_versions = DEFAULT_SUPPORTED_VERSIONS.to_vec();
let decode = PartialDecode::new(buf.as_slice().into(), 0, &supported_versions, false)
let decode = PartialDecode::new(buf, 0, &supported_versions, false)
.unwrap()
.0;
let mut packet = decode.finish(Some(&*server.header.remote)).unwrap();
Expand Down
Loading