-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
File.*AllText* optimizations #58167
File.*AllText* optimizations #58167
Conversation
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsThe reduced memory allocations come from lack of The reduced CPU time comes mostly from using bigger buffers and setting file preallocation size.
|
@jozkee please let me know if you would like me to separate the refactor from the optimizations |
@@ -890,14 +650,10 @@ public static Task WriteAllLinesAsync(string path, IEnumerable<string> contents, | |||
|
|||
public static Task WriteAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken)) | |||
{ | |||
if (path == null) | |||
throw new ArgumentNullException(nameof(path)); | |||
Validate(path, encoding); |
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.
It's probably fine, but this does change which exception will be thrown if there are multiple things wrong.
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
I assume no new tests are warranted as it's purely perf, but having touched this code can you see any we maybe are missing? |
…need to store the preamble
…nefit, as it requires additional sys-call(s)
Co-authored-by: Stephen Toub <stoub@microsoft.com>
@stephentoub @danmoseley I've added missing tests, which identified a bug, fixed it and updated perf numbers to include Linux numbers as well. On Linux it's now even two times faster for large strings! |
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/File.netcoreapp.cs
Outdated
Show resolved
Hide resolved
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.
Otherwise; LGTM, Thanks.
src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllText.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllText.cs
Outdated
Show resolved
Hide resolved
I am closing the PR as it's pointless with the regression that has been recently introduced and backported to 6.0 (#59705) |
You'd made a bunch of style changes, consolidating input validation, etc. Do you want to keep those? |
It was, because I wanted to clarify how |
# Conflicts: # src/libraries/System.Private.CoreLib/src/System/IO/File.cs
Co-authored-by: David Cantú <dacantu@microsoft.com>
I was able to get some nice perf wins anyway (mostly due to using larger buffers which reduced the number of sys-calls). The failures are not related ( |
Nice! |
Ubuntu x64 improvements - dotnet/perf-autofiling-issues#1897 |
alpine improvements - dotnet/perf-autofiling-issues#1922 |
windows x64 improvements - dotnet/perf-autofiling-issues#1933 |
woo hoo. also, @kunalspathak @adamsitnik what happened here? it's not obvious to me that this change improved File.Exists -- |
A curious bimodality that seems to stay in one state for a week or more. I wonder whether iterations within an individual run are stable? I don't see a way to determine from the report. |
Improvement dotnet/perf-autofiling-issues#1795 (click on full history to see) |
It's interesting that it got both faster and more stable despite still being an IO benchmark. I suspect that it might be caused by using the preallocation size which avoids fragmentation. |
That is a beautiful graph. Nice. |
Wow; it really is! Great work, and I love seeing the data loop closed like this, attributing the improvements to the change. |
The reduced memory allocations come from lack of
StreamWriter
andFileStream
buffer allocations.The reduced CPU time comes mostly from using bigger buffers and setting file preallocation size.
Windows
Ubuntu