Skip to content
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 ValueTask-returning async FileStream methods wherever possible #51050

Closed
adamsitnik opened this issue Apr 10, 2021 · 5 comments
Closed

Use ValueTask-returning async FileStream methods wherever possible #51050

adamsitnik opened this issue Apr 10, 2021 · 5 comments
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Milestone

Comments

@adamsitnik
Copy link
Member

With #50802 and the next upcoming PR from @carlossanlop the following FileStream methods are going to become allocation-free:

ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default)
ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)

The "old", Task-returning overloads are still going to be allocating memory:

Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

The person who is willing to work on this task should:

  1. Search the entire runtime repo for places where the mentioned Task-returning methods are used. Please keep in mind that it's possible that we are already doing the right thing and you won't find any places for improvements.
  2. If you find places that can be improved, 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 (with a focus on the managed allocations).
  4. Contribute the improvements to dotnet/runtime and provide benchmark results

Note: some of the libraries are still targeting .NET Standard or even Full .NET Framework. For these libs, you might need to use conditional compilation like in the example below:

#if MS_IO_REDIST
await sw.WriteAsync(buffer, 0, batchSize).ConfigureAwait(false);
#else
await sw.WriteAsync(new ReadOnlyMemory<char>(buffer, 0, batchSize), cancellationToken).ConfigureAwait(false);
#endif

@adamsitnik adamsitnik added area-System.IO tenet-performance Performance related issue help wanted [up-for-grabs] Good issue for external contributors labels Apr 10, 2021
@adamsitnik adamsitnik added this to the 6.0.0 milestone Apr 10, 2021
@ghost
Copy link

ghost commented Apr 10, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

With #50802 and the next upcoming PR from @carlossanlop the following FileStream methods are going to become allocation-free:

ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default)
ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)

The "old", Task-returning overloads are still going to be allocating memory:

Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

The person who is willing to work on this task should:

  1. Search the entire runtime repo for places where the mentioned Task-returning methods are used. Please keep in mind that it's possible that we are already doing the right thing and you won't find any places for improvements.
  2. If you find places that can be improved, 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 (with a focus on the managed allocations).
  4. Contribute the improvements to dotnet/runtime and provide benchmark results

Note: some of the libraries are still targeting .NET Standard or even Full .NET Framework. For these libs, you might need to use conditional compilation like in the example below:

#if MS_IO_REDIST
await sw.WriteAsync(buffer, 0, batchSize).ConfigureAwait(false);
#else
await sw.WriteAsync(new ReadOnlyMemory<char>(buffer, 0, batchSize), cancellationToken).ConfigureAwait(false);
#endif

Author: adamsitnik
Assignees: -
Labels:

area-System.IO, tenet-performance, up-for-grabs

Milestone: 6.0.0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 10, 2021
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Apr 10, 2021
@stephentoub
Copy link
Member

stephentoub commented Apr 10, 2021

Search the entire runtime repo for places where the mentioned Task-returning methods are used. Please keep in mind that it's possible that we are already doing the right thing and you won't find any places for improvements.

This is unlikely to be very fruitful. I've previously made passes through to fix all such stream usage, and we now have an analyzer that flags such usage.

<Rule Id="CA1835" Action="Warning" /> <!-- Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' -->

@adamsitnik
Copy link
Member Author

This is unlikely to be very fruitful.

@stephentoub I agree, I just want someone to set it to Error and ensure that we are not doing it anywhere.

we now have an analyzer that flags such usage.

Thanks! We are going to mention the analyser in our FileStream perf best practices docs 👍

@stephentoub
Copy link
Member

stephentoub commented Apr 12, 2021

I just want someone to set it to Error

What does this mean? Do you mean the analyzer? We treat warnings as errors.

@adamsitnik
Copy link
Member Author

We treat warnings as errors.

I was not aware of that. In such a case, I am just going to close the issue. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

2 participants