From e4cf2978e6e9bfbce6f706da6d9554aa9f7547cd Mon Sep 17 00:00:00 2001 From: Israel Lot Date: Tue, 17 Apr 2018 21:43:10 -0300 Subject: [PATCH 1/3] Remove useless sempahore object --- Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs | 2 -- MQTTnet.sln | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs index 1d48f23..800b103 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs @@ -19,7 +19,6 @@ namespace MQTTnet.Adapter private const int ReadBufferSize = 4096; // TODO: Move buffer size to config private bool _isDisposed; - private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); private readonly IMqttNetLogger _logger; private readonly IMqttChannel _channel; @@ -232,7 +231,6 @@ namespace MQTTnet.Adapter public void Dispose() { _isDisposed = true; - _semaphore?.Dispose(); _channel?.Dispose(); } diff --git a/MQTTnet.sln b/MQTTnet.sln index fab7608..07830c3 100644 --- a/MQTTnet.sln +++ b/MQTTnet.sln @@ -39,6 +39,9 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Extensions.Rpc", "Extensions\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj", "{C444E9C8-95FA-430E-9126-274129DE16CD}" EndProject Global + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|ARM = Debug|ARM From 942bf705821a392bdb121c021c190f4422fbea0e Mon Sep 17 00:00:00 2001 From: Israel Lot Date: Thu, 19 Apr 2018 19:38:03 -0300 Subject: [PATCH 2/3] one less copy and allocation --- .../Serializer/MqttPacketSerializer.cs | 7 +++---- .../Serializer/MqttPacketWriter.cs | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs index 8225e60..6848330 100644 --- a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs +++ b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs @@ -24,19 +24,18 @@ namespace MQTTnet.Serializer { // Leave enough head space for max header size (fixed + 4 variable remaining length) stream.Position = 5; - var fixedHeader = SerializePacket(packet, writer); - var remainingLength = MqttPacketWriter.EncodeRemainingLength((int)stream.Length - 5); + stream.Position = 1; + var remainingLength = MqttPacketWriter.EncodeRemainingLength((int)stream.Length - 5, stream); - var headerSize = remainingLength.Length + 1; + var headerSize = remainingLength + 1; var headerOffset = 5 - headerSize; // Position cursor on correct offset on beginining of array (has leading 0x0) stream.Position = headerOffset; writer.Write(fixedHeader); - writer.Write(remainingLength, 0, remainingLength.Length); #if NET461 || NET452 || NETSTANDARD2_0 var buffer = stream.GetBuffer(); diff --git a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs index 3de4961..368d224 100644 --- a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs +++ b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs @@ -56,14 +56,18 @@ namespace MQTTnet.Serializer Write(value); } - public static byte[] EncodeRemainingLength(int length) + public static int EncodeRemainingLength(int length,MemoryStream stream) { + //write the encoded remaining length right aligned on the 4 byte buffer + if (length <= 0) { - return new[] { (byte)0 }; + stream.Seek(3, SeekOrigin.Current); + stream.WriteByte(0); + return 1; } - var bytes = new byte[4]; + byte[] buffer = new byte[4]; var offset = 0; // Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html. @@ -77,13 +81,15 @@ namespace MQTTnet.Serializer encodedByte = encodedByte | 128; } - bytes[offset] = (byte)encodedByte; + buffer[offset] = (byte)encodedByte; offset++; } while (x > 0); - Array.Resize(ref bytes, offset); - return bytes; + stream.Seek(4 - offset, SeekOrigin.Current); + stream.Write(buffer, 0, offset); + + return offset; } } } From 04a40807687b20a45e767be55c04e6d3f469370c Mon Sep 17 00:00:00 2001 From: Israel Lot Date: Thu, 19 Apr 2018 19:45:01 -0300 Subject: [PATCH 3/3] removed performance section from sln file --- MQTTnet.sln | 3 --- 1 file changed, 3 deletions(-) diff --git a/MQTTnet.sln b/MQTTnet.sln index 07830c3..fab7608 100644 --- a/MQTTnet.sln +++ b/MQTTnet.sln @@ -39,9 +39,6 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Extensions.Rpc", "Extensions\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj", "{C444E9C8-95FA-430E-9126-274129DE16CD}" EndProject Global - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|ARM = Debug|ARM