From 3f5503e4abcb7692139b014e355bba6ad56b7c27 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 11 Jul 2019 20:28:47 -0400 Subject: [PATCH] Remove GetWaiterTask allocation for display class (dotnet/corefx#39411) Using an async local function here that accesses its outer function's state is resulting in the compiler allocating a display class at the beginning of GetWaiterTask. Commit migrated from https://github.com/dotnet/corefx/commit/59b60a232ecf3df2319fadd0193f5dcb42948e29 --- .../Net/Http/SocketsHttpHandler/Http2Stream.cs | 40 +++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs index 052ebe4..6be326d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs @@ -747,34 +747,34 @@ namespace System.Net.Http // if it is cancelable, then register for the cancellation callback, allocate a task for the asynchronously // completing case, etc. return cancellationToken.CanBeCanceled ? - new ValueTask(GetWaiterTaskCore()) : + new ValueTask(GetCancelableWaiterTask(cancellationToken)) : new ValueTask(this, _waitSource.Version); + } - async Task GetWaiterTaskCore() + private async Task GetCancelableWaiterTask(CancellationToken cancellationToken) + { + using (cancellationToken.UnsafeRegister(s => { - using (cancellationToken.UnsafeRegister(s => - { - var thisRef = (Http2Stream)s; - - bool signalWaiter; - lock (thisRef.SyncObject) - { - signalWaiter = thisRef._hasWaiter; - thisRef._hasWaiter = false; - } + var thisRef = (Http2Stream)s; - if (signalWaiter) - { - // Wake up the wait. It will then immediately check whether cancellation was requested and throw if it was. - thisRef._waitSource.SetResult(true); - } - }, this)) + bool signalWaiter; + lock (thisRef.SyncObject) { - await new ValueTask(this, _waitSource.Version).ConfigureAwait(false); + signalWaiter = thisRef._hasWaiter; + thisRef._hasWaiter = false; } - CancellationHelper.ThrowIfCancellationRequested(cancellationToken); + if (signalWaiter) + { + // Wake up the wait. It will then immediately check whether cancellation was requested and throw if it was. + thisRef._waitSource.SetResult(true); + } + }, this)) + { + await new ValueTask(this, _waitSource.Version).ConfigureAwait(false); } + + CancellationHelper.ThrowIfCancellationRequested(cancellationToken); } public void Trace(string message, [CallerMemberName] string memberName = null) => -- 2.7.4