Don't copy the array for incoming msgpack reads (#1686)

* Don't copy the array for incoming msgpack reads
- Don't use ToArray on the already sliced msgpack data.
- Turns out msgpack is self describing enough to not require the count, it just needs the buffer and start offset.
This commit is contained in:
David Fowler 2018-03-22 08:47:06 -07:00 committed by GitHub
parent 3f84eee116
commit b111c91cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -3,8 +3,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Protocols;
using Microsoft.AspNetCore.SignalR.Internal.Formatters;
using Microsoft.AspNetCore.Sockets;
@ -41,15 +43,18 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
{
while (BinaryMessageParser.TryParseMessage(ref input, out var payload))
{
messages.Add(ParseMessage(payload.ToArray(), binder));
var isArray = MemoryMarshal.TryGetArray(payload, out var arraySegment);
// This will never be false unless we started using un-managed buffers
Debug.Assert(isArray);
messages.Add(ParseMessage(arraySegment.Array, arraySegment.Offset, binder));
}
return messages.Count > 0;
}
private static HubMessage ParseMessage(byte[] input, IInvocationBinder binder)
private static HubMessage ParseMessage(byte[] input, int startOffset, IInvocationBinder binder)
{
using (var unpacker = Unpacker.Create(input))
using (var unpacker = Unpacker.Create(input, startOffset))
{
_ = ReadArrayLength(unpacker, "elementCount");