Add Read::is_append_only method to optimize read_to_string #89711
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.
I've added a new method called
is_append_only
to theRead
trait. It returns true ifread_to_end
andread_to_string
can be trusted not to read or overwrite any existing data in their input buffer.These readers use it to signal that they have optimized
read_to_end
methods:[u8]
extends the input buffer to the full slice size all at once instead of doubling its size repeatedly until it's big enough.File
pre-allocates enough space for the full file size, allowing for fewerread
syscalls and reducing buffer reallocations.These readers take advantage of it:
BufReader
wraps a[u8]
orFile
itsread_to_string
will call their optimizedread_to_end
s.Overall, this fixes a slow path where
BufReader::read_to_string
would be forced to read into a side buffer when passed a non-empty input buffer.