Repository navigation
IoUringState: replace HashMap with flat slice - #8599
Conversation
|
There are a lot of unsafe and panics, so why not |
There was a problem hiding this comment.
Neat idea, though I'm not a huge fan of this overall, because of added complexity. I'd prefer to keep io_uring code "as simple as reasonably possible". 😬
I see that generic RequestId might be useful in the future, but not yet convinced it will be.
For the fixed-sized State, I dunno. I'd say if we can "freeze" the capacity of HashMap (so there's no realloc), it's fine to just use a HashMap.
| pub struct IoUringState<'data, T> { | ||
| requests: ahash::HashMap<RequestId, IoUringRequest<'data, T>>, | ||
| pub struct IoUringState<'data, T, RequestId> { | ||
| requests: UninitSlice<(RequestId, IoUringRequest<'data, T>)>, |
There was a problem hiding this comment.
I'd just use Vec. I don't see what we gain by using boxed slice. inb4: saving usize worth of stack space, but come on...
There was a problem hiding this comment.
It's more about communication/intent, rather than about performance.
Box<[T]> means "not supposed to be resized", and Vec<T> means "maybe we will resize it, maybe not".
Tho, I plan to move this code away. I need experimenting, but it'll be either of these:
- I'll use
slab,stable-vec,slotmap, or whatever, as this idea is not new. - Or move this data structure into separate module like
common::slotmapto keep these unsafes away fromIoUringState. - Or just use
Vec<Option<…>>.
See next PR #8601. Generic |
b52ae31 to
d3c2d82
Compare
d3c2d82 to
522b6a2
Compare
This comment was marked as resolved.
This comment was marked as resolved.
|
Update: replaced my unsafe thingy with |
There was a problem hiding this comment.
♻️ Duplicate comments (2)
lib/common/common/src/universal_io/io_uring/runtime.rs (2)
19-23:⚠️ Potential issue | 🟠 MajorUse the kernel-reported SQ size in
enqueue_while, too.
new()now pulls the actual submission capacity, butenqueue_whilestill caps againstIO_URING_QUEUE_LENGTHand thenexpectspush()to succeed. If the SQ is smaller than that constant, this still panics.Suggested shape
- if self.in_progress + squeue.len() >= IO_URING_QUEUE_LENGTH as _ { + squeue.sync(); + if self.in_progress + squeue.len() >= squeue.capacity() { return Ok(()); } while let Some(entry) = entries(&mut self.state)? { - unsafe { squeue.push(&entry).expect("submission queue is not full") }; + if squeue.is_full() { + break; + } + unsafe { squeue.push(&entry).expect("checked with squeue.is_full") }; - if self.in_progress + squeue.len() >= IO_URING_QUEUE_LENGTH as _ { + if self.in_progress + squeue.len() >= squeue.capacity() { break; } }For the Rust `io-uring` crate, does `SubmissionQueue::capacity()` reflect the kernel-reported SQ size, and should `push()` be guarded with `sq.is_full()` / `sq.capacity()` (with `sq.sync()` if needed) instead of a separate configured constant?Also applies to: 38-46
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/common/common/src/universal_io/io_uring/runtime.rs` around lines 19 - 23, The code currently caps enqueue_while against the static IO_URING_QUEUE_LENGTH and calls push().expect(...), which can panic if the kernel SQ is smaller; change enqueue_while to use the runtime submission capacity from io_uring.submission().capacity() (the same value passed into IoUringState::with_capacity via get_io_uring()), and replace unconditional push().expect with a guarded loop that checks sq.is_full() or compares against sq.capacity() (and calls sq.sync()/sleep/wake as appropriate) before attempting push, handling the full case without panicking; apply the same fix to the other occurrence around the 38-46 block so all enqueues respect SubmissionQueue::capacity() rather than IO_URING_QUEUE_LENGTH.
104-113:⚠️ Potential issue | 🟠 MajorMake the abort path return the logical
RequestId.
abort(slot)currently drops the entry and swallows missing slots, so failed CQEs only report an internal slot number and state mismatches slip by silently. Returningio::Result<RequestId>here keeps the error actionable and preserves the same invariant checkfinalizealready has.Suggested shape
- pub fn abort(&mut self, slot: usize) { - self.requests.try_remove(slot); + pub fn abort(&mut self, slot: usize) -> io::Result<RequestId> { + let (id, _) = self + .requests + .try_remove(slot) + .ok_or_else(|| io::Error::other(format!("request in slot {slot} does not exist")))?; + Ok(id) }- self.state.abort(slot); + let id = self.state.abort(slot)?; return Err(io_error_context( io::Error::from_raw_os_error(-result), - format!("io_uring operation in slot {slot} failed"), + format!("io_uring request {id} failed"), ));Also applies to: 266-267
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@lib/common/common/src/universal_io/io_uring/runtime.rs`:
- Around line 19-23: The code currently caps enqueue_while against the static
IO_URING_QUEUE_LENGTH and calls push().expect(...), which can panic if the
kernel SQ is smaller; change enqueue_while to use the runtime submission
capacity from io_uring.submission().capacity() (the same value passed into
IoUringState::with_capacity via get_io_uring()), and replace unconditional
push().expect with a guarded loop that checks sq.is_full() or compares against
sq.capacity() (and calls sq.sync()/sleep/wake as appropriate) before attempting
push, handling the full case without panicking; apply the same fix to the other
occurrence around the 38-46 block so all enqueues respect
SubmissionQueue::capacity() rather than IO_URING_QUEUE_LENGTH.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5c6d2c9e-9be1-481f-b17f-fa512b2d3d6e
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (4)
lib/common/common/Cargo.tomllib/common/common/src/universal_io/io_uring/mod.rslib/common/common/src/universal_io/io_uring/read_iter.rslib/common/common/src/universal_io/io_uring/runtime.rs
✅ Files skipped from review due to trivial changes (1)
- lib/common/common/Cargo.toml
Replace HashMap with flat slice
pub struct IoUringState<T> { - requests: ahash::HashMap< - RequestId, - IoUringRequest<T>, - >, + requests: Vec<Option< + (RequestId, IoUringRequest<T>) + >>, }(in this PR it's more sophisticated than
Vec<Option<…>>, but the idea is the same)Currently, we use
HashMapto store requests. Performance-wise that means we need to hashRequestIdon eachio_uringrequest/response, and also probably do allocations.But since
io_uringhas fixed size queue, we can improve the performance by storing requests in a flat fixed-size array instead ofHashMap:MakeRequestIdto be a generic parameter(this change removed from this PR; it will go into #8601)
Details
Previously, we were passing
RequestIdintoio_uring'suser_data. In this PR we pass slot number (aka index withinIoUringState::request).Thus, now we don't need to check
RequestIdfor uniqueness. (collisions won't break the logic)Thus, now we can make
RequestIdto be generic parameter and let callers decide whichRequestIdto use:RequestId = usize.RequestId = ().RequestId, e.g.: