From f71e31a1cd5c8a327f6434baf9dbcf4da96f3265 Mon Sep 17 00:00:00 2001 From: Tim Seaward Date: Thu, 21 Sep 2017 07:30:59 +0100 Subject: [PATCH] Fix the double buffer rent/free (#921) * Fix the double buffer rent/free * Fix the double cast to span --- .../Internal/Formatters/BinaryMessageFormatter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageFormatter.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageFormatter.cs index 6d23544a9e..dfd311d996 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageFormatter.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Formatters/BinaryMessageFormatter.cs @@ -14,14 +14,14 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Formatters { // TODO: Optimize for size - (e.g. use Varints) var length = sizeof(long); - var buffer = ArrayPool.Shared.Rent(length); - BufferWriter.WriteBigEndian(buffer, payload.Length); - output.Write(buffer, 0, length); - ArrayPool.Shared.Return(buffer); + var buffer = ArrayPool.Shared.Rent(length + payload.Length); + var bufferSpan = buffer.AsSpan(); + + BufferWriter.WriteBigEndian(bufferSpan, payload.Length); + bufferSpan = bufferSpan.Slice(length); + payload.CopyTo(bufferSpan); + output.Write(buffer, 0, payload.Length + length); - buffer = ArrayPool.Shared.Rent(payload.Length); - payload.CopyTo(buffer); - output.Write(buffer, 0, payload.Length); ArrayPool.Shared.Return(buffer); } }