System.Threading.Task is one of the de facto best practices for .net programming for async programming for all these years. C# 7.0 has added a cherry on top of it with Value Task for optimizing performance and abstraction.
To illustrate the benefit in performance, Let’s call an API to return the restaurant data for a particular city and cache the result. I have used caching here to show that there is a mix of synchronous and asynchronous code.
I ran the benchmark test with the Value Task and Task, Here is what the benefit.
As per the result, we are using half the memory of Task and in a high-performance environment that can a lifesaver.
Value Task being a struct helps avoid the creation of the object, also the internal state machine, when the code is executing the synchronous way.
One of the problems with using Task in the abstraction is we will force the implementation to use the asynchronous code where it may not be required.
For example, If we create an ICacheprovier interface with the async Get method, We will force the same to Implemented by MemoryCacheprovider too. With the advent of ValueTask, this can be avoided, and memory cache doesn’t need to cost of Task creation with this model.