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
With the current implementation, the ValidateOptimized method is called once for every type used as a type parameter. I suspect it only needs to be called once.
Moving most of the static members out of the ValueTaskCompletionSource struct would allow this:
internalstaticclassTaskHelper{
#if NETSTANDARD1_3publicstaticreadonlyboolUseOptimizedPath=false;
#else
privatestaticclassTaskInternals<T>{publicstaticreadonlyFunc<Task<T>,T,bool>TrySetResult=TryCreate<T>(nameof(TrySetResult));publicstaticreadonlyFunc<Task<T>,Exception,bool>TrySetException=TryCreate<Exception>(nameof(TrySetException));publicstaticreadonlyFunc<Task<T>,CancellationToken,bool>TrySetCanceled=TryCreate<CancellationToken>(nameof(TrySetCanceled));[MethodImpl(MethodImplOptions.NoInlining)]privatestaticFunc<Task<T>,TArg,bool>TryCreate<TArg>(stringmethodName){try{varmethod=typeof(Task<T>).GetMethod(methodName,BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance,null,new[]{typeof(TArg)},null);returnmethodisnull?null:(Func<Task<T>,TArg,bool>)Delegate.CreateDelegate(typeof(Func<Task<T>,TArg,bool>),method);}catch{returnnull;}}}publicstaticreadonlyboolUseOptimizedPath=ValidateOptimized();[MethodImpl(MethodImplOptions.NoInlining)]privatestaticboolValidateOptimized(){try{if(TaskInternals<object>.TrySetResultisnull)returnfalse;if(TaskInternals<object>.TrySetExceptionisnull)returnfalse;if(TaskInternals<object>.TrySetCanceledisnull)returnfalse;vartask=CreateUninitializedTask<object>();if(taskisnull)returnfalse;if(task.IsCompleted)returnfalse;if(!TaskInternals<object>.TrySetResult(task,default))returnfalse;if(task.Status!=TaskStatus.RanToCompletion)returnfalse;task=CreateUninitializedTask<object>();if(!TaskInternals<object>.TrySetException(task,newInvalidOperationException()))returnfalse;if(!task.IsCompleted)returnfalse;if(!task.IsFaulted)returnfalse;try{_=task.Result;returnfalse;}catch(AggregateExceptionex)when(ex.InnerExceptionisInvalidOperationException){}returntask.Exception?.InnerExceptionisInvalidOperationException;}catch{returnfalse;}}[MethodImpl(MethodImplOptions.NoInlining)]privatestaticvoidSpinUntilCompleted([NotNull]Tasktask){// Spin wait until the completion is finalized by another thread.varsw=newSpinWait();while(!task.IsCompleted){sw.SpinOnce();}}[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticTask<T>CreateUninitializedTask<T>()=>(Task<T>)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Task<T>));[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticboolTrySetResult<T>(thisTask<T>task,Tvalue){boolresult=TaskInternals<T>.TrySetResult(task,value);if(!result&&!task.IsCompleted)SpinUntilCompleted(task);returnresult;}[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticboolTrySetException<T>(thisTask<T>task,Exceptionerror){boolresult=TaskInternals<T>.TrySetException(task,error);if(!result&&!task.IsCompleted)SpinUntilCompleted(task);returnresult;}[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticboolTrySetCanceled<T>(thisTask<T>task,CancellationTokencancellationToken){boolresult=TaskInternals<T>.TrySetCanceled(task,cancellationToken);if(!result&&!task.IsCompleted)SpinUntilCompleted(task);returnresult;}
#endif
}
The text was updated successfully, but these errors were encountered:
With the current implementation, the
ValidateOptimized
method is called once for every type used as a type parameter. I suspect it only needs to be called once.Moving most of the static members out of the
ValueTaskCompletionSource
struct would allow this:The text was updated successfully, but these errors were encountered: