From 9839e6b07baff218edfee0b145c98d7635e2190c Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Fri, 16 Mar 2018 11:40:15 -0700 Subject: [PATCH] Perf in MessageParsers (#1616) --- .../Internal/Formatters/BinaryMessageParser.cs | 8 ++++---- .../Internal/Formatters/TextMessageParser.cs | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageParser.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageParser.cs index a795180975..cfeb9a2756 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageParser.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageParser.cs @@ -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 buffer, out ReadOnlySpan 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; } diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/TextMessageParser.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/TextMessageParser.cs index e8b0cb5e82..efd79e3586 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/TextMessageParser.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/TextMessageParser.cs @@ -9,11 +9,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters { public static bool TryParseMessage(ref ReadOnlySpan buffer, out ReadOnlySpan payload) { - payload = default; - var index = buffer.IndexOf(TextMessageFormatter.RecordSeparator); if (index == -1) { + payload = default; return false; }