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

Consider using ValueTask<T> instead of Task<T> #33804

Open
terrajobst opened this issue Mar 19, 2020 · 5 comments
Open

Consider using ValueTask<T> instead of Task<T> #33804

terrajobst opened this issue Mar 19, 2020 · 5 comments
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@terrajobst
Copy link
Member

Flag internal/private methods that returns Ts that won't be entirely cached (e.g. bool) and where every caller of the method only ever awaits its result directly.

Category: Performance

@terrajobst terrajobst added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks code-analyzer Marks an issue that suggests a Roslyn analyzer labels Mar 19, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Mar 19, 2020
@jeffhandley jeffhandley added this to the Future milestone Mar 20, 2020
@jeffhandley jeffhandley added the code-fixer Marks an issue that suggests a Roslyn code fixer label Mar 21, 2020
@jeffhandley
Copy link
Member

Estimates:

  • Analyzer: Large
  • Fixer: Large

@terrajobst terrajobst removed the untriaged New issue has not been triaged by the area owner label Mar 21, 2020
@weitzhandler
Copy link
Contributor

weitzhandler commented Apr 29, 2020

The problem with ValueTask is that it's hard to be used in generics since they don't implement a shared interface, so if you make a static class with some generics, you'll have to write out all types if one of the types can't be inferred.
See xunit/xunit#1919 and #25654.

@Joe4evr
Copy link
Contributor

Joe4evr commented Apr 29, 2020

@weitzhandler If the only thing you're concerned with is not having to write unnecessary type arguments, then dotnet/csharplang#1349 has you covered.

@carlossanlop
Copy link
Member

carlossanlop commented Jun 22, 2021

that won't be entirely cached (e.g. bool)

@terrajobst can you please expand on this? What do you mean by "entirely cached"?

every caller of the method only ever awaits its result directly

Do you mean the analyzer should only flag cases where the await keyword is next to the invocation itself?

For example, if you have:

private Task<MyType> MyMethodAsync() { ... }

Do you mean the analyzer should only suggest changing to ValueTask<T> when all the invocations of the method look like this?:

MyType myType = await MyMethodAsync();

But if the analyzer finds at least one invocation that looks like this, then we don't suggest changing to ValueTask<T>?:

Task<MyType> myTask = MyMethodAsync();
await myTask;

@stephentoub stephentoub added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 21, 2023
@bartonjs
Copy link
Member

bartonjs commented Jul 27, 2023

Video

Generally seems good as proposed. A few notes from the discussion

  • It should follow existing conventions for InternalsVisibleTo with internal members.
  • It should probably ignore virtual members in the initial implementation, then add them in later.
  • The general rule is "if any caller code would have to change, don't recommend this change"
  • It only applies to methods with the async keyword.

Category: Performance
Level: Info

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 27, 2023
@buyaa-n buyaa-n added the help wanted [up-for-grabs] Good issue for external contributors label Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

9 participants