Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Code improves
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed May 30, 2019
1 parent d8a4581 commit 450541b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VoidTaskResult> task) => new ValueTask(task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected

/// <summary>Schedules the state machine to proceed to the next action when the specified awaiter completes.</summary>
/// <typeparam name="TAwaiter">The type of the awaiter.</typeparam>
/// <typeparam name="TStateMachine">The type of the state machine.</typeparam>
Expand Down Expand Up @@ -158,17 +156,15 @@ public ValueTask<TResult> Task
{
get
{
if (ReferenceEquals(s_haveResultSentinel, m_task))
{
return new ValueTask<TResult>(_result);
}
else
{
return new ValueTask<TResult>(m_task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref m_task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected
}
return ReferenceEquals(s_haveResultSentinel, m_task) ?
new ValueTask<TResult>(_result) :
CreateValueTask(ref m_task);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ValueTask<TResult> CreateValueTask(ref Task<TResult> task) => new ValueTask<TResult>(task ?? AsyncMethodBuilderCore.InitializeTaskAsPromise(ref task!)); // TODO-NULLABLE: Remove ! when nullable attributes are respected

/// <summary>Schedules the state machine to proceed to the next action when the specified awaiter completes.</summary>
/// <typeparam name="TAwaiter">The type of the awaiter.</typeparam>
/// <typeparam name="TStateMachine">The type of the state machine.</typeparam>
Expand Down

0 comments on commit 450541b

Please sign in to comment.