From e38cf2dcf915643f8ab4488787eae3807823c1f8 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Mon, 18 Dec 2023 18:29:35 -0800 Subject: [PATCH] Don't reallocate send buffer for every batch of transmits --- quinn/src/connection.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/quinn/src/connection.rs b/quinn/src/connection.rs index efc4370bf9..1c5af5b9c1 100644 --- a/quinn/src/connection.rs +++ b/quinn/src/connection.rs @@ -795,6 +795,7 @@ impl ConnectionRef { io_poller: socket.clone().create_io_poller(), socket, runtime, + send_buffer: BytesMut::new(), buffered_transmit: None, }), shared: Shared::default(), @@ -875,6 +876,7 @@ pub(crate) struct State { socket: Arc, io_poller: Pin>, runtime: Arc, + send_buffer: BytesMut, /// We buffer a transmit when the underlying I/O would block buffered_transmit: Option, } @@ -885,21 +887,23 @@ impl State { let mut transmits = 0; let max_datagrams = self.socket.max_transmit_segments(); - let capacity = self.inner.current_mtu(); - let mut buffer = BytesMut::with_capacity(capacity as usize); + self.send_buffer.reserve(self.inner.current_mtu() as usize); loop { // Retry the last transmit, or get a new one. let t = match self.buffered_transmit.take() { Some(t) => t, - None => match self.inner.poll_transmit(now, max_datagrams, &mut buffer) { + None => match self + .inner + .poll_transmit(now, max_datagrams, &mut self.send_buffer) + { Some(t) => { transmits += match t.segment_size { None => 1, Some(s) => (t.size + s - 1) / s, // round up }; - let buffer = buffer.split_to(t.size).freeze(); + let buffer = self.send_buffer.split_to(t.size).freeze(); udp_transmit(t, buffer) } None => break,