diff --git a/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs b/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs index 34663937a5a5..dd4cde7bf873 100644 --- a/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs +++ b/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs @@ -8,9 +8,9 @@ namespace Microsoft.AspNetCore.Http.Features { public class HttpRequestIdentifierFeature : IHttpRequestIdentifierFeature { - // Base32 encoding - in ascii sort order for easy text based sorting - private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; - // Seed the _requestId for this application instance with + // Base32 encoding - in ascii sort order for easy text based sorting + private static readonly char[] s_encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV".ToCharArray(); + // Seed the _requestId for this application instance with // the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 // for a roughly increasing _requestId over restarts private static long _requestId = DateTime.UtcNow.Ticks; @@ -34,31 +34,26 @@ public string TraceIdentifier } } - private static unsafe string GenerateRequestId(long id) + private static string GenerateRequestId(long id) { - // The following routine is ~310% faster than calling long.ToString() on x64 - // and ~600% faster than calling long.ToString() on x86 in tight loops of 1 million+ iterations - // See: https://github.com/aspnet/Hosting/pull/385 - - // stackalloc to allocate array on stack rather than heap - char* charBuffer = stackalloc char[13]; - - charBuffer[0] = _encode32Chars[(int)(id >> 60) & 31]; - charBuffer[1] = _encode32Chars[(int)(id >> 55) & 31]; - charBuffer[2] = _encode32Chars[(int)(id >> 50) & 31]; - charBuffer[3] = _encode32Chars[(int)(id >> 45) & 31]; - charBuffer[4] = _encode32Chars[(int)(id >> 40) & 31]; - charBuffer[5] = _encode32Chars[(int)(id >> 35) & 31]; - charBuffer[6] = _encode32Chars[(int)(id >> 30) & 31]; - charBuffer[7] = _encode32Chars[(int)(id >> 25) & 31]; - charBuffer[8] = _encode32Chars[(int)(id >> 20) & 31]; - charBuffer[9] = _encode32Chars[(int)(id >> 15) & 31]; - charBuffer[10] = _encode32Chars[(int)(id >> 10) & 31]; - charBuffer[11] = _encode32Chars[(int)(id >> 5) & 31]; - charBuffer[12] = _encode32Chars[(int)id & 31]; - - // string ctor overload that takes char* - return new string(charBuffer, 0, 13); + return string.Create(13, id, (buffer, value) => + { + char[] encode32Chars = s_encode32Chars; + + buffer[12] = encode32Chars[value & 31]; + buffer[11] = encode32Chars[(value >> 5) & 31]; + buffer[10] = encode32Chars[(value >> 10) & 31]; + buffer[9] = encode32Chars[(value >> 15) & 31]; + buffer[8] = encode32Chars[(value >> 20) & 31]; + buffer[7] = encode32Chars[(value >> 25) & 31]; + buffer[6] = encode32Chars[(value >> 30) & 31]; + buffer[5] = encode32Chars[(value >> 35) & 31]; + buffer[4] = encode32Chars[(value >> 40) & 31]; + buffer[3] = encode32Chars[(value >> 45) & 31]; + buffer[2] = encode32Chars[(value >> 50) & 31]; + buffer[1] = encode32Chars[(value >> 55) & 31]; + buffer[0] = encode32Chars[(value >> 60) & 31]; + }); } } } diff --git a/src/Middleware/ResponseCaching/src/Internal/FastGuid.cs b/src/Middleware/ResponseCaching/src/Internal/FastGuid.cs index b62ba22f8388..571227516b09 100644 --- a/src/Middleware/ResponseCaching/src/Internal/FastGuid.cs +++ b/src/Middleware/ResponseCaching/src/Internal/FastGuid.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal internal class FastGuid { // Base32 encoding - in ascii sort order for easy text based sorting - private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; + private static readonly char[] s_encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV".ToCharArray(); // Global ID private static long NextId; @@ -53,28 +53,25 @@ internal static FastGuid NewGuid() return new FastGuid(Interlocked.Increment(ref NextId)); } - private static unsafe string GenerateGuidString(FastGuid guid) + private static string GenerateGuidString(FastGuid guid) { - // stackalloc to allocate array on stack rather than heap - char* charBuffer = stackalloc char[13]; - - // ID - charBuffer[0] = _encode32Chars[(int)(guid.IdValue >> 60) & 31]; - charBuffer[1] = _encode32Chars[(int)(guid.IdValue >> 55) & 31]; - charBuffer[2] = _encode32Chars[(int)(guid.IdValue >> 50) & 31]; - charBuffer[3] = _encode32Chars[(int)(guid.IdValue >> 45) & 31]; - charBuffer[4] = _encode32Chars[(int)(guid.IdValue >> 40) & 31]; - charBuffer[5] = _encode32Chars[(int)(guid.IdValue >> 35) & 31]; - charBuffer[6] = _encode32Chars[(int)(guid.IdValue >> 30) & 31]; - charBuffer[7] = _encode32Chars[(int)(guid.IdValue >> 25) & 31]; - charBuffer[8] = _encode32Chars[(int)(guid.IdValue >> 20) & 31]; - charBuffer[9] = _encode32Chars[(int)(guid.IdValue >> 15) & 31]; - charBuffer[10] = _encode32Chars[(int)(guid.IdValue >> 10) & 31]; - charBuffer[11] = _encode32Chars[(int)(guid.IdValue >> 5) & 31]; - charBuffer[12] = _encode32Chars[(int)guid.IdValue & 31]; - - // string ctor overload that takes char* - return new string(charBuffer, 0, 13); + return string.Create(13, guid.IdValue, (buffer, value) => + { + char[] encode32Chars = s_encode32Chars; + buffer[12] = encode32Chars[value & 31]; + buffer[11] = encode32Chars[(value >> 5) & 31]; + buffer[10] = encode32Chars[(value >> 10) & 31]; + buffer[9] = encode32Chars[(value >> 15) & 31]; + buffer[8] = encode32Chars[(value >> 20) & 31]; + buffer[7] = encode32Chars[(value >> 25) & 31]; + buffer[6] = encode32Chars[(value >> 30) & 31]; + buffer[5] = encode32Chars[(value >> 35) & 31]; + buffer[4] = encode32Chars[(value >> 40) & 31]; + buffer[3] = encode32Chars[(value >> 45) & 31]; + buffer[2] = encode32Chars[(value >> 50) & 31]; + buffer[1] = encode32Chars[(value >> 55) & 31]; + buffer[0] = encode32Chars[(value >> 60) & 31]; + }); } } } diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/CorrelationIdGenerator.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/CorrelationIdGenerator.cs index fc161d4116a6..a6c641d59429 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/CorrelationIdGenerator.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/CorrelationIdGenerator.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure internal static class CorrelationIdGenerator { // Base32 encoding - in ascii sort order for easy text based sorting - private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; + private static readonly char[] s_encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV".ToCharArray(); // Seed the _lastConnectionId for this application instance with // the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 @@ -18,31 +18,26 @@ internal static class CorrelationIdGenerator public static string GetNextId() => GenerateId(Interlocked.Increment(ref _lastId)); - private static unsafe string GenerateId(long id) + private static string GenerateId(long id) { - // The following routine is ~310% faster than calling long.ToString() on x64 - // and ~600% faster than calling long.ToString() on x86 in tight loops of 1 million+ iterations - // See: https://github.com/aspnet/Hosting/pull/385 - - // stackalloc to allocate array on stack rather than heap - char* charBuffer = stackalloc char[13]; - - charBuffer[0] = _encode32Chars[(int)(id >> 60) & 31]; - charBuffer[1] = _encode32Chars[(int)(id >> 55) & 31]; - charBuffer[2] = _encode32Chars[(int)(id >> 50) & 31]; - charBuffer[3] = _encode32Chars[(int)(id >> 45) & 31]; - charBuffer[4] = _encode32Chars[(int)(id >> 40) & 31]; - charBuffer[5] = _encode32Chars[(int)(id >> 35) & 31]; - charBuffer[6] = _encode32Chars[(int)(id >> 30) & 31]; - charBuffer[7] = _encode32Chars[(int)(id >> 25) & 31]; - charBuffer[8] = _encode32Chars[(int)(id >> 20) & 31]; - charBuffer[9] = _encode32Chars[(int)(id >> 15) & 31]; - charBuffer[10] = _encode32Chars[(int)(id >> 10) & 31]; - charBuffer[11] = _encode32Chars[(int)(id >> 5) & 31]; - charBuffer[12] = _encode32Chars[(int)id & 31]; - - // string ctor overload that takes char* - return new string(charBuffer, 0, 13); + return string.Create(13, id, (buffer, value) => + { + char[] encode32Chars = s_encode32Chars; + + buffer[12] = encode32Chars[value & 31]; + buffer[11] = encode32Chars[(value >> 5) & 31]; + buffer[10] = encode32Chars[(value >> 10) & 31]; + buffer[9] = encode32Chars[(value >> 15) & 31]; + buffer[8] = encode32Chars[(value >> 20) & 31]; + buffer[7] = encode32Chars[(value >> 25) & 31]; + buffer[6] = encode32Chars[(value >> 30) & 31]; + buffer[5] = encode32Chars[(value >> 35) & 31]; + buffer[4] = encode32Chars[(value >> 40) & 31]; + buffer[3] = encode32Chars[(value >> 45) & 31]; + buffer[2] = encode32Chars[(value >> 50) & 31]; + buffer[1] = encode32Chars[(value >> 55) & 31]; + buffer[0] = encode32Chars[(value >> 60) & 31]; + }); } } } diff --git a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/StringUtilities.cs b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/StringUtilities.cs index 95073a97de11..edc3a8eeed95 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/StringUtilities.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Infrastructure/StringUtilities.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -94,8 +94,8 @@ public static unsafe bool TryGetAsciiString(byte* input, char* output, int count var vector = Unsafe.AsRef>(input); isValid &= CheckBytesInAsciiRange(vector); Vector.Widen( - vector, - out Unsafe.AsRef>(output), + vector, + out Unsafe.AsRef>(output), out Unsafe.AsRef>(output + Vector.Count)); input += Vector.Count; @@ -109,7 +109,7 @@ public static unsafe bool TryGetAsciiString(byte* input, char* output, int count return isValid; } - private static readonly string _encode16Chars = "0123456789ABCDEF"; + private static readonly char[] s_encode16Chars = "0123456789ABCDEF".ToCharArray(); /// /// A faster version of String.Concat(, , .ToString("X8")) @@ -118,7 +118,7 @@ public static unsafe bool TryGetAsciiString(byte* input, char* output, int count /// /// /// - public static unsafe string ConcatAsHexSuffix(string str, char separator, uint number) + public static string ConcatAsHexSuffix(string str, char separator, uint number) { var length = 1 + 8; if (str != null) @@ -126,31 +126,28 @@ public static unsafe string ConcatAsHexSuffix(string str, char separator, uint n length += str.Length; } - // stackalloc to allocate array on stack rather than heap - char* charBuffer = stackalloc char[length]; - - var i = 0; - if (str != null) + return string.Create(length, (str, separator, number), (buffer, tuple) => { - for (i = 0; i < str.Length; i++) + var (tupleStr, tupleSeparator, tupleNumber) = tuple; + char[] encode16Chars = s_encode16Chars; + + var i = 0; + if (tupleStr != null) { - charBuffer[i] = str[i]; + tupleStr.AsSpan().CopyTo(buffer); + i = tupleStr.Length; } - } - - charBuffer[i] = separator; - - charBuffer[i + 1] = _encode16Chars[(int)(number >> 28) & 0xF]; - charBuffer[i + 2] = _encode16Chars[(int)(number >> 24) & 0xF]; - charBuffer[i + 3] = _encode16Chars[(int)(number >> 20) & 0xF]; - charBuffer[i + 4] = _encode16Chars[(int)(number >> 16) & 0xF]; - charBuffer[i + 5] = _encode16Chars[(int)(number >> 12) & 0xF]; - charBuffer[i + 6] = _encode16Chars[(int)(number >> 8) & 0xF]; - charBuffer[i + 7] = _encode16Chars[(int)(number >> 4) & 0xF]; - charBuffer[i + 8] = _encode16Chars[(int)number & 0xF]; - // string ctor overload that takes char* - return new string(charBuffer, 0, length); + buffer[i + 8] = encode16Chars[tupleNumber & 0xF]; + buffer[i + 7] = encode16Chars[(tupleNumber >> 4) & 0xF]; + buffer[i + 6] = encode16Chars[(tupleNumber >> 8) & 0xF]; + buffer[i + 5] = encode16Chars[(tupleNumber >> 12) & 0xF]; + buffer[i + 4] = encode16Chars[(tupleNumber >> 16) & 0xF]; + buffer[i + 3] = encode16Chars[(tupleNumber >> 20) & 0xF]; + buffer[i + 2] = encode16Chars[(tupleNumber >> 24) & 0xF]; + buffer[i + 1] = encode16Chars[(tupleNumber >> 28) & 0xF]; + buffer[i] = tupleSeparator; + }); } [MethodImpl(MethodImplOptions.AggressiveInlining)] // Needs a push