-
Notifications
You must be signed in to change notification settings - Fork 4.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
Use FileStream buffering only when it makes sense #51048
Comments
Tagging subscribers to this area: @carlossanlop Issue DetailsWhenever buffering is enabled, we need to allocate runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs Lines 51 to 52 in 0af5228
which has a finalizer (that is an additional work for the GC): runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs Line 32 in 0af5228
and aquires lock for every runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs Line 346 in 0af5228
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs Line 641 in 0af5228
this is of course done for some good reasons, but the thing is that we don't always need the buffering. A good example is runtime/src/libraries/System.IO.FileSystem/src/System/IO/File.cs Lines 678 to 680 in 0af5228
but later use buffers that are at least as big as the internal
to write to the file:
The person who is willing to work on this task should:
|
Related to #45946 (comment) . Currently using |
I acknowledge the fact that setting Part of our .NET 6 FileStream work is to publish a doc about performance best practices. We are going to explain what buffering is, how it's different from the buffering done by the OS and how to disable it. |
According to the current source code in
is incorrect for Unix-like platforms. |
It's not incorrect. The Net5CompatFileStreamStrategy lazily creates the buffer only if it's needed. With a _bufferSize of 1, any non-empty user buffer will result in skipping the buffer and it won't be lazily created. This is the case for both Windows and Unix in .NET 5, and has been for a long time. |
Whenever buffering is enabled, we need to allocate
BufferedFileStreamStrategy
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs
Lines 51 to 52 in 0af5228
which has a finalizer (that is an additional work for the GC):
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Line 32 in 0af5228
and aquires lock for every
ReadAsync
andWriteAsync
:runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Line 346 in 0af5228
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Line 641 in 0af5228
this is of course done for some good reasons, but the thing is that we don't always need the buffering.
A good example is
File.WriteAllTextAsync
where we create aFileStream
with buffering enabled:runtime/src/libraries/System.IO.FileSystem/src/System/IO/File.cs
Lines 678 to 680 in 0af5228
but later use buffers that are at least as big as the internal
FileStream
buffer (so we never take advantage of buffering)runtime/src/libraries/System.IO.FileSystem/src/System/IO/File.cs
Line 979 in 0af5228
to write to the file:
runtime/src/libraries/System.IO.FileSystem/src/System/IO/File.cs
Line 989 in 0af5228
The person who is willing to work on this task should:
bufferSize
of theFileStream
ctor is not set to1
in an explicit way), but it hurts the performance like in the example above. If possible, please verify thatFileStream
used for async IO is created withFileOptions.Asynchronous
orisAsync: true
flag.The text was updated successfully, but these errors were encountered: