Perf in MessageParsers (#1616)

This commit is contained in:
BrennanConroy 2018-03-16 11:40:15 -07:00 committed by GitHub
parent 1eb64712fb
commit 9839e6b07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -7,15 +7,13 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters
{
public static class BinaryMessageParser
{
private static int[] _numBitsToShift = new[] { 0, 7, 14, 21, 28 };
private const int MaxLengthPrefixSize = 5;
public static bool TryParseMessage(ref ReadOnlySpan<byte> buffer, out ReadOnlySpan<byte> payload)
{
payload = default;
if (buffer.IsEmpty)
{
payload = default;
return false;
}
@ -39,7 +37,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters
do
{
byteRead = lengthPrefixBuffer[numBytes];
length = length | (((uint)(byteRead & 0x7f)) << _numBitsToShift[numBytes]);
length = length | (((uint)(byteRead & 0x7f)) << (numBytes * 7));
numBytes++;
}
while (numBytes < lengthPrefixBuffer.Length && ((byteRead & 0x80) != 0));
@ -47,6 +45,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters
// size bytes are missing
if ((byteRead & 0x80) != 0 && (numBytes < MaxLengthPrefixSize))
{
payload = default;
return false;
}
@ -58,6 +57,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters
// We don't have enough data
if (buffer.Length < length + numBytes)
{
payload = default;
return false;
}

View File

@ -9,11 +9,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters
{
public static bool TryParseMessage(ref ReadOnlySpan<byte> buffer, out ReadOnlySpan<byte> payload)
{
payload = default;
var index = buffer.IndexOf(TextMessageFormatter.RecordSeparator);
if (index == -1)
{
payload = default;
return false;
}