Skip to content

Commit 9939253

Browse files
authored
Merge pull request #262 from Berrysoft/iocp-pool-vec
2 parents 8f83b7b + 4427d68 commit 9939253

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ tempfile = "3.8.1"
5555
tokio = "1.33.0"
5656
widestring = "1.0.2"
5757
windows-sys = "0.52.0"
58+
59+
[profile.bench]
60+
debug = true
61+
lto = true
62+
codegen-units = 1

compio-driver/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ socket2 = { workspace = true }
4040

4141
# Windows specific dependencies
4242
[target.'cfg(windows)'.dependencies]
43-
compio-buf = { workspace = true, features = ["arrayvec"] }
4443
aligned-array = "1.0.1"
4544
once_cell = { workspace = true }
4645
windows-sys = { workspace = true, features = [

compio-driver/src/iocp/cp/global.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ fn iocp_start() -> io::Result<()> {
5555
let port = iocp_port()?;
5656
std::thread::spawn(move || {
5757
instrument!(compio_log::Level::TRACE, "iocp_start");
58+
let mut entries = Vec::with_capacity(CompletionPort::DEFAULT_CAPACITY);
5859
loop {
59-
for entry in port.port.poll_raw(None)? {
60+
let len = port.port.poll_raw(None, entries.spare_capacity_mut())?;
61+
unsafe { entries.set_len(len) };
62+
63+
for entry in entries.drain(..) {
6064
// Any thin pointer is OK because we don't use the type of opcode.
6165
let overlapped_ptr: *mut Overlapped = entry.lpOverlapped.cast();
6266
let overlapped = unsafe { &*overlapped_ptr };

compio-driver/src/iocp/cp/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
1515
use std::{
1616
io,
17+
mem::MaybeUninit,
1718
os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle},
1819
time::Duration,
1920
};
2021

21-
use compio_buf::arrayvec::ArrayVec;
2222
use compio_log::*;
2323
use windows_sys::Win32::{
2424
Foundation::{
@@ -55,6 +55,8 @@ struct CompletionPort {
5555
}
5656

5757
impl CompletionPort {
58+
pub const DEFAULT_CAPACITY: usize = 1024;
59+
5860
pub fn new() -> io::Result<Self> {
5961
let port = syscall!(BOOL, CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 1))?;
6062
trace!("new iocp handle: {port}");
@@ -104,10 +106,8 @@ impl CompletionPort {
104106
pub fn poll_raw(
105107
&self,
106108
timeout: Option<Duration>,
107-
) -> io::Result<impl Iterator<Item = OVERLAPPED_ENTRY>> {
108-
const DEFAULT_CAPACITY: usize = 1024;
109-
110-
let mut entries = ArrayVec::<OVERLAPPED_ENTRY, { DEFAULT_CAPACITY }>::new();
109+
entries: &mut [MaybeUninit<OVERLAPPED_ENTRY>],
110+
) -> io::Result<usize> {
111111
let mut recv_count = 0;
112112
let timeout = match timeout {
113113
Some(timeout) => timeout.as_millis() as u32,
@@ -117,17 +117,16 @@ impl CompletionPort {
117117
BOOL,
118118
GetQueuedCompletionStatusEx(
119119
self.port.as_raw_handle() as _,
120-
entries.as_mut_ptr(),
121-
DEFAULT_CAPACITY as _,
120+
entries.as_mut_ptr().cast(),
121+
entries.len() as _,
122122
&mut recv_count,
123123
timeout,
124124
0
125125
)
126126
)?;
127127
trace!("recv_count: {recv_count}");
128-
unsafe { entries.set_len(recv_count as _) };
129128

130-
Ok(entries.into_iter())
129+
Ok(recv_count as _)
131130
}
132131

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

0 commit comments

Comments
 (0)