Eliminate allocation during HTTP2 path validation (#19273)

This commit is contained in:
James Newton-King 2020-02-25 15:37:04 +13:00 committed by GitHub
parent 1a4dbb7cc5
commit 08a8b38718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -317,9 +317,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
try
{
const int MaxPathBufferStackAllocSize = 256;
// The decoder operates only on raw bytes
var pathBuffer = new byte[pathSegment.Length].AsSpan();
for (int i = 0; i < pathSegment.Length; i++)
Span<byte> pathBuffer = pathSegment.Length <= MaxPathBufferStackAllocSize
// A constant size plus slice generates better code
// https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383159929
? stackalloc byte[MaxPathBufferStackAllocSize].Slice(0, pathSegment.Length)
// TODO - Consider pool here for less than 4096
// https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383604184
: new byte[pathSegment.Length];
for (var i = 0; i < pathSegment.Length; i++)
{
var ch = pathSegment[i];
// The header parser should already be checking this

View File

@ -691,9 +691,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3
try
{
const int MaxPathBufferStackAllocSize = 256;
// The decoder operates only on raw bytes
var pathBuffer = new byte[pathSegment.Length].AsSpan();
for (int i = 0; i < pathSegment.Length; i++)
Span<byte> pathBuffer = pathSegment.Length <= MaxPathBufferStackAllocSize
// A constant size plus slice generates better code
// https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383159929
? stackalloc byte[MaxPathBufferStackAllocSize].Slice(0, pathSegment.Length)
// TODO - Consider pool here for less than 4096
// https://github.com/dotnet/aspnetcore/pull/19273#discussion_r383604184
: new byte[pathSegment.Length];
for (var i = 0; i < pathSegment.Length; i++)
{
var ch = pathSegment[i];
// The header parser should already be checking this