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_onlyto theReadtrait. It returns true ifread_to_endandread_to_stringcan 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_endmethods:[u8]extends the input buffer to the full slice size all at once instead of doubling its size repeatedly until it's big enough.Filepre-allocates enough space for the full file size, allowing for fewerreadsyscalls and reducing buffer reallocations.These readers take advantage of it:
BufReaderwraps a[u8]orFileitsread_to_stringwill call their optimizedread_to_ends.Overall, this fixes a slow path where
BufReader::read_to_stringwould be forced to read into a side buffer when passed a non-empty input buffer.