-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Update documentation for std::fs::read_to_string to reflect performan… #130610
Conversation
…ce improvements on Windows
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @workingjubilee (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Considering the buffers use exponential growing the allocation behavior should hardly make a difference. I don't think it's worth pointing this out in the documentation (guaranteeing any implementation details) unless the advantage is tremendous. Do you have any numbers that show that it's worth steering users in this direction and tying our hands regarding future implementation changes? See also #130600 (comment) |
? Er, do they? If you use |
It's O(1) vs. O(log(n)) allocations, which sometimes makes a difference, but when you're doing IO then I'd expect the IO overhead to dwarf the allocation overhead. The changes were motivated not by allocation behavior but by repeated initialization costs in the windows kernel (#110650). I could be wrong. Perhaps the default allocator on windows is slow enough that it's noticeable. That's why I asked for numbers |
I see... as you say, it doesn't seem like the performance difference is as significant as I initially thought, especially since IO tends to be the bottleneck in such cases. I raised this issue just because I was surprised by the source code and the issue #110650 when I searched about |
@ducktherapy (who raised issue #110650) had posted a related question on reddit and StackOverflow. |
That was before the fix. |
/// This is a convenience function for using [`File::open`] and [`read_to_string`] | ||
/// with fewer imports and without an intermediate variable. | ||
/// with fewer imports, without an intermediate variable, and with improved performance | ||
/// by referencing the file's metadata to preallocate buffer size. |
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.
I'm still game to accept this but it's a "should have better performance" or "may be faster and more efficient" rather than "is definitely going to have better performance for (specific implementation detail)" because of OS-specific differences.
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.
To clarify, which part are you referring to by saying "may be faster and more efficient"?
The only optimization that fs::read_to_string has is that it can use the size from the file metadata directly and doesn't need to retrieve the current seek position of the file handle.
@rustbot author |
@fiveseven-lambda |
I intended to close the PR but forgot to do so. I’ll go ahead and close it now. I am sorry for any inconvenience caused. |
…ce improvements on Windows
Closes #130600.
This pull request updates the documentation for
std::fs::read_to_string
to clarify its performance advantages on Windows compared to the combination ofstd::fs::File::open
andstd::io::Read::read_to_string
.In PR #110655, optimizations were introduced to improve the performance of
std::fs::read_to_string
on Windows. However, the current documentation does not reflect these improvements, potentially misleading users into thinking that usingFile::open
andRead::read_to_string
would have the same performance characteristics.The updated documentation now explicitly mentions that
std::fs::read_to_string
offers better performance on Windows than manually opening the file and usingstd::io::Read::read_to_string
.r? workingjubilee