Repository navigation
Description
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
| internal static FileStreamStrategy EnableBufferingIfNeeded(WindowsFileStreamStrategy strategy, int bufferSize) | |
| => bufferSize == 1 ? strategy : new BufferedFileStreamStrategy(strategy, bufferSize); |
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
| ~BufferedFileStreamStrategy() |
and aquires lock for every ReadAsync and WriteAsync:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Line 346 in 0af5228
| SemaphoreSlim semaphore = EnsureAsyncActiveSemaphoreInitialized(); |
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Line 641 in 0af5228
| SemaphoreSlim semaphore = EnsureAsyncActiveSemaphoreInitialized(); |
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 a FileStream with buffering enabled:
runtime/src/libraries/System.IO.FileSystem/src/System/IO/File.cs
Lines 678 to 680 in 0af5228
| FileStream stream = new FileStream( | |
| path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, DefaultBufferSize, | |
| FileOptions.Asynchronous | FileOptions.SequentialScan); |
but later use buffers that are at least as big as the internal FileStream buffer (so we never take advantage of buffering)
| buffer = ArrayPool<char>.Shared.Rent(DefaultBufferSize); |
to write to the file:
| await sw.WriteAsync(new ReadOnlyMemory<char>(buffer, 0, batchSize), cancellationToken).ConfigureAwait(false); |
The person who is willing to work on this task should:
- Search the entire runtime repo for places where buffering is enabled (
bufferSizeof theFileStreamctor is not set to1in an explicit way), but it hurts the performance like in the example above. If possible, please verify thatFileStreamused for async IO is created withFileOptions.AsynchronousorisAsync: trueflag. - Contribute benchmarks to the dotnet/performance repo after reading the Microbenchmark Design Guidelines doc. The benchmarks need to cover the code that is about to be changed.
- Run the benchmarks following the Benchmarking workflow for dotnet/runtime repository doc and ensure the changes are improving perf
- Contribute the improvements to dotnet/runtime and provide benchmark results
Activity
huoyaoyuan commentedon Apr 10, 2021
Related to #45946 (comment) . Currently using
isAsyncforces the user to provide a buffer size, which doesn't have a help to determine. It's very difficult to learn that1is optimal now.adamsitnik commentedon Apr 12, 2021
I acknowledge the fact that setting
bufferSizeto1is not intuitive and a trick known to very few engineers.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.
sakno commentedon May 7, 2021
According to the current source code in
mainbranch, this statementis incorrect for Unix-like platforms.
ChooseStrategymethod always createsNet5CompatFileStreamStrategywithout analysis ofbufferSizeargument as for Windows.stephentoub commentedon May 23, 2021
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.
stephentoub commentedon Oct 15, 2025
All known cases have been addressed.