Skip to content

Fast-path with ValueTask slower than Task #56849

Answered by stephentoub
cincuranet asked this question in General
Discussion options

You must be logged in to vote

Allocation and gen0 collection is relatively cheap, so the allocations occurring for the tasks here is likely not significantly negatively impacting those throughput numbers. The impact of allocation ends up being higher when there's more going on than in a microbenchmark. For a serialized benchmark, I expect Task<T> to be faster. A ValueTask<T> is a discriminated union of a Task<T> and a T, which means it has fields for both, which means copying one is more expensive than copying a Task<T> (that also means a ValueTask<T> takes up more space in a state machine for an async method for which it's awaited). A reference to a Task<T> could be passed back in a register, but a ValueTask<decimal>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cincuranet
Comment options

cincuranet Aug 5, 2021
Collaborator Author

Answer selected by cincuranet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants