From 450541bbd97095e162b0d88b50ba3c53bcd0a3b4 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 30 May 2019 15:55:07 +0100 Subject: [PATCH] Code improves --- .../AsyncIteratorMethodBuilder.cs | 6 ++-- .../AsyncValueTaskMethodBuilder.cs | 28 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncIteratorMethodBuilder.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncIteratorMethodBuilder.cs index 42df1078e71f..fee9d40cdfcf 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncIteratorMethodBuilder.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncIteratorMethodBuilder.cs @@ -13,10 +13,10 @@ namespace System.Runtime.CompilerServices public struct AsyncIteratorMethodBuilder { // AsyncIteratorMethodBuilder is used by the language compiler as part of generating - // async iterators. For now, the implementation just wraps AsyncTaskMethodBuilder, as - // most of the logic is shared. However, in the future this could be changed and + // async iterators. For now, the implementation just forwards to AsyncMethodBuilderCore, + // as most of the logic is shared. However, in the future this could be changed and // optimized. For example, we do need to allocate an object (once) to flow state like - // ExecutionContext, which AsyncTaskMethodBuilder handles, but it handles it by + // ExecutionContext, which AsyncMethodBuilderCore handles, but it handles it by // allocating a Task-derived object. We could optimize this further by removing // the Task from the hierarchy, but in doing so we'd also lose a variety of optimizations // related to it, so we'd need to replicate all of those optimizations (e.g. storing diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs index 6fe1dab75d5d..065bce7ec94e 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs @@ -63,17 +63,15 @@ public ValueTask Task { get { - if (ReferenceEquals(m_task, System.Threading.Tasks.Task.s_cachedCompleted)) - { - return default; - } - else - { - return new ValueTask(m_task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref m_task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected - } + return ReferenceEquals(m_task, System.Threading.Tasks.Task.s_cachedCompleted) ? + default : + CreateValueTask(ref m_task); } } + [MethodImpl(MethodImplOptions.NoInlining)] + private static ValueTask CreateValueTask(ref Task task) => new ValueTask(task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected + /// Schedules the state machine to proceed to the next action when the specified awaiter completes. /// The type of the awaiter. /// The type of the state machine. @@ -158,17 +156,15 @@ public ValueTask Task { get { - if (ReferenceEquals(s_haveResultSentinel, m_task)) - { - return new ValueTask(_result); - } - else - { - return new ValueTask(m_task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref m_task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected - } + return ReferenceEquals(s_haveResultSentinel, m_task) ? + new ValueTask(_result) : + CreateValueTask(ref m_task); } } + [MethodImpl(MethodImplOptions.NoInlining)] + private static ValueTask CreateValueTask(ref Task task) => new ValueTask(task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected + /// Schedules the state machine to proceed to the next action when the specified awaiter completes. /// The type of the awaiter. /// The type of the state machine.