From cf77efc2ff82c40233b0737d38751171db187e43 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 23 Dec 2015 10:20:25 +0000 Subject: [PATCH] Fast path pre-completed Input reads --- .../Http/SocketInput.cs | 14 +++++++------- .../Http/SocketInputExtensions.cs | 12 ++++-------- .../Infrastructure/TaskUtilities.cs | 1 + 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInput.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInput.cs index 3cdbba59c8..efe431f943 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInput.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInput.cs @@ -116,13 +116,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http Complete(); } - public void AbortAwaiting() - { - _awaitableError = new ObjectDisposedException(nameof(SocketInput), "The request was aborted"); - - Complete(); - } - private void Complete() { var awaitableState = Interlocked.Exchange( @@ -177,6 +170,13 @@ namespace Microsoft.AspNet.Server.Kestrel.Http } } + public void AbortAwaiting() + { + _awaitableError = new ObjectDisposedException(nameof(SocketInput), "The request was aborted"); + + Complete(); + } + public SocketInput GetAwaiter() { return this; diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInputExtensions.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInputExtensions.cs index 39cb3202a0..d514c3893f 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInputExtensions.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketInputExtensions.cs @@ -1,8 +1,8 @@ // 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; using System.Threading.Tasks; +using Microsoft.AspNet.Server.Kestrel.Infrastructure; namespace Microsoft.AspNet.Server.Kestrel.Http { @@ -10,15 +10,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http { public static ValueTask ReadAsync(this SocketInput input, byte[] buffer, int offset, int count) { - while (true) + while (input.IsCompleted) { - if (!input.IsCompleted) - { - return input.ReadAsyncAwaited(buffer, offset, count); - } - var begin = input.ConsumingStart(); - int actual; var end = begin.CopyTo(buffer, offset, count, out actual); input.ConsumingComplete(end, end); @@ -32,6 +26,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http return 0; } } + + return input.ReadAsyncAwaited(buffer, offset, count); } private static async Task ReadAsyncAwaited(this SocketInput input, byte[] buffer, int offset, int count) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/TaskUtilities.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/TaskUtilities.cs index e67ded5acb..6a56e7507a 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/TaskUtilities.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/TaskUtilities.cs @@ -12,5 +12,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure #else public static Task CompletedTask = Task.FromResult(null); #endif + public static Task ZeroTask = Task.FromResult(0); } } \ No newline at end of file