-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Fix perf regression in Read::read_to_end on short reads due to not checking if the cursor has initialized bytes
#158083
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
Open
asder8215
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
asder8215:default_read_to_end_mark_init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−16
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| use core::cmp; | ||
| use core::mem::{DropGuard, MaybeUninit}; | ||
|
|
||
| use crate::io::{ | ||
|
|
@@ -840,6 +839,9 @@ pub fn default_read_to_end<R: Read + ?Sized>( | |
| .and_then(|s| s.checked_add(1024)?.checked_next_multiple_of(DEFAULT_BUF_SIZE)) | ||
| .unwrap_or(DEFAULT_BUF_SIZE); | ||
|
|
||
| // Tracks how many bytes are initialized in the buffer | ||
| let mut init_until = buf.len(); | ||
|
|
||
| const PROBE_SIZE: usize = 32; | ||
|
|
||
| fn small_probe_read<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> { | ||
|
|
@@ -887,30 +889,38 @@ pub fn default_read_to_end<R: Read + ?Sized>( | |
| } | ||
|
|
||
| loop { | ||
| if buf.len() == buf.capacity() && buf.capacity() == start_cap { | ||
| if buf.len() == buf.capacity() { | ||
| // The buffer might be an exact fit. Let's read into a probe buffer | ||
| // and see if it returns `Ok(0)`. If so, we've avoided an | ||
| // unnecessary doubling of the capacity. But if not, append the | ||
| // probe buffer to the primary buffer and let its capacity grow. | ||
| let read = small_probe_read(r, buf)?; | ||
|
|
||
| if read == 0 { | ||
| return Ok(buf.len() - start_len); | ||
| if buf.len() == start_cap { | ||
| let read = small_probe_read(r, buf)?; | ||
| if read == 0 { | ||
| return Ok(buf.len() - start_len); | ||
| } | ||
| } else { | ||
| // buf is full, need more space | ||
| buf.try_reserve(PROBE_SIZE)?; | ||
| } | ||
| init_until = buf.len(); | ||
| } | ||
|
|
||
| if buf.len() == buf.capacity() { | ||
| // buf is full, need more space | ||
| buf.try_reserve(PROBE_SIZE)?; | ||
| } | ||
| let (was_init, buf_len) = if init_until > buf.len() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still need |
||
| (true, init_until - buf.len()) | ||
| } else { | ||
| (false, usize::min(max_read_size, buf.capacity() - buf.len())) | ||
| }; | ||
|
|
||
| let mut spare = buf.spare_capacity_mut(); | ||
| let buf_len = cmp::min(spare.len(), max_read_size); | ||
| spare = &mut spare[..buf_len]; | ||
| let mut read_buf: BorrowedBuf<'_, u8> = spare.into(); | ||
|
|
||
| // Note that we don't track already initialized bytes here, but this is fine | ||
| // because we explicitly limit the read size | ||
| if was_init { | ||
| // SAFETY: These bytes were initialized but not filled in the previous loop | ||
| unsafe { read_buf.set_init() }; | ||
| } | ||
|
|
||
| let mut cursor = read_buf.unfilled(); | ||
| let result = loop { | ||
| match r.read_buf(cursor.reborrow()) { | ||
|
|
@@ -924,6 +934,10 @@ pub fn default_read_to_end<R: Read + ?Sized>( | |
| let bytes_read = cursor.written(); | ||
| let is_init = read_buf.is_init(); | ||
|
|
||
| if is_init { | ||
| init_until = buf.len() + buf_len; | ||
| } | ||
|
asder8215 marked this conversation as resolved.
|
||
|
|
||
| // SAFETY: BorrowedBuf's invariants mean this much memory is initialized. | ||
| unsafe { | ||
| let new_len = bytes_read + buf.len(); | ||
|
|
@@ -948,9 +962,11 @@ pub fn default_read_to_end<R: Read + ?Sized>( | |
| if !is_init { | ||
| max_read_size = usize::MAX; | ||
| } | ||
| // we have passed a larger buffer than previously and the | ||
| // reader still hasn't returned a short read | ||
| else if buf_len >= max_read_size && bytes_read == buf_len { | ||
| // the spare buffer has initialized and read in `max_read_size` bytes. | ||
| // it's possible that we have more than `max_read_size` bytes to read | ||
| // left, so a larger buffer may be necessary to minimize the number of | ||
| // iterations of reading in bytes to the buffer | ||
| else if bytes_read == max_read_size { | ||
| max_read_size = max_read_size.saturating_mul(2); | ||
| } | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept the exact fit check inside the
buf.len() == buf.capacity()conditional, so that we're not running through both conditional separately/repeatedly in the case thatbuf.len() != buf.capacity(). Hope that's fine.View changes since the review