Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(driver,iocp): use Vec instead of ArrayVec #262

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ tempfile = "3.8.1"
tokio = "1.33.0"
widestring = "1.0.2"
windows-sys = "0.52.0"

[profile.bench]
debug = true
lto = true
codegen-units = 1
1 change: 0 additions & 1 deletion compio-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ socket2 = { workspace = true }

# Windows specific dependencies
[target.'cfg(windows)'.dependencies]
compio-buf = { workspace = true, features = ["arrayvec"] }
aligned-array = "1.0.1"
once_cell = { workspace = true }
windows-sys = { workspace = true, features = [
Expand Down
6 changes: 5 additions & 1 deletion compio-driver/src/iocp/cp/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ fn iocp_start() -> io::Result<()> {
let port = iocp_port()?;
std::thread::spawn(move || {
instrument!(compio_log::Level::TRACE, "iocp_start");
let mut entries = Vec::with_capacity(CompletionPort::DEFAULT_CAPACITY);
loop {
for entry in port.port.poll_raw(None)? {
let len = port.port.poll_raw(None, entries.spare_capacity_mut())?;
unsafe { entries.set_len(len) };

for entry in entries.drain(..) {
// Any thin pointer is OK because we don't use the type of opcode.
let overlapped_ptr: *mut Overlapped = entry.lpOverlapped.cast();
let overlapped = unsafe { &*overlapped_ptr };
Expand Down
22 changes: 12 additions & 10 deletions compio-driver/src/iocp/cp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

use std::{
io,
mem::MaybeUninit,
os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle},
time::Duration,
};

use compio_buf::arrayvec::ArrayVec;
use compio_log::*;
use windows_sys::Win32::{
Foundation::{
Expand Down Expand Up @@ -55,6 +55,8 @@ struct CompletionPort {
}

impl CompletionPort {
pub const DEFAULT_CAPACITY: usize = 1024;

pub fn new() -> io::Result<Self> {
let port = syscall!(BOOL, CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 1))?;
trace!("new iocp handle: {port}");
Expand Down Expand Up @@ -104,10 +106,8 @@ impl CompletionPort {
pub fn poll_raw(
&self,
timeout: Option<Duration>,
) -> io::Result<impl Iterator<Item = OVERLAPPED_ENTRY>> {
const DEFAULT_CAPACITY: usize = 1024;

let mut entries = ArrayVec::<OVERLAPPED_ENTRY, { DEFAULT_CAPACITY }>::new();
entries: &mut [MaybeUninit<OVERLAPPED_ENTRY>],
) -> io::Result<usize> {
let mut recv_count = 0;
let timeout = match timeout {
Some(timeout) => timeout.as_millis() as u32,
Expand All @@ -117,17 +117,16 @@ impl CompletionPort {
BOOL,
GetQueuedCompletionStatusEx(
self.port.as_raw_handle() as _,
entries.as_mut_ptr(),
DEFAULT_CAPACITY as _,
entries.as_mut_ptr().cast(),
entries.len() as _,
&mut recv_count,
timeout,
0
)
)?;
trace!("recv_count: {recv_count}");
unsafe { entries.set_len(recv_count as _) };

Ok(entries.into_iter())
Ok(recv_count as _)
}

// If current_driver is specified, any entry that doesn't belong the driver will
Expand All @@ -137,7 +136,10 @@ impl CompletionPort {
timeout: Option<Duration>,
current_driver: Option<RawFd>,
) -> io::Result<impl Iterator<Item = Entry>> {
Ok(self.poll_raw(timeout)?.filter_map(move |entry| {
let mut entries = Vec::with_capacity(Self::DEFAULT_CAPACITY);
let len = self.poll_raw(timeout, entries.spare_capacity_mut())?;
unsafe { entries.set_len(len) };
Ok(entries.into_iter().filter_map(move |entry| {
// Any thin pointer is OK because we don't use the type of opcode.
let overlapped_ptr: *mut Overlapped = entry.lpOverlapped.cast();
let overlapped = unsafe { &*overlapped_ptr };
Expand Down