Skip to content

Possible Task.WhenAll performance improvements #110530

Closed
@Alex-Sob

Description

@Alex-Sob

Let me share a couple of considerations related to Task.WhenAll performance, specifically for WhenAll<TResult>(IEnumerable<Task<TResult>> tasks) overload.

  1. Currently if tasks is not ICollection<T> and therefore the count is not known, it will create a list of tasks and then return new WhenAllPromise<TResult>(list.ToArray()). It seems like WhenAllPromise could take a Span<T> instead of an array, but currently it cannot because it stores that array into the field. Looks like the field is there only to be used in ShouldNotifyDebuggerOfWaitCompletion property and is probably needed only for debugging, can that allocation made by list.ToArray() be avoided in release builds?

  2. I believe that it's pretty common when a set of tasks is produced for some collection of items, e.g when calling source.Select(x => ProcessAsync(x)). We know collection size, but then we're losing it when calling Select. It probably would make sense to handle Iterator<T> internal type to get task count if it's available to avoid unnecessary allocations.

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