From ca31627a5e4c01378bdf0cfb495316803666c7e4 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 1 Mar 2017 22:33:37 -0800 Subject: [PATCH] Parser clean up (#1419) - Remove stackalloc - Remove extra Move in ParseRequestLine --- .../Internal/Http/KestrelHttpParser.cs | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs index 2d1e51a013..18477d28dd 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs @@ -32,32 +32,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http examined = buffer.End; var start = buffer.Start; - ReadCursor end; - if (ReadCursorOperations.Seek(start, buffer.End, out end, ByteLF) == -1) + if (ReadCursorOperations.Seek(start, buffer.End, out var end, ByteLF) == -1) { return false; } - const int stackAllocLimit = 512; - // Move 1 byte past the \n end = buffer.Move(end, 1); var startLineBuffer = buffer.Slice(start, end); Span span; - if (startLineBuffer.IsSingleSpan) { // No copies, directly use the one and only span span = startLineBuffer.First.Span; } - else if (startLineBuffer.Length < stackAllocLimit) - { - // Multiple buffers and < stackAllocLimit, copy into a stack buffer - byte* stackBuffer = stackalloc byte[startLineBuffer.Length]; - span = new Span(stackBuffer, startLineBuffer.Length); - startLineBuffer.CopyTo(span); - } else { // We're not a single span here but we can use pooled arrays to avoid allocations in the rare case @@ -242,7 +231,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http var query = span.Slice(queryStart, queryEnd - queryStart); handler.OnStartLine(method, httpVersion, targetBuffer, pathBuffer, query, customMethod); - consumed = buffer.Move(start, i); + + consumed = end; examined = consumed; return true; } @@ -297,8 +287,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http return false; } - const int stackAllocLimit = 512; - if (lineEnd != bufferEnd) { lineEnd = buffer.Move(lineEnd, 1); @@ -312,13 +300,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http // No copies, directly use the one and only span span = headerBuffer.First.Span; } - else if (headerBuffer.Length < stackAllocLimit) - { - // Multiple buffers and < stackAllocLimit, copy into a stack buffer - byte* stackBuffer = stackalloc byte[headerBuffer.Length]; - span = new Span(stackBuffer, headerBuffer.Length); - headerBuffer.CopyTo(span); - } else { // We're not a single span here but we can use pooled arrays to avoid allocations in the rare case