-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
ADO.NET APIs force needless Task allocations #24067
Comments
As they are private readonly static Task<bool> TrueTask = Task.FromResult(true);
private readonly static Task<bool> FalseTask = Task.FromResult(false);
public overrides Task<bool> ReadAsync(CancellationToken cancellationToken)
{
if (!NeedsMoreData)
{
return TryMoveNextRow() ? TrueTask : FalseTask;
}
else
{
return ReadAsyncAwaited(cancellationToken);
}
}
private async Task<bool> ReadAsyncAwaited(CancellationToken cancellationToken)
{
await ReadMoreData(cancellationToken);
return TryMoveNextRow();
} |
@benaadams of course, silly of me to have forgotten this. In fact, unless I'm mistaken an async method that returns |
You'll still pay the cost of the |
Yep, Npgsql has this pattern in some performance-critical paths (if data is cached return that, otherwise call an async method to load). |
The ADO.NET API added async methods (e.g.
DbDataReader.ReadAsync()
,DbDataReader.NextResultAsync()
) before ValueTask existed. Unfortunately this means that these methods return Task instances, which are heap-allocated, although in many (most?) cases the ADO.NET driver will already have the next row or resultset cached in its internal buffer (this is the case for Npgsql).Ideally new API methods would be introduced, which would be identical in every way except that they return a ValueTask rather than a Task (how these new methods would be named is a depressing question...). Note that the above two examples may not be the only ones that could benefit from this.
The text was updated successfully, but these errors were encountered: