Skip to content

Commit d6508cc

Browse files
committed
tweak buffer sizes and remove unsafe
1 parent 86e2d66 commit d6508cc

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

  • pulsebeam-runtime/src/net

pulsebeam-runtime/src/net/udp.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ use std::{
88
net::SocketAddr,
99
};
1010

11-
// Up to 8x IO loop latency, a bit of headroom for keyframe bursts.
12-
// With 1ms scheduling delay, this is capped to 8ms latency.
13-
pub const SOCKET_RECV_SIZE: usize = 8 * BATCH_SIZE * CHUNK_SIZE;
14-
// per-client-pacer handles the latency bloat. But, big enough for keyframe bursts to many subscribers
15-
pub const SOCKET_SEND_SIZE: usize = 32 * BATCH_SIZE * CHUNK_SIZE;
11+
pub const SOCKET_SEND_SIZE: usize = 2 * 1024 * 1024;
12+
pub const SOCKET_RECV_SIZE: usize = 4 * 1024 * 1024;
1613

1714
pub struct UdpTransport {
1815
reader: UdpTransportReader,
@@ -85,7 +82,7 @@ pub async fn bind(addr: SocketAddr, external_addr: Option<SocketAddr>) -> io::Re
8582
state: state.clone(),
8683
local_addr,
8784
meta: [RecvMeta::default(); BATCH_SIZE],
88-
batch_buffer: Vec::with_capacity(BATCH_SIZE * CHUNK_SIZE),
85+
batch_buffer: vec![0u8; BATCH_SIZE * CHUNK_SIZE],
8986
};
9087

9188
let writer = UdpTransportWriter {
@@ -134,23 +131,15 @@ impl UdpTransportReader {
134131
#[inline]
135132
pub fn try_recv_batch(&mut self, out: &mut Vec<RecvPacketBatch>) -> std::io::Result<usize> {
136133
self.sock.try_io(tokio::io::Interest::READABLE, || {
137-
// Prepare the pointers for the kernel
138-
// We set len=capacity so we can take mutable slices of the uninitialized memory
139-
unsafe { self.batch_buffer.set_len(self.batch_buffer.capacity()) };
140-
let ptr = self.batch_buffer.as_mut_ptr();
141-
142-
let mut slices: [IoSliceMut; BATCH_SIZE] = std::array::from_fn(|i| {
143-
let offset = i * CHUNK_SIZE;
144-
// SAFETY: We know the buffer is BATCH_SIZE * CHUNK_SIZE large
145-
unsafe {
146-
IoSliceMut::new(std::slice::from_raw_parts_mut(ptr.add(offset), CHUNK_SIZE))
147-
}
134+
let mut chunks = self.batch_buffer.chunks_exact_mut(CHUNK_SIZE);
135+
let mut slices: [IoSliceMut; BATCH_SIZE] = std::array::from_fn(|_| {
136+
let chunk = chunks.next().expect("batch_buffer is sized correctly");
137+
IoSliceMut::new(chunk)
148138
});
149139

150140
let res = self
151141
.state
152142
.recv((&*self.sock).into(), &mut slices, &mut self.meta);
153-
let _ = slices; // slices is no longer safe to use
154143

155144
match res {
156145
Ok(count) => {

0 commit comments

Comments
 (0)