From 3509323ad182f47ea19cc20f5cfd5735eb447720 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Thu, 28 Feb 2019 22:42:42 -0800 Subject: [PATCH] Remove extra await/state machine in FlushAsync (#7646) --- ...Microsoft.AspNetCore.Http.netcoreapp3.0.cs | 1 - .../Http/src/Microsoft.AspNetCore.Http.csproj | 1 + src/Http/Http/src/WriteOnlyPipeStream.cs | 18 ++++--------- .../src/Internal/Http/Http1OutputProducer.cs | 1 + .../Core/src/Internal/Http/HttpProtocol.cs | 1 + .../src/Internal/Http/ValueTaskExtensions.cs | 27 ------------------- .../src/Internal/Http2/Http2OutputProducer.cs | 1 + ...soft.AspNetCore.Server.Kestrel.Core.csproj | 1 + .../ValueTaskExtensions.cs | 0 ...soft.AspNetCore.SignalR.Client.Core.csproj | 5 +++- ...crosoft.AspNetCore.Http.Connections.csproj | 2 +- 11 files changed, 15 insertions(+), 43 deletions(-) delete mode 100644 src/Servers/Kestrel/Core/src/Internal/Http/ValueTaskExtensions.cs rename src/{SignalR/common/Shared => Shared/ValueTaskExtensions}/ValueTaskExtensions.cs (100%) diff --git a/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp3.0.cs b/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp3.0.cs index 2d80a27dff..2ff4bd700a 100644 --- a/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp3.0.cs +++ b/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp3.0.cs @@ -574,7 +574,6 @@ namespace System.IO.Pipelines public override System.IAsyncResult BeginWrite(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state) { throw null; } public override void EndWrite(System.IAsyncResult asyncResult) { } public override void Flush() { } - [System.Diagnostics.DebuggerStepThroughAttribute] public override System.Threading.Tasks.Task FlushAsync(System.Threading.CancellationToken cancellationToken) { throw null; } public override int Read(byte[] buffer, int offset, int count) { throw null; } public override System.Threading.Tasks.Task ReadAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken) { throw null; } diff --git a/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj b/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj index ffb49ec649..fd9d84712a 100644 --- a/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj +++ b/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj @@ -12,6 +12,7 @@ + diff --git a/src/Http/Http/src/WriteOnlyPipeStream.cs b/src/Http/Http/src/WriteOnlyPipeStream.cs index ada377b300..1d8d23328b 100644 --- a/src/Http/Http/src/WriteOnlyPipeStream.cs +++ b/src/Http/Http/src/WriteOnlyPipeStream.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.Internal; namespace System.IO.Pipelines { @@ -76,14 +77,14 @@ namespace System.IO.Pipelines { ThrowHelper.ThrowInvalidOperationException_SynchronousFlushesDisallowed(); } - + FlushAsync(default).GetAwaiter().GetResult(); } /// - public override async Task FlushAsync(CancellationToken cancellationToken) + public override Task FlushAsync(CancellationToken cancellationToken) { - await InnerPipeWriter.FlushAsync(cancellationToken); + return InnerPipeWriter.FlushAsync(cancellationToken).GetAsTask(); } /// @@ -162,16 +163,7 @@ namespace System.IO.Pipelines private Task WriteAsyncInternal(ReadOnlyMemory source, CancellationToken cancellationToken = default) { - var task = InnerPipeWriter.WriteAsync(source, cancellationToken); - - if (task.IsCompletedSuccessfully) - { - // Most ValueTask implementations reset in GetResult, so call it before returning completed task - task.GetAwaiter().GetResult(); - return Task.CompletedTask; - } - - return task.AsTask(); + return InnerPipeWriter.WriteAsync(source, cancellationToken).GetAsTask(); } } } diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs b/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs index 73c3125859..5ad99ccabe 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs @@ -8,6 +8,7 @@ using System.IO.Pipelines; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Features; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs b/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs index 724aa8514e..c575895a77 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs @@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/ValueTaskExtensions.cs b/src/Servers/Kestrel/Core/src/Internal/Http/ValueTaskExtensions.cs deleted file mode 100644 index a23b85030e..0000000000 --- a/src/Servers/Kestrel/Core/src/Internal/Http/ValueTaskExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.IO.Pipelines; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; - -namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http -{ - internal static class ValueTaskExtensions - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Task GetAsTask(this in ValueTask valueTask) - { - if (valueTask.IsCompletedSuccessfully) - { - // Signal consumption to the IValueTaskSource - valueTask.GetAwaiter().GetResult(); - return Task.CompletedTask; - } - else - { - return valueTask.AsTask(); - } - } - } -} diff --git a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs index d650645b85..9de710baf6 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs @@ -8,6 +8,7 @@ using System.IO.Pipelines; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.FlowControl; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; diff --git a/src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj b/src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj index 02e82bb23d..40589fab69 100644 --- a/src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj +++ b/src/Servers/Kestrel/Core/src/Microsoft.AspNetCore.Server.Kestrel.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/src/SignalR/common/Shared/ValueTaskExtensions.cs b/src/Shared/ValueTaskExtensions/ValueTaskExtensions.cs similarity index 100% rename from src/SignalR/common/Shared/ValueTaskExtensions.cs rename to src/Shared/ValueTaskExtensions/ValueTaskExtensions.cs diff --git a/src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj b/src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj index ca04b50a30..dbd15acce9 100644 --- a/src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj +++ b/src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj @@ -13,7 +13,10 @@ - + + + + diff --git a/src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj b/src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj index 130b334551..b0e35b336b 100644 --- a/src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj +++ b/src/SignalR/common/Http.Connections/src/Microsoft.AspNetCore.Http.Connections.csproj @@ -14,12 +14,12 @@ - +