From 31dc30f0d4a29df0ed48f9501821322039d3c22d Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 4 Feb 2019 00:40:12 +0000 Subject: [PATCH 1/2] Use Pipelines on HttpContext --- .../Middleware/PlaintextMiddleware.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Benchmarks/Middleware/PlaintextMiddleware.cs b/src/Benchmarks/Middleware/PlaintextMiddleware.cs index 2cd1d23b7..2d50095be 100644 --- a/src/Benchmarks/Middleware/PlaintextMiddleware.cs +++ b/src/Benchmarks/Middleware/PlaintextMiddleware.cs @@ -32,6 +32,19 @@ public Task Invoke(HttpContext httpContext) return _next(httpContext); } +#if NETCOREAPP3_0 + public async static Task WriteResponse(HttpResponse response) + { + var payloadLength = _helloWorldPayload.Length; + response.StatusCode = 200; + response.ContentType = "text/plain"; + response.ContentLength = payloadLength; + + var pipe = response.BodyPipe; + pipe.Write(_helloWorldPayload); + await pipe.FlushAsync(); + } +#else public static Task WriteResponse(HttpResponse response) { var payloadLength = _helloWorldPayload.Length; @@ -40,10 +53,20 @@ public static Task WriteResponse(HttpResponse response) response.ContentLength = payloadLength; return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength); } +#endif } public static class PlaintextMiddlewareExtensions { +#if NETCOREAPP3_0 + internal static void Write(this PipeWriter pipe, byte[] payload) + { + var span = pipe.GetSpan(sizeHint: payload.Length); + payload.CopyTo(span); + pipe.Advance(payload.Length); + } +#endif + public static IApplicationBuilder UsePlainText(this IApplicationBuilder builder) { return builder.UseMiddleware(); From a5f5386c9ed0e4f673e2eb39fcb41c06ddf9a637 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 4 Feb 2019 03:17:02 +0000 Subject: [PATCH 2/2] Use WriteAsync --- .../Middleware/PlaintextMiddleware.cs | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Benchmarks/Middleware/PlaintextMiddleware.cs b/src/Benchmarks/Middleware/PlaintextMiddleware.cs index 2d50095be..8d50f7daf 100644 --- a/src/Benchmarks/Middleware/PlaintextMiddleware.cs +++ b/src/Benchmarks/Middleware/PlaintextMiddleware.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO.Pipelines; using System.Text; using System.Threading.Tasks; using Benchmarks.Configuration; @@ -33,16 +34,30 @@ public Task Invoke(HttpContext httpContext) } #if NETCOREAPP3_0 - public async static Task WriteResponse(HttpResponse response) + public static Task WriteResponse(HttpResponse response) { var payloadLength = _helloWorldPayload.Length; response.StatusCode = 200; response.ContentType = "text/plain"; response.ContentLength = payloadLength; - var pipe = response.BodyPipe; - pipe.Write(_helloWorldPayload); - await pipe.FlushAsync(); + var vt = response.BodyPipe.WriteAsync(_helloWorldPayload); + + if (vt.IsCompletedSuccessfully) + { + // Signal consumption to the IValueTaskSource + vt.GetAwaiter().GetResult(); + return Task.CompletedTask; + } + else + { + return AwaitResult(vt); + } + + async Task AwaitResult(ValueTask flushResult) + { + await flushResult; + } } #else public static Task WriteResponse(HttpResponse response) @@ -58,15 +73,6 @@ public static Task WriteResponse(HttpResponse response) public static class PlaintextMiddlewareExtensions { -#if NETCOREAPP3_0 - internal static void Write(this PipeWriter pipe, byte[] payload) - { - var span = pipe.GetSpan(sizeHint: payload.Length); - payload.CopyTo(span); - pipe.Advance(payload.Length); - } -#endif - public static IApplicationBuilder UsePlainText(this IApplicationBuilder builder) { return builder.UseMiddleware();