From 4bba074d778933b312011df0ade8ff875250cded Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 28 Oct 2015 08:53:11 +0000 Subject: [PATCH 1/2] Read to null buffer Read to null buffer rather than Stream.Null as the CopyToAsync will create a `new byte[]` on each call https://github.com/dotnet/coreclr/blob/bc146608854d1db9cdbcc0b08029a87754e12b49/src/mscorlib/src/System/IO/Stream.cs#L218 of size 81920 bytes! https://github.com/dotnet/coreclr/blob/bc146608854d1db9cdbcc0b08029a87754e12b49/src/mscorlib/src/System/IO/Stream.cs#L48 --- src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs index 13546271e5..5b7678e48f 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs @@ -29,6 +29,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http private readonly object _onStartingSync = new Object(); private readonly object _onCompletedSync = new Object(); private readonly FrameRequestHeaders _requestHeaders = new FrameRequestHeaders(); + private readonly byte[] _nullBuffer = new byte[4096]; private readonly FrameResponseHeaders _responseHeaders = new FrameResponseHeaders(); private List, object>> _onStarting; @@ -198,7 +199,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Http await ProduceEnd(); // Finish reading the request body in case the app did not. - await RequestBody.CopyToAsync(Stream.Null); + while (await RequestBody.ReadAsync(_nullBuffer, 0, _nullBuffer.Length) != 0) + { + ; + } } terminated = !_keepAlive; From bdadc8a3dace276ecb1a65d4ea49caafa7c97fb3 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 28 Oct 2015 22:44:35 +0000 Subject: [PATCH 2/2] Move comment into loop, remove noop --- src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs index 5b7678e48f..8f1c804926 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs @@ -198,10 +198,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http await ProduceEnd(); - // Finish reading the request body in case the app did not. while (await RequestBody.ReadAsync(_nullBuffer, 0, _nullBuffer.Length) != 0) { - ; + // Finish reading the request body in case the app did not. } }