From a1af4238aec76c7050e52ca9ac1f9304ee2daee6 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 29 Apr 2024 05:39:48 +0000 Subject: [PATCH] virtio-devices: make ioeventfds() return an iterator MSHV's SEV-SNP implementation calls ioeventfds whenever there is an event. This change removes the need frequent allocation and deallocation of a vector, while at the same time makes sure other call sites are unaffected. Signed-off-by: Wei Liu --- virtio-devices/src/transport/mod.rs | 2 +- virtio-devices/src/transport/pci_device.rs | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/virtio-devices/src/transport/mod.rs b/virtio-devices/src/transport/mod.rs index 3a43b73cf..fae6f166b 100644 --- a/virtio-devices/src/transport/mod.rs +++ b/virtio-devices/src/transport/mod.rs @@ -9,5 +9,5 @@ pub use pci_common_config::{VirtioPciCommonConfig, VIRTIO_PCI_COMMON_CONFIG_ID}; pub use pci_device::{VirtioPciDevice, VirtioPciDeviceActivator, VirtioPciDeviceError}; pub trait VirtioTransport { - fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)>; + fn ioeventfds(&self, base_addr: u64) -> impl Iterator; } diff --git a/virtio-devices/src/transport/pci_device.rs b/virtio-devices/src/transport/pci_device.rs index 36f47c94d..c06b5cb1b 100644 --- a/virtio-devices/src/transport/pci_device.rs +++ b/virtio-devices/src/transport/pci_device.rs @@ -830,18 +830,14 @@ impl VirtioPciDevice { } impl VirtioTransport for VirtioPciDevice { - fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)> { + fn ioeventfds(&self, base_addr: u64) -> impl Iterator { let notify_base = base_addr + NOTIFICATION_BAR_OFFSET; - self.queue_evts() - .iter() - .enumerate() - .map(|(i, event)| { - ( - event, - notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER), - ) - }) - .collect() + self.queue_evts().iter().enumerate().map(move |(i, event)| { + ( + event, + notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER), + ) + }) } }