Skip to content

Use FileStream buffering only when it makes sense #51048

Closed

Description

@adamsitnik

Whenever buffering is enabled, we need to allocate BufferedFileStreamStrategy

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):

and aquires lock for every ReadAsync and WriteAsync:

SemaphoreSlim semaphore = EnsureAsyncActiveSemaphoreInitialized();

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:

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:

  1. Search the entire runtime repo for places where buffering is enabled (bufferSize of the FileStream ctor is not set to 1 in an explicit way), but it hurts the performance like in the example above. If possible, please verify that FileStream used for async IO is created with FileOptions.Asynchronous or isAsync: true flag.
  2. 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.
  3. Run the benchmarks following the Benchmarking workflow for dotnet/runtime repository doc and ensure the changes are improving perf
  4. Contribute the improvements to dotnet/runtime and provide benchmark results

Activity

  1. added
    tenet-performance
    help wanted
    on Apr 10, 2021
  2. added this to the 6.0.0 milestone on Apr 10, 2021
  3. ghost added
    untriaged
    on Apr 10, 2021
  4. removed
    untriaged
    on Apr 10, 2021
  5. huoyaoyuan commented on Apr 10, 2021

    @huoyaoyuan
    Member
    1. If possible, please verify that FileStream used for async IO is created with FileOptions.Asynchronous or isAsync: true flag.

    Related to #45946 (comment) . Currently using isAsync forces the user to provide a buffer size, which doesn't have a help to determine. It's very difficult to learn that 1 is optimal now.

  6. adamsitnik commented on Apr 12, 2021

    @adamsitnik
    MemberAuthor

    It's very difficult to learn that 1 is optimal now.

    I acknowledge the fact that setting bufferSize to 1 is 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.

  7. sakno commented on May 7, 2021

    @sakno
    Contributor

    According to the current source code in main branch, this statement

    I acknowledge the fact that setting bufferSize to 1 is not intuitive and a trick known to very few engineers.

    is incorrect for Unix-like platforms. ChooseStrategy method always creates Net5CompatFileStreamStrategy without analysis of bufferSize argument as for Windows.

  8. stephentoub commented on May 23, 2021

    @stephentoub
    Member

    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.

  9. modified the milestones: 6.0.0, 7.0.0 on Jul 22, 2021
  10. modified the milestones: 7.0.0, Future on Aug 9, 2022
  11. added
    in-pr
    on Oct 11, 2025
  12. stephentoub commented on Oct 15, 2025

    @stephentoub
    Member

    All known cases have been addressed.

  13. locked and limited conversation to collaborators on Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.IOhelp wantedin-prtenet-performance

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions