lidavidm commented on PR #1865: URL: https://github.com/apache/arrow-adbc/pull/1865#issuecomment-2111527407
> A method may return an instance of this value type when it's likely that the result of its operation will be available synchronously, and when it's expected to be invoked so frequently that the cost of allocating a new [Task<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1?view=net-8.0) for each call will be prohibitive. > > There are tradeoffs to using a [ValueTask<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=net-8.0) instead of a [Task<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1?view=net-8.0). For example, while a [ValueTask<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=net-8.0) can help avoid an allocation in the case where the successful result is available synchronously, it also contains multiple fields, whereas a [Task<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1?view=net-8.0) as a reference type is a single field. This means that returning a [ValueTask<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=net-8.0) from a method results in copying more data. It also means, that if a method that returns a [ValueTask<TResult>](https://learn.microsoft.com/en-us/dotnet/api/system. threading.tasks.valuetask-1?view=net-8.0) is awaited within an async method, the state machine for that async method will be larger, because it must store a struct containing multiple fields instead of a single reference. Wow, C#/.NET has thought this all through, pretty neat. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
