Skip to content

Task.WhenAny - avoid allocation when more than 2 tasks are provided #126748

Closed

Description

@unsafePtr

Description

When there are more than two Tasks passed to Task.WhenAny, an array is allocated as a protection measure.

// Make a defensive copy, as the user may manipulate the input collection
// after we return but before the WhenAny asynchronously completes.
TTask[] tasksCopy = tasks.ToArray();
foreach (TTask task in tasksCopy)

Would it be possible to use ArrayPool to eliminate this allocation in WhenAny? This could be beneficial in high-throughput async scenarios where WhenAny is called frequently, as it currently allocates a Task[] on every call with more than two tasks.

I've looked into the internals and it seems like this change could be straightforward to add — the rented array could be returned to the pool once the work is finished.

for (int i = 0; i < numTasks; i++)
{
TTask task = tasks[i];
if (task != null && // if an element was erroneously nulled out concurrently, just skip it; worst case is we don't remove a continuation
!task.IsCompleted) task.RemoveContinuation(this);
}
_tasks = null;

I'm happy to implement this change myself if the approach is considered feasible — would just appreciate approval before diving in. It may also be worth adding a dedicated fast path for exactly 3/4 tasks, similar to how 2 tasks is currently handled, to avoid the allocation entirely in that case as well.

Activity

  1. added
    tenet-performance
    on Apr 10, 2026
  2. added
    untriaged
    on Apr 10, 2026
  3. dotnet-policy-service commented on Apr 10, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-threading-tasks
    See info in area-owners.md if you want to be subscribed.

  4. jkotas commented on Apr 10, 2026

    @jkotas
    Member

    Have you measured the cost of this allocation vs. the total cost of the WhenAny API?

    ArrayPool is not free, it can hurt overall performance when used for small short-lived allocations, and comes with safety risks associated with manually managed lifetime.

  5. timcassell commented on Apr 13, 2026

    @timcassell

    When necessary to avoid async allocations, I'd suggest using another library besides TPL, like ProtoPromise or UniTask. https://github.com/timcassell/CSharpAsynchronousBenchmarks

  6. tannergooding commented on Apr 15, 2026

    @tannergooding
    Member

    As Jan calls out, we'd need perf/benchmark numbers for any change here. Ideally this would also showcase improvements to real world code.

    I'd expect that adding a special path for a common number of tasks (likely 1-4, which is what we optimize in various other places including for params) would be the better and safer approach here.

  7. unsafePtr commented on Apr 15, 2026

    @unsafePtr
    ContributorAuthor

    Hi all! I've been on holiday and picked this up this morning. Please find the preliminary results below and the basline is the current toolchain. @jkotas there is no performance degradation even on small counts of 3/5 items. @tannergooding , agree that common number of tasks can be expanded.

    
    BenchmarkDotNet v0.16.0-nightly.20260320.467, Windows 11 (10.0.26200.8037/25H2/2025Update/HudsonValley2)
    13th Gen Intel Core i7-13700KF 3.40GHz, 1 CPU, 24 logical and 16 physical cores
    Memory: 63.72 GB Total, 50.87 GB Available
    .NET SDK 11.0.100-preview.3.26170.106
      [Host]     : .NET 11.0.0 (11.0.0-preview.3.26170.106, 11.0.26.17106), X64 RyuJIT x86-64-v3
      Job-UHUKNN : .NET 11.0.0 (11.0.0-dev, 42.42.42.42424), X64 RyuJIT x86-64-v3
      Job-SVDRLY : .NET 11.0.0 (11.0.0-dev, 42.42.42.42424), X64 RyuJIT x86-64-v3
    
    
    
    Method Job Toolchain Count Mean Error StdDev Ratio Allocated Alloc Ratio
    AllCompleted Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 3 43.47 ns 0.327 ns 0.306 ns 1.00 136 B 1.00
    AllCompleted Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 3 43.01 ns 0.085 ns 0.075 ns 0.99 88 B 0.65
    AllPendingThenComplete Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 3 143.93 ns 1.225 ns 1.023 ns 1.00 584 B 1.00
    AllPendingThenComplete Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 3 139.44 ns 0.925 ns 0.865 ns 0.97 536 B 0.92
    AllCompleted Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 5 46.18 ns 0.249 ns 0.233 ns 1.00 152 B 1.00
    AllCompleted Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 5 44.58 ns 0.254 ns 0.225 ns 0.97 88 B 0.58
    AllPendingThenComplete Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 5 206.41 ns 1.231 ns 1.091 ns 1.00 888 B 1.00
    AllPendingThenComplete Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 5 203.33 ns 1.152 ns 1.078 ns 0.99 824 B 0.93
    AllCompleted Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 10 66.66 ns 0.296 ns 0.276 ns 1.00 192 B 1.00
    AllCompleted Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 10 46.63 ns 0.145 ns 0.136 ns 0.70 88 B 0.46
    AllPendingThenComplete Job-UHUKNN \runtime-baseline\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 10 377.16 ns 3.154 ns 2.950 ns 1.00 1648 B 1.00
    AllPendingThenComplete Job-SVDRLY \runtime\artifacts\bin\testhost\net11.0-windows-Release-x64\shared\Microsoft.NETCore.App\11.0.0\corerun.exe 10 370.77 ns 3.907 ns 3.655 ns 0.98 1544 B 0.94

    Benchmark

    [MemoryDiagnoser(false)]
    public class Perf_WhenAny
    {
        [Params(3, 5, 10)]
        public int Count;
    
        private Task[] _completed = default!;
    
        [GlobalSetup]
        public void Setup()
        {
            _completed = new Task[Count];
            for (int i = 0; i < Count; i++)
            {
                _completed[i] = Task.CompletedTask;
            }
        }
    
        [Benchmark]
        public Task AllCompleted() => WhenAnyNoInlining(_completed);
    
        [Benchmark]
        [MethodImpl(MethodImplOptions.NoInlining)]
        public object AllPendingThenComplete()
        {
            var sources = new TaskCompletionSource<int>[Count];
            var tasks = new Task<int>[Count];
            for (int i = 0; i < Count; i++)
            {
                sources[i] = new TaskCompletionSource<int>();
                tasks[i] = sources[i].Task;
            }
    
            object result = WhenAnyNoInlining(tasks);
            sources[0].SetResult(0);
            return result;
        }
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        public Task WhenAnyNoInlining(Task[] tasks) => Task.WhenAny(tasks);
    }

    Implementation

     internal sealed class PooledCompleteOnInvokePromise<TTask> : Task<TTask>, ITaskCompletionAction where TTask : Task
      {
          private TTask[]? _tasks;
          private readonly int _taskCount;
          private int _stateFlags;
    
          private const int CompletedFlag = 1;
    
          public PooledCompleteOnInvokePromise(TTask[] rentedTasks, int taskCount)
          {
              Debug.Assert(rentedTasks != null);
              Debug.Assert(taskCount > 0 && taskCount <= rentedTasks.Length);
              _tasks = rentedTasks;
              _taskCount = taskCount;
    
              if (TplEventSource.Log.IsEnabled())
                  TplEventSource.Log.TraceOperationBegin(this.Id, "Task.WhenAny", 0);
    
              if (s_asyncDebuggingEnabled)
                  AddToActiveTasks(this);
          }
    
          public void Invoke(Task completingTask)
          {
              if ((_stateFlags & CompletedFlag) == 0 &&
                  Interlocked.Exchange(ref _stateFlags, CompletedFlag) == 0)
              {
                  if (TplEventSource.Log.IsEnabled())
                  {
                      TplEventSource.Log.TraceOperationRelation(this.Id, CausalityRelation.Choice);
                      TplEventSource.Log.TraceOperationEnd(this.Id, AsyncCausalityStatus.Completed);
                  }
    
                  if (s_asyncDebuggingEnabled)
                      RemoveFromActiveTasks(this);
    
                  bool success = TrySetResult((TTask)completingTask);
                  Debug.Assert(success, "Only one task should have gotten to this point, and thus this must be successful.");
    
                  TTask[]? tasks = _tasks;
                  int numTasks = _taskCount;
                  Debug.Assert(tasks != null);
                  for (int i = 0; i < numTasks; i++)
                  {
                      TTask task = tasks[i];
                      if (task != null && !task.IsCompleted) task.RemoveContinuation(this);
                  }
                  _tasks = null;
    
                  ArrayPool<TTask>.Shared.Return(tasks, clearArray: true);
              }
          }
    
          public bool InvokeMayRunArbitraryCode => true;
      }
  8. jkotas commented on Apr 15, 2026

    @jkotas
    Member

    there is no performance degradation

    Microbenchmarks like this won't show the overall degradation caused by ArrayPool use.

  9. unsafePtr commented on Apr 21, 2026

    @unsafePtr
    ContributorAuthor

    Added fast path for 3/4 Tasks. #127216

    Follow-up - are there scenarios where callers with large, known-stable task collections (e.g., long-lived cached arrays in a hot path) could skip the defensive copy safely? Would you consider having public static Task<Task> WhenAny(Task[] tasks, bool copyTasks)?

  10. unsafePtr commented on Apr 21, 2026

    @unsafePtr
    ContributorAuthor

    Closing. 2-task is the most common pattern, and the maintenance cost of adding 3- and 4-task specializations is higher than the benefit - as discussed in #127216.

    Sourcegraph global regex search for direct 3-arg Task.WhenAny(a, b, c):
    https://sourcegraph.com/search?q=context:global+patterntype:regexp+Task%5C.WhenAny%5C%28%5B%5E%2C%28%29%5D%2B%2C%5B%5E%2C%28%29%5D%2B%2C%5B%5E%2C%28%29%5D%2B%5C%29&patternType=regexp

    Thank you for the feedback.

  11. removed
    untriaged
    on Apr 21, 2026
  12. locked and limited conversation to collaborators on May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions