From d888c56889a5d4b5f4a05b41366fd5d93001abd1 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 5 May 2019 18:45:38 +0100 Subject: [PATCH] Use same Task for Task.CompletedTask and ATMB.Completed --- .../Runtime/CompilerServices/AsyncMethodBuilder.cs | 9 +-------- .../shared/System/Threading/Tasks/Task.cs | 4 +++- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs index 9fcb418f8532..3949a5c5f1ab 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @@ -191,13 +191,6 @@ private void NotifySynchronizationContextOfCompletion() /// public struct AsyncTaskMethodBuilder { - /// A cached VoidTaskResult task used for builders that complete synchronously. -#if PROJECTN - private static readonly Task s_cachedCompleted = AsyncTaskCache.CreateCacheableTask(default(VoidTaskResult)); -#else - private static readonly Task s_cachedCompleted = AsyncTaskMethodBuilder.s_defaultResultTask; -#endif - /// The generic builder object to which this non-generic instance delegates. private AsyncTaskMethodBuilder m_builder; // mutable struct: must not be readonly. Debugger depends on the exact name of this field. @@ -270,7 +263,7 @@ public Task Task /// /// The builder is not initialized. /// The task has already completed. - public void SetResult() => m_builder.SetResult(s_cachedCompleted); // Using s_cachedCompleted is faster than using s_defaultResultTask. + public void SetResult() => m_builder.SetResult(Task.s_cachedCompleted); // Using s_cachedCompleted is faster than using s_defaultResultTask via generic. /// /// Completes the in the diff --git a/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs b/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs index ffe299bedb32..c1dba3bf39a5 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @@ -1487,8 +1487,10 @@ bool IAsyncResult.CompletedSynchronously /// public static TaskFactory Factory { get; } = new TaskFactory(); + // Is a Task{VoidTaskResult} so it can be shared with AsyncTaskMethodBuilder + internal static readonly Task s_cachedCompleted = new Task(false, default, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default); /// Gets a task that's already been completed successfully. - public static Task CompletedTask { get; } = new Task(false, (TaskCreationOptions)InternalTaskOptions.DoNotDispose, default); + public static Task CompletedTask => s_cachedCompleted; /// /// Provides an event that can be used to wait for completion.