You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead, the tasks should be interleaved so that tasks are returned in their order of completion, using this task combinator. It is a small change that dramatically improves performance by minimizing idle CPU time between incomplete requests.
staticIEnumerable<Task<T>>Interleaved<T>(IEnumerable<Task<T>>tasks){varinputTasks=tasks.ToList();varsources=(from_inEnumerable.Range(0,inputTasks.Count)
select new TaskCompletionSource<T>()).ToList();intnextTaskIndex=-1;foreach(varinputTaskin inputTasks){
inputTask.ContinueWith(completed =>{varsource=sources[Interlocked.Increment(refnextTaskIndex)];if(completed.IsFaulted)source.TrySetException(completed.Exception.InnerExceptions);elseif(completed.IsCanceled)source.TrySetCanceled();elsesource.TrySetResult(completed.Result);},CancellationToken.None,TaskContinuationOptions.ExecuteSynchronously,TaskScheduler.Default);}returnfrom source in sources
select source.Task;}
There is also a synchronous method named FindAllPages that returns a collection of Lazy<TResult> instead of Task<TResult>. Because it is synchronous, I don't think that it can be interleaved in this way. Any method that interleaves Lazy objects would probably still use TaskCompletionSource under the hood, defeating the purpose of synchronous methods. One more reason to drop synchronous API calls.
The text was updated successfully, but these errors were encountered:
The
FindAllPagesAsync
extension method returns a collection of tasks that complete in order that they were started.GW2.NET/src/GW2NET.Core/Common/Paginator.cs
Line 119 in 1fd0356
Instead, the tasks should be interleaved so that tasks are returned in their order of completion, using this task combinator. It is a small change that dramatically improves performance by minimizing idle CPU time between incomplete requests.
https://msdn.microsoft.com/en-us/library/hh873173(v=vs.110).aspx
There is also a synchronous method named
FindAllPages
that returns a collection ofLazy<TResult>
instead ofTask<TResult>
. Because it is synchronous, I don't think that it can be interleaved in this way. Any method that interleavesLazy
objects would probably still useTaskCompletionSource
under the hood, defeating the purpose of synchronous methods. One more reason to drop synchronous API calls.The text was updated successfully, but these errors were encountered: