Skip to content

Commit

Permalink
Merge pull request #490 from timcassell/optimize-configureawait
Browse files Browse the repository at this point in the history
Optimize contexts
  • Loading branch information
timcassell authored Nov 10, 2024
2 parents d716974 + 7fa7449 commit 3292c95
Show file tree
Hide file tree
Showing 35 changed files with 515 additions and 406 deletions.
4 changes: 2 additions & 2 deletions Package/Core/Linq/CompilerServices/AsyncStreamYielder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Proto.Promises.CompilerServices
#if !PROTO_PROMISE_DEVELOPER_MODE
[DebuggerNonUserCode, StackTraceHidden]
#endif
public readonly partial struct AsyncStreamYielder<T> : ICriticalNotifyCompletion, Internal.IPromiseAwaiter
public readonly partial struct AsyncStreamYielder<T> : ICriticalNotifyCompletion, Internal.IPromiseAwareAwaiter
{
private readonly Internal.PromiseRefBase.AsyncEnumerableWithIterator<T> _target;
private readonly int _enumerableId;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void GetResult()
=> _target.GetResultForAsyncStreamYielder(_enumerableId);

[MethodImpl(Internal.InlineOption)]
void Internal.IPromiseAwaiter.AwaitOnCompletedInternal(Internal.PromiseRefBase asyncPromiseRef)
void Internal.IPromiseAwareAwaiter.AwaitOnCompletedInternal(Internal.PromiseRefBase asyncPromiseRef)
=> _target.AwaitOnCompletedForAsyncStreamYielder(asyncPromiseRef, _enumerableId);

void INotifyCompletion.OnCompleted(Action continuation)
Expand Down
6 changes: 3 additions & 3 deletions Package/Core/Linq/Internal/AsyncEnumerableInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ internal override sealed void Handle(PromiseRefBase handler, Promise.State state
if (Interlocked.CompareExchange(ref _enumerableId, _iteratorCompleteId, _iteratorCompleteExpectedId) != _iteratorCompleteExpectedId)
{
handler.MaybeReportUnhandledAndDispose(state);
_rejectContainer = CreateRejectContainer(new InvalidOperationException("AsyncEnumerable.Create async iterator completed invalidly. Did you YieldAsync without await?"), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new InvalidOperationException("AsyncEnumerable.Create async iterator completed invalidly. Did you YieldAsync without await?"), int.MinValue, null, this);
state = Promise.State.Rejected;
}
else
{
_rejectContainer = handler._rejectContainer;
RejectContainer = handler.RejectContainer;
handler.SuppressRejection = true;
handler.MaybeDispose();
}
Expand All @@ -298,7 +298,7 @@ protected void HandleFromSynchronouslyCompletedIterator()
Promise.State state = Promise.State.Resolved;
if (Interlocked.CompareExchange(ref _enumerableId, _iteratorCompleteId, _iteratorCompleteExpectedId) != _iteratorCompleteExpectedId)
{
_rejectContainer = CreateRejectContainer(new InvalidOperationException("AsyncEnumerable.Create async iterator completed invalidly. Did you YieldAsync without await?"), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new InvalidOperationException("AsyncEnumerable.Create async iterator completed invalidly. Did you YieldAsync without await?"), int.MinValue, null, this);
state = Promise.State.Rejected;
}
HandleNextInternal(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ partial class PromiseRefBase
#if !PROTO_PROMISE_DEVELOPER_MODE
[DebuggerNonUserCode, StackTraceHidden]
#endif
internal readonly partial struct AsyncStreamAwaiterForLinqExtension<T> : ICriticalNotifyCompletion, IPromiseAwaiter
internal readonly partial struct AsyncStreamAwaiterForLinqExtension<T> : ICriticalNotifyCompletion, IPromiseAwareAwaiter
{
private readonly AsyncEnumerableWithIterator<T> _target;
private readonly int _enumerableId;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void GetResult()
}

[MethodImpl(InlineOption)]
void IPromiseAwaiter.AwaitOnCompletedInternal(PromiseRefBase asyncPromiseRef)
void IPromiseAwareAwaiter.AwaitOnCompletedInternal(PromiseRefBase asyncPromiseRef)
=> _target.AwaitOnCompletedForAsyncStreamYielder(asyncPromiseRef, _enumerableId, hasValue: false);

void INotifyCompletion.OnCompleted(Action continuation) => throw new System.InvalidOperationException("Must only be used in async Linq extension methods.");
Expand Down
2 changes: 1 addition & 1 deletion Package/Core/Linq/Internal/MergeInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state, int i
// The async enumerator was canceled or rejected, notify all enumerators that they don't need to continue executing.
CancelEnumerators();
}
_enumeratorsAndRejectContainers[index].rejectContainer = handler._rejectContainer;
_enumeratorsAndRejectContainers[index].rejectContainer = handler.RejectContainer;
_readyQueue.RemoveProducer();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ private Promise.State CompleteAndGetState()
_result[passthrough.Index] = owner.GetResult<T>();
if (owner.State == Promise.State.Rejected)
{
RecordException(owner._rejectContainer.GetValueAsException());
RecordException(owner.RejectContainer.GetValueAsException());
}
passthrough.Dispose();
}

if (_exceptions != null)
{
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
_exceptions = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private Promise.State CompleteAndGetState()
if (_exceptions != null)
{
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
_exceptions = null;
}

Expand All @@ -79,7 +79,7 @@ private Promise.State CompleteAndGetState()
{
var passthrough = passthroughs.Pop();
var owner = passthrough.Owner;
_result[passthrough.Index] = new Promise.ResultContainer(owner._rejectContainer, owner.State);
_result[passthrough.Index] = new Promise.ResultContainer(owner.RejectContainer, owner.State);
passthrough.Dispose();
}

Expand Down Expand Up @@ -153,7 +153,7 @@ private Promise.State CompleteAndGetState()
if (_exceptions != null)
{
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
_exceptions = null;
}

Expand All @@ -162,7 +162,7 @@ private Promise.State CompleteAndGetState()
{
var passthrough = passthroughs.Pop();
var owner = passthrough.Owner;
_result[passthrough.Index] = new Promise<T>.ResultContainer(owner.GetResult<T>(), owner._rejectContainer, owner.State);
_result[passthrough.Index] = new Promise<T>.ResultContainer(owner.GetResult<T>(), owner.RejectContainer, owner.State);
passthrough.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void MaybeHandleDisposeAsync()
return;
}

_rejectContainer = CreateRejectContainer(exception, int.MinValue, null, this);
RejectContainer = CreateRejectContainer(exception, int.MinValue, null, this);
HandleNextInternal(Promise.State.Rejected);
}

Expand Down
10 changes: 5 additions & 5 deletions Package/Core/PromiseGroups/Internal/PromiseMergeGroupInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
CancelGroup();
if (state == Promise.State.Rejected)
{
RecordException(handler._rejectContainer.GetValueAsException());
RecordException(handler.RejectContainer.GetValueAsException());
}
}
handler.MaybeDispose();
Expand All @@ -147,7 +147,7 @@ private Promise.State CompleteAndGetState()
return _completeState;
}

_rejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
_exceptions = null;
return Promise.State.Rejected;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
s_getResult.Invoke(owner, index, ref _result);
if (owner.State == Promise.State.Rejected)
{
var exception = owner._rejectContainer.GetValueAsException();
var exception = owner.RejectContainer.GetValueAsException();
if (_isExtended & index == 0)
{
// If this is an extended merge group, we need to extract the inner exceptions of the first cluster to make a flattened AggregateException.
Expand All @@ -253,13 +253,13 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
if (group._exceptions != null)
{
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(group._exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(group._exceptions), int.MinValue, null, this);
group._exceptions = null;
}
else
{
// The group may have been completed by a void promise, in which case it already converted its exceptions to a reject container.
_rejectContainer = handler._rejectContainer;
RejectContainer = handler.RejectContainer;
}
group.MaybeDispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
{
// If this is an extended merge group, we need to propagate the exceptions from cancelation token callbacks.
state = Promise.State.Rejected;
_rejectContainer = owner._rejectContainer;
RejectContainer = owner.RejectContainer;
}
passthrough.Dispose();
}
Expand All @@ -75,14 +75,14 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
{
// In case any cancelation token callbacks threw, we propagate them out of this promise instead of resolving this and ignoring the exceptions.
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(group._exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(group._exceptions), int.MinValue, null, this);
group._exceptions = null;
}
else if (handler._rejectContainer != null)
else if (handler.RejectContainer != null)
{
// The group may have been already completed, in which case it already converted its exceptions to a reject container.
state = Promise.State.Rejected;
_rejectContainer = handler._rejectContainer;
RejectContainer = handler.RejectContainer;
}
group.MaybeDispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private Promise.State CompleteAndGetState()
if ((state != Promise.State.Resolved | _cancelationThrew) & _exceptions != null)
{
state = Promise.State.Rejected;
_rejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
RejectContainer = CreateRejectContainer(new AggregateException(_exceptions), int.MinValue, null, this);
}
_exceptions = null;
return state;
Expand Down Expand Up @@ -143,7 +143,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state)
{
if (state == Promise.State.Rejected)
{
RecordException(handler._rejectContainer.GetValueAsException());
RecordException(handler.RejectContainer.GetValueAsException());
}
if (_cancelOnNonResolved)
{
Expand Down Expand Up @@ -203,7 +203,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state, int i
{
if (state == Promise.State.Rejected)
{
RecordException(handler._rejectContainer.GetValueAsException());
RecordException(handler.RejectContainer.GetValueAsException());
}
if (_cancelOnNonResolved)
{
Expand Down Expand Up @@ -263,7 +263,7 @@ internal override void Handle(PromiseRefBase handler, Promise.State state, int i
{
if (state == Promise.State.Rejected)
{
RecordException(handler._rejectContainer.GetValueAsException());
RecordException(handler.RejectContainer.GetValueAsException());
}
if (_cancelOnNonResolved)
{
Expand Down
4 changes: 2 additions & 2 deletions Package/Core/PromiseGroups/PromiseMergeResultsGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ private static class MergeResult<T>
[MethodImpl(Internal.InlineOption)]
private static void GetMergeResult(Internal.PromiseRefBase handler, int index, ref Promise<T>.ResultContainer result)
{
result = new Promise<T>.ResultContainer(handler.GetResult<T>(), handler._rejectContainer, handler.State);
result = new Promise<T>.ResultContainer(handler.GetResult<T>(), handler.RejectContainer, handler.State);
}

#if NETCOREAPP || UNITY_2021_2_OR_NEWER
Expand All @@ -1385,7 +1385,7 @@ private static class MergeResultVoid
[MethodImpl(Internal.InlineOption)]
private static void GetMergeResult(Internal.PromiseRefBase handler, int index, ref ResultContainer result)
{
result = new ResultContainer(handler._rejectContainer, handler.State);
result = new ResultContainer(handler.RejectContainer, handler.State);
}

#if NETCOREAPP || UNITY_2021_2_OR_NEWER
Expand Down
Loading

0 comments on commit 3292c95

Please sign in to comment.