Skip to content

Commit e75e77b

Browse files
hikejsclaude
andcommitted
perf(devices): optimize memory allocations in virtio hot paths
Reduce heap allocations in performance-critical virtio device processing: 1. vsock_windows.rs: - Use fixed 4KB stack buffer instead of Vec in harvest_stream_reads - Eliminates heap allocation on every stream read iteration 2. net_windows.rs: - Use stack array for virtio-net header (was Vec) - Pre-allocate frame_data with 1500-byte capacity (typical MTU) - Remove unnecessary descriptor collection (iterate directly) - Remove unused DescriptorChain import These changes reduce allocator pressure in the TX/RX hot paths without changing behavior. All WHPX tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9d61ea0 commit e75e77b

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

src/devices/src/virtio/net_windows.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use utils::eventfd::{EventFd, EFD_NONBLOCK};
2020
use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
2121

2222
use super::{
23-
ActivateError, ActivateResult, DescriptorChain, DeviceState, InterruptTransport, Queue,
23+
ActivateError, ActivateResult, DeviceState, InterruptTransport, Queue,
2424
VirtioDevice, TYPE_NET,
2525
};
2626

@@ -183,12 +183,11 @@ impl Net {
183183
while let Some(head) = self.queues[TX_INDEX].pop(mem) {
184184
let index = head.index;
185185
let mut total_len: u32 = 0;
186-
let mut hdr_bytes = vec![0u8; VIRTIO_NET_HDR_SIZE];
186+
let mut hdr_bytes = [0u8; VIRTIO_NET_HDR_SIZE];
187187
let mut hdr_bytes_read: usize = 0;
188-
let mut frame_data = Vec::new();
188+
let mut frame_data = Vec::with_capacity(1500); // Pre-allocate for typical MTU
189189

190-
let descs: Vec<DescriptorChain<'_>> = head.into_iter().collect();
191-
for desc in &descs {
190+
for desc in head.into_iter() {
192191
if desc.is_write_only() {
193192
continue;
194193
}

src/devices/src/virtio/vsock_windows.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const QUEUE_SIZE: u16 = 256;
3131

3232
const VIRTIO_F_VERSION_1: u32 = 32;
3333
const VIRTIO_F_IN_ORDER: usize = 35;
34+
#[allow(dead_code)] // Reserved for future DGRAM implementation
3435
const VIRTIO_VSOCK_F_DGRAM: u32 = 3;
3536
const VSOCK_HOST_CID: u64 = 2;
3637

@@ -547,8 +548,8 @@ impl Vsock {
547548

548549
// Read up to the available credit amount
549550
let read_size = (available_credit as usize).min(4096);
550-
let mut rx_buf = vec![0_u8; read_size];
551-
match state.stream.read(&mut rx_buf) {
551+
let mut rx_buf = [0u8; 4096];
552+
match state.stream.read(&mut rx_buf[..read_size]) {
552553
Ok(n) if n > 0 => {
553554
// Update tx_cnt to track bytes sent to peer
554555
state.tx_cnt = state.tx_cnt.saturating_add(n as u32);

0 commit comments

Comments
 (0)