-
Notifications
You must be signed in to change notification settings - Fork 109
feat(driver): buffer pool #358
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d16a48e
feat(driver,iocp): buffer pool
Berrysoft 89c65ae
feat(driver): buffer ring
Berrysoft 694d538
ci: test buf-ring
Berrysoft 3334b53
fix(driver,fusion): check buf-ring
Berrysoft 5e4e226
fix(iocp): remove unused imports
Berrysoft 71dc458
fix(driver): return buffer back if op cancelled
Berrysoft 021c60d
fix(driver): use OwnedBuffer
Berrysoft c5f9dec
fix(driver): drop pool and forget owned buffer
Berrysoft 267603c
fix: warnings from clippy
Berrysoft 4706b3e
doc: add more comments
Berrysoft b3ab2c2
fix(iour): comments on reuse_buffer
Berrysoft 810d03b
Apply suggestions from code review
Berrysoft 7e7b401
fix(iour): make get_buffer return Result
Berrysoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| //! The fallback buffer pool. It is backed by a [`VecDeque`] of [`Vec<u8>`]. | ||
| //! An [`OwnedBuffer`] is selected when the op is created. It keeps a strong | ||
| //! reference to the buffer pool. The [`BorrowedBuffer`] is created after the op | ||
| //! returns successfully. | ||
|
|
||
| use std::{ | ||
| borrow::{Borrow, BorrowMut}, | ||
| cell::RefCell, | ||
| collections::VecDeque, | ||
| fmt::{Debug, Formatter}, | ||
| io, | ||
| mem::ManuallyDrop, | ||
| ops::{Deref, DerefMut}, | ||
| rc::Rc, | ||
| }; | ||
|
|
||
| use compio_buf::{IntoInner, IoBuf, IoBufMut, SetBufInit, Slice}; | ||
|
|
||
| struct BufferPoolInner { | ||
| buffers: RefCell<VecDeque<Vec<u8>>>, | ||
| } | ||
|
|
||
| impl BufferPoolInner { | ||
| pub(crate) fn add_buffer(&self, mut buffer: Vec<u8>) { | ||
| buffer.clear(); | ||
| self.buffers.borrow_mut().push_back(buffer) | ||
| } | ||
| } | ||
|
|
||
| /// Buffer pool | ||
| /// | ||
| /// A buffer pool to allow user no need to specify a specific buffer to do the | ||
| /// IO operation | ||
| pub struct BufferPool { | ||
| inner: Rc<BufferPoolInner>, | ||
| } | ||
|
|
||
| impl Debug for BufferPool { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("BufferPool").finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl BufferPool { | ||
| pub(crate) fn new(buffer_len: u16, buffer_size: usize) -> Self { | ||
| // To match the behavior of io-uring, extend the number of buffers. | ||
| let buffers = (0..buffer_len.next_power_of_two()) | ||
| .map(|_| Vec::with_capacity(buffer_size)) | ||
| .collect(); | ||
|
|
||
| Self { | ||
| inner: Rc::new(BufferPoolInner { | ||
| buffers: RefCell::new(buffers), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| /// Select an [`OwnedBuffer`] when the op creates. | ||
| pub(crate) fn get_buffer(&self, len: usize) -> io::Result<OwnedBuffer> { | ||
| let buffer = self | ||
| .inner | ||
| .buffers | ||
| .borrow_mut() | ||
| .pop_front() | ||
| .ok_or_else(|| io::Error::other("buffer ring has no available buffer"))?; | ||
| let len = if len == 0 { | ||
| buffer.capacity() | ||
| } else { | ||
| buffer.capacity().min(len) | ||
| }; | ||
| Ok(OwnedBuffer::new(buffer.slice(..len), self.inner.clone())) | ||
| } | ||
|
|
||
| /// Return the buffer to the pool. | ||
| pub(crate) fn add_buffer(&self, buffer: Vec<u8>) { | ||
| self.inner.add_buffer(buffer); | ||
| } | ||
|
|
||
| /// ## Safety | ||
| /// * `len` should be valid. | ||
| pub(crate) unsafe fn create_proxy(&self, mut slice: OwnedBuffer, len: usize) -> BorrowedBuffer { | ||
| unsafe { | ||
| slice.set_buf_init(len); | ||
| } | ||
| BorrowedBuffer::new(slice.into_inner(), self) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct OwnedBuffer { | ||
| buffer: ManuallyDrop<Slice<Vec<u8>>>, | ||
| pool: ManuallyDrop<Rc<BufferPoolInner>>, | ||
| } | ||
|
|
||
| impl OwnedBuffer { | ||
| fn new(buffer: Slice<Vec<u8>>, pool: Rc<BufferPoolInner>) -> Self { | ||
| Self { | ||
| buffer: ManuallyDrop::new(buffer), | ||
| pool: ManuallyDrop::new(pool), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| unsafe impl IoBuf for OwnedBuffer { | ||
| fn as_buf_ptr(&self) -> *const u8 { | ||
| self.buffer.as_buf_ptr() | ||
| } | ||
|
|
||
| fn buf_len(&self) -> usize { | ||
| self.buffer.buf_len() | ||
| } | ||
|
|
||
| fn buf_capacity(&self) -> usize { | ||
| self.buffer.buf_capacity() | ||
| } | ||
| } | ||
|
|
||
| unsafe impl IoBufMut for OwnedBuffer { | ||
| fn as_buf_mut_ptr(&mut self) -> *mut u8 { | ||
| self.buffer.as_buf_mut_ptr() | ||
| } | ||
| } | ||
|
|
||
| impl SetBufInit for OwnedBuffer { | ||
| unsafe fn set_buf_init(&mut self, len: usize) { | ||
| self.buffer.set_buf_init(len); | ||
| } | ||
| } | ||
|
|
||
| impl Drop for OwnedBuffer { | ||
| fn drop(&mut self) { | ||
| // Safety: `take` is called only once here. | ||
| self.pool | ||
| .add_buffer(unsafe { ManuallyDrop::take(&mut self.buffer) }.into_inner()); | ||
| // Safety: `drop` is called only once here. | ||
| unsafe { ManuallyDrop::drop(&mut self.pool) }; | ||
| } | ||
| } | ||
|
|
||
| impl IntoInner for OwnedBuffer { | ||
| type Inner = Slice<Vec<u8>>; | ||
|
|
||
| fn into_inner(mut self) -> Self::Inner { | ||
| // Safety: `self` is forgotten in this method. | ||
| let buffer = unsafe { ManuallyDrop::take(&mut self.buffer) }; | ||
| // The buffer is taken, we only need to drop the Rc. | ||
| // Safety: `self` is forgotten in this method. | ||
| unsafe { ManuallyDrop::drop(&mut self.pool) }; | ||
| std::mem::forget(self); | ||
| buffer | ||
| } | ||
| } | ||
|
|
||
| /// Buffer borrowed from buffer pool | ||
| /// | ||
| /// When IO operation finish, user will obtain a `BorrowedBuffer` to access the | ||
| /// filled data | ||
| pub struct BorrowedBuffer<'a> { | ||
| buffer: ManuallyDrop<Slice<Vec<u8>>>, | ||
| pool: &'a BufferPool, | ||
| } | ||
|
|
||
| impl<'a> BorrowedBuffer<'a> { | ||
| pub(crate) fn new(buffer: Slice<Vec<u8>>, pool: &'a BufferPool) -> Self { | ||
| Self { | ||
| buffer: ManuallyDrop::new(buffer), | ||
| pool, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Debug for BorrowedBuffer<'_> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("BorrowedBuffer").finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl Drop for BorrowedBuffer<'_> { | ||
| fn drop(&mut self) { | ||
| // Safety: `take` is called only once here. | ||
| let buffer = unsafe { ManuallyDrop::take(&mut self.buffer) }; | ||
| self.pool.add_buffer(buffer.into_inner()); | ||
| } | ||
| } | ||
|
|
||
| impl Deref for BorrowedBuffer<'_> { | ||
| type Target = [u8]; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.buffer.deref() | ||
| } | ||
| } | ||
|
|
||
| impl DerefMut for BorrowedBuffer<'_> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| self.buffer.deref_mut() | ||
| } | ||
| } | ||
|
|
||
| impl AsRef<[u8]> for BorrowedBuffer<'_> { | ||
| fn as_ref(&self) -> &[u8] { | ||
| self.deref() | ||
| } | ||
| } | ||
|
|
||
| impl AsMut<[u8]> for BorrowedBuffer<'_> { | ||
| fn as_mut(&mut self) -> &mut [u8] { | ||
| self.deref_mut() | ||
| } | ||
| } | ||
|
|
||
| impl Borrow<[u8]> for BorrowedBuffer<'_> { | ||
| fn borrow(&self) -> &[u8] { | ||
| self.deref() | ||
| } | ||
| } | ||
|
|
||
| impl BorrowMut<[u8]> for BorrowedBuffer<'_> { | ||
| fn borrow_mut(&mut self) -> &mut [u8] { | ||
| self.deref_mut() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.