Repository navigation
add Task<T>.CastUp and ValueTask<T>.CastUp - #136898
lagudafuadtosin wants to merge 3 commits into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-threading-tasks |
|
Are there any places in the libraries in this repo where this API can be used? |
| #endregion | ||
|
|
||
| /// <summary>Task that completes with the outcome of a <see cref="Task{TDerived}"/>.</summary> | ||
| private sealed class CastUpPromise<TDerived> : Task<TResult>, ITaskCompletionAction where TDerived : class?, TResult |
There was a problem hiding this comment.
With runtime-async, how is the performance comparing with simply async CastUp() => await task?
There was a problem hiding this comment.
good point. i measured it and the wrapper was faster when the task had already completed, so i added a fast path for that case. CastUp is now faster in both cases (16.5 vs 21.5 ns completed, 89 vs 157 ns pending).
yes, ZipArchiveEntry.Async.cs has CastToStreamAsync, an async helper that only exists to turn Task into Task. i've switched its 3 callers to Task.CastUp and removed it. |
| /// <param name="task">The task to cast.</param> | ||
| /// <returns>A <see cref="Task{TResult}"/> that completes when <paramref name="task"/> completes, with the same result, exceptions or cancellation. It may be the same instance as <paramref name="task"/>.</returns> | ||
| /// <exception cref="ArgumentNullException"><paramref name="task"/> is <see langword="null"/>.</exception> | ||
| public static Task<TResult> CastUp<TDerived>(Task<TDerived> task) where TDerived : class?, TResult |
There was a problem hiding this comment.
@VSadov @jakobbotsch Is this implementation the best we can do with runtime async?
fixes #89778
adds the approved api. Task.CastUp returns the task itself when it already is a Task, otherwise a task that completes with the same result, exceptions or cancellation. ValueTask.CastUp reuses its result or source and only wraps a task. tests added.