Skip to content

Commit

Permalink
Don't reallocate send buffer for every batch of transmits
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Dec 19, 2023
1 parent 55cede6 commit 0c65778
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions quinn/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ impl ConnectionRef {
ref_count: 0,
socket,
runtime,
send_buffer: BytesMut::new(),
buffered_transmit: None,
}),
shared: Shared::default(),
Expand Down Expand Up @@ -876,6 +877,7 @@ pub(crate) struct State {
ref_count: usize,
socket: Box<dyn AsyncUdpSocket>,
runtime: Arc<dyn Runtime>,
send_buffer: BytesMut,
/// We buffer a transmit when the underlying `poll_send` can't complete
buffered_transmit: Option<udp::Transmit>,
}
Expand All @@ -886,14 +888,13 @@ 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);

while let Some(t) = self.buffered_transmit.take().or_else(|| {
self.inner
.poll_transmit(now, max_datagrams, &mut buffer)
.poll_transmit(now, max_datagrams, &mut self.send_buffer)
.map(|t| {
let buffer = buffer.split_to(t.size).freeze();
let buffer = self.send_buffer.split_to(t.size).freeze();
udp_transmit(t, buffer)
})
}) {
Expand Down

0 comments on commit 0c65778

Please sign in to comment.