Skip to content

Proposal: Add Task.WhenAny(Task,Task) overload to avoid Task[] allocation #23021

@clrjunkie

Description

@clrjunkie

public static Task WhenAny(Task task1, Task task2);

or

public static Task WhenEither(Task task1, Task task2);

The scenario is of multiple independent worker tasks observing one control task.
Currently, each worker needs to allocate an Array which appears wasteful.


EDITED: 2/25/2020 by @stephentoub to add proposal for review:

public class Task
{
    // existing
    public static Task<Task> WhenAny(params Task[] tasks);
    public static Task<Task> WhenAny(IEnumerable<Task> tasks);
    public static Task<Task<TResult>> WhenAny<TResult>(params Task<TResult>[] tasks);
    public static Task<Task<TResult>> WhenAny<TResult>(IEnumerable<Task<TResult>> tasks);

    // new
    public static Task<Task> WhenAny(Task task1, Task task2);
    public static Task<Task<TResult>> WhenAny<TResult>(Task<TResult> task1, Task<TResult> task2);
}

This will end up improving existing call sites that do Task.WhenAny(t1, t2), as they'll now bind to the new overload rather than to the params array overload. The new overload can avoid both the external and internal array allocations, and have a dedicated implementation specialized to the very common case of just two tasks (I looked at over a hundred call sites in various code bases, and all but one call site used just two tasks as input).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions