-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Remove some redundant checks from BufReader #98748
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
761ddf3
Remove some redundant checks from BufReader
saethlin b9497be
Allow Buffer methods to inline
saethlin 5e5ce43
Rename and document the new BufReader internals
saethlin 5fa1926
Add Buffer::consume_with to enable direct buffer access with one check
saethlin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
///! An encapsulation of `BufReader`'s buffer management logic. | ||
/// | ||
/// This module factors out the basic functionality of `BufReader` in order to protect two core | ||
/// invariants: | ||
/// * `filled` bytes of `buf` are always initialized | ||
/// * `pos` is always <= `filled` | ||
/// Since this module encapsulates the buffer management logic, we can ensure that the range | ||
/// `pos..filled` is always a valid index into the initialized region of the buffer. This means | ||
/// that user code which wants to do reads from a `BufReader` via `buffer` + `consume` can do so | ||
/// without encountering any runtime bounds checks. | ||
use crate::cmp; | ||
saethlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use crate::io::{self, Read, ReadBuf}; | ||
use crate::mem::MaybeUninit; | ||
|
||
pub struct Buffer { | ||
// The buffer. | ||
buf: Box<[MaybeUninit<u8>]>, | ||
// The current seek offset into `buf`, must always be <= `filled`. | ||
pos: usize, | ||
// Each call to `fill_buf` sets `filled` to indicate how many bytes at the start of `buf` are | ||
// initialized with bytes from a read. | ||
filled: usize, | ||
} | ||
|
||
impl Buffer { | ||
#[inline] | ||
pub fn with_capacity(capacity: usize) -> Self { | ||
let buf = Box::new_uninit_slice(capacity); | ||
Self { buf, pos: 0, filled: 0 } | ||
} | ||
|
||
#[inline] | ||
pub fn buffer(&self) -> &[u8] { | ||
// SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and | ||
// that region is initialized because those are all invariants of this type. | ||
unsafe { MaybeUninit::slice_assume_init_ref(self.buf.get_unchecked(self.pos..self.filled)) } | ||
} | ||
|
||
#[inline] | ||
pub fn capacity(&self) -> usize { | ||
self.buf.len() | ||
} | ||
|
||
#[inline] | ||
pub fn filled(&self) -> usize { | ||
self.filled | ||
} | ||
|
||
#[inline] | ||
pub fn pos(&self) -> usize { | ||
self.pos | ||
} | ||
|
||
#[inline] | ||
pub fn discard_buffer(&mut self) { | ||
self.pos = 0; | ||
self.filled = 0; | ||
} | ||
|
||
#[inline] | ||
pub fn consume(&mut self, amt: usize) { | ||
self.pos = cmp::min(self.pos + amt, self.filled); | ||
} | ||
|
||
/// If there are `amt` bytes available in the buffer, pass a slice containing those bytes to | ||
/// `visitor` and return true. If there are not enough bytes available, return false. | ||
#[inline] | ||
pub fn consume_with<V>(&mut self, amt: usize, mut visitor: V) -> bool | ||
where | ||
V: FnMut(&[u8]), | ||
{ | ||
if let Some(claimed) = self.buffer().get(..amt) { | ||
visitor(claimed); | ||
// If the indexing into self.buffer() succeeds, amt must be a valid increment. | ||
self.pos += amt; | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn unconsume(&mut self, amt: usize) { | ||
self.pos = self.pos.saturating_sub(amt); | ||
} | ||
|
||
#[inline] | ||
pub fn fill_buf(&mut self, mut reader: impl Read) -> io::Result<&[u8]> { | ||
// If we've reached the end of our internal buffer then we need to fetch | ||
// some more data from the reader. | ||
// Branch using `>=` instead of the more correct `==` | ||
// to tell the compiler that the pos..cap slice is always valid. | ||
if self.pos >= self.filled { | ||
debug_assert!(self.pos == self.filled); | ||
|
||
let mut readbuf = ReadBuf::uninit(&mut self.buf); | ||
|
||
reader.read_buf(&mut readbuf)?; | ||
|
||
self.filled = readbuf.filled_len(); | ||
self.pos = 0; | ||
} | ||
Ok(self.buffer()) | ||
} | ||
} |
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
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.