Let me share a couple of considerations related to Task.WhenAll performance, specifically for WhenAll<TResult>(IEnumerable<Task<TResult>> tasks) overload.
-
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?
-
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.
Let me share a couple of considerations related to
Task.WhenAllperformance, specifically forWhenAll<TResult>(IEnumerable<Task<TResult>> tasks)overload.Currently if
tasksis notICollection<T>and therefore the count is not known, it will create a list of tasks and then returnnew WhenAllPromise<TResult>(list.ToArray()). It seems likeWhenAllPromisecould take aSpan<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 inShouldNotifyDebuggerOfWaitCompletionproperty and is probably needed only for debugging, can that allocation made bylist.ToArray()be avoided in release builds?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 callingSelect. It probably would make sense to handleIterator<T>internal type to get task count if it's available to avoid unnecessary allocations.