From 985c3084a88c014acc576680ec13ce6b30255c48 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sun, 23 Jun 2019 12:37:55 +0200 Subject: [PATCH] Performance refactoring. --- .../MQTTnet/Formatter/MqttPacketBodyReader.cs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Source/MQTTnet/Formatter/MqttPacketBodyReader.cs b/Source/MQTTnet/Formatter/MqttPacketBodyReader.cs index dece6f3..1deb1fe 100644 --- a/Source/MQTTnet/Formatter/MqttPacketBodyReader.cs +++ b/Source/MQTTnet/Formatter/MqttPacketBodyReader.cs @@ -22,10 +22,7 @@ namespace MQTTnet.Formatter _length = length; } - public int Offset - { - get => _offset; - } + public int Offset => _offset; public int Length => _length - _offset; @@ -36,22 +33,13 @@ namespace MQTTnet.Formatter _offset = _initialOffset + position; } - public ArraySegment Read(int length) - { - ValidateReceiveBuffer(length); - - var buffer = new ArraySegment(_buffer, (int)_offset, (int)length); - _offset += length; - return buffer; - } - public byte ReadByte() { ValidateReceiveBuffer(1); return _buffer[_offset++]; } - + public bool ReadBoolean() { ValidateReceiveBuffer(1); @@ -73,7 +61,11 @@ namespace MQTTnet.Formatter public byte[] ReadRemainingData() { - return new ArraySegment(_buffer, (int)_offset, (int)(_length - _offset)).ToArray(); + var bufferLength = _length - _offset; + var buffer = new byte[bufferLength]; + Array.Copy(_buffer, _offset, buffer, 0, bufferLength); + + return buffer; } public ushort ReadTwoByteInteger() @@ -131,7 +123,7 @@ namespace MQTTnet.Formatter ValidateReceiveBuffer(length); - var result = new ArraySegment(_buffer, (int)_offset, length); + var result = new ArraySegment(_buffer, _offset, length); _offset += length; return result;