Skip to content

Commit

Permalink
Use a partial extension method to strip out unnecessary state writes …
Browse files Browse the repository at this point in the history
…in Release mode.
  • Loading branch information
timcassell committed Aug 7, 2024
1 parent d445111 commit 6c56b6a
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 30 deletions.
11 changes: 11 additions & 0 deletions Package/Core/InternalShared/DebugInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,17 @@ partial void RemoveComplete(PromiseRefBase completePromise)

internal static string GetFormattedStacktrace(ITraceable traceable) => null;
#endif // PROMISE_DEBUG

// Dispose validates the state is not pending in Debug or Developer mode,
// so we only set the state for early dispose in those modes.
static partial void PrepareEarlyDispose(this PromiseRefBase promise);

#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
static partial void PrepareEarlyDispose(this PromiseRefBase promise)
{
promise.SetCompletionState(Promise.State.Resolved);
}
#endif
} // class Internal

partial struct Promise
Expand Down
4 changes: 1 addition & 3 deletions Package/Core/Linq/Generators/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ internal override Promise DisposeAsync(int id)

new private void Dispose()
{
#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
SetCompletionState(Promise.State.Resolved);
#endif
this.PrepareEarlyDispose();
base.Dispose();
_disposed = true;
ObjectPool.MaybeRepool(this);
Expand Down
4 changes: 1 addition & 3 deletions Package/Core/Linq/Generators/Repeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ internal override Promise DisposeAsync(int id)

new private void Dispose()
{
#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
SetCompletionState(Promise.State.Resolved);
#endif
this.PrepareEarlyDispose();
base.Dispose();
_current = default;
_disposed = true;
Expand Down
4 changes: 1 addition & 3 deletions Package/Core/Linq/Generators/Return.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ internal override Promise DisposeAsync(int id)

new private void Dispose()
{
#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
SetCompletionState(Promise.State.Resolved);
#endif
this.PrepareEarlyDispose();
base.Dispose();
_current = default;
_disposed = true;
Expand Down
11 changes: 5 additions & 6 deletions Package/Core/PromiseGroups/Internal/PromiseEachGroupInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ private void CancelGroup()
{
ValidateNoPending();

#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
SetCompletionState(Promise.State.Resolved);
#endif
// MoveNextAsync/DisposeAsync may have completed synchronously, in which case this will never have had a waiter added to it.
// So we need to mark it awaited to prevent the finalizer from reporting it as not awaited.
WasAwaitedOrForgotten = true;
base.Dispose();
_current = default;
_cancelationException = null;
Expand Down Expand Up @@ -150,6 +144,11 @@ internal override Promise DisposeAsync(int id)
}

var exception = GetAggregateException();

// MoveNextAsync may have completed synchronously, or not called at all, in which case this will never have had a waiter added to it or had its promise state set.
// So we need to mark it awaited to prevent the finalizer from reporting it as not awaited, and prepare the dispose.
this.PrepareEarlyDispose();
WasAwaitedOrForgotten = true;
Dispose();
return exception == null
? Promise.Resolved()
Expand Down
8 changes: 3 additions & 5 deletions Package/Core/Promises/Internal/EachInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ internal void AddPromise(PromiseRefBase promise, short id)
{
ValidateNoPending();

#if PROMISE_DEBUG || PROTO_PROMISE_DEVELOPER_MODE
SetCompletionState(Promise.State.Resolved);
#endif
// MoveNextAsync/DisposeAsync may have completed synchronously, in which case this will never have had a waiter added to it.
// So we need to mark it awaited to prevent the finalizer from reporting it as not awaited.
// MoveNextAsync/DisposeAsync may have completed synchronously, or not called at all, in which case this will never have had a waiter added to it or had its promise state set.
// So we need to mark it awaited to prevent the finalizer from reporting it as not awaited, and prepare the dispose.
this.PrepareEarlyDispose();
WasAwaitedOrForgotten = true;
base.Dispose();
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down
4 changes: 2 additions & 2 deletions Package/Core/Threading/Internal/AsyncLockInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down Expand Up @@ -156,7 +156,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down Expand Up @@ -115,7 +115,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down Expand Up @@ -178,7 +178,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down Expand Up @@ -241,7 +241,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down
2 changes: 1 addition & 1 deletion Package/Core/Threading/Internal/AsyncSemaphoreInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal override void MaybeDispose()
[MethodImpl(InlineOption)]
internal void DisposeImmediate()
{
SetCompletionState(Promise.State.Resolved);
this.PrepareEarlyDispose();
MaybeDispose();
}

Expand Down

0 comments on commit 6c56b6a

Please sign in to comment.