mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +08:00
Implement buffer copy using different policies (#1599)
use Memory-Copy Co-authored-by: Christian <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<DebugType>Full</DebugType>
|
<DebugType>Full</DebugType>
|
||||||
<TargetFrameworks>net6.0</TargetFrameworks>
|
<TargetFrameworks>net6.0</TargetFrameworks>
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Jobs;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
|
||||||
|
namespace MQTTnet.Benchmarks
|
||||||
|
{
|
||||||
|
[SimpleJob(RuntimeMoniker.Net60)]
|
||||||
|
[RPlotExporter, RankColumn]
|
||||||
|
[MemoryDiagnoser]
|
||||||
|
public class MemoryCopyBenchmark
|
||||||
|
{
|
||||||
|
const int max_length = 1024 * 8;
|
||||||
|
private byte[] source;
|
||||||
|
private byte[] target;
|
||||||
|
|
||||||
|
[Params(64 - 1, 128 - 1, 256 - 1, 512 - 1, 1024 - 1, 2048 - 1, 5096 - 1)]
|
||||||
|
public int Length { get; set; }
|
||||||
|
|
||||||
|
[GlobalSetup]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
source = new byte[max_length];
|
||||||
|
target = new byte[max_length];
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(Baseline = true)]
|
||||||
|
public void Array_Copy()
|
||||||
|
{
|
||||||
|
Array.Copy(source, 0, target, 0, Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void Memory_Copy()
|
||||||
|
{
|
||||||
|
MQTTnet.Internal.Memory.Copy(source, 0, target, 0, Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ namespace MQTTnet.Formatter
|
|||||||
ValidateReceiveBuffer(length);
|
ValidateReceiveBuffer(length);
|
||||||
|
|
||||||
var result = new byte[length];
|
var result = new byte[length];
|
||||||
Array.Copy(_buffer, _position, result, 0, length);
|
Memory.Copy(_buffer, _position, result, 0, length);
|
||||||
_position += length;
|
_position += length;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -80,8 +80,7 @@ namespace MQTTnet.Formatter
|
|||||||
}
|
}
|
||||||
|
|
||||||
var buffer = new byte[bufferLength];
|
var buffer = new byte[bufferLength];
|
||||||
Array.Copy(_buffer, _position, buffer, 0, bufferLength);
|
Memory.Copy(_buffer, _position, buffer, 0, bufferLength);
|
||||||
|
|
||||||
_position += bufferLength;
|
_position += bufferLength;
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using MQTTnet.Exceptions;
|
using MQTTnet.Exceptions;
|
||||||
|
using MQTTnet.Internal;
|
||||||
using MQTTnet.Protocol;
|
using MQTTnet.Protocol;
|
||||||
|
|
||||||
namespace MQTTnet.Formatter
|
namespace MQTTnet.Formatter
|
||||||
@@ -125,7 +126,7 @@ namespace MQTTnet.Formatter
|
|||||||
|
|
||||||
EnsureAdditionalCapacity(count);
|
EnsureAdditionalCapacity(count);
|
||||||
|
|
||||||
Array.Copy(buffer, offset, _buffer, _position, count);
|
Memory.Copy(buffer, offset, _buffer, _position, count);
|
||||||
IncreasePosition(count);
|
IncreasePosition(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +150,7 @@ namespace MQTTnet.Formatter
|
|||||||
_buffer[_position] = (byte)(valueLength >> 8);
|
_buffer[_position] = (byte)(valueLength >> 8);
|
||||||
_buffer[_position + 1] = (byte)valueLength;
|
_buffer[_position + 1] = (byte)valueLength;
|
||||||
|
|
||||||
Array.Copy(value, 0, _buffer, _position + 2, valueLength);
|
Memory.Copy(value, 0, _buffer, _position + 2, valueLength);
|
||||||
IncreasePosition(valueLength + 2);
|
IncreasePosition(valueLength + 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ namespace MQTTnet.Formatter
|
|||||||
}
|
}
|
||||||
|
|
||||||
var buffer = new byte[Length];
|
var buffer = new byte[Length];
|
||||||
Array.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
|
Memory.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
|
||||||
Array.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
|
Memory.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
@@ -57,8 +57,8 @@ namespace MQTTnet.Formatter
|
|||||||
}
|
}
|
||||||
|
|
||||||
var buffer = new byte[Length];
|
var buffer = new byte[Length];
|
||||||
Array.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
|
Memory.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
|
||||||
Array.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
|
Memory.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
|
||||||
|
|
||||||
return new ArraySegment<byte>(buffer);
|
return new ArraySegment<byte>(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace MQTTnet.Internal
|
||||||
|
{
|
||||||
|
public static class Memory
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Copy(byte[] source, int sourceIndex, byte[] destination, int destinationIndex, int length)
|
||||||
|
{
|
||||||
|
#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1
|
||||||
|
source.AsSpan(sourceIndex, length).CopyTo(destination.AsSpan(destinationIndex, length));
|
||||||
|
#elif NET461_OR_GREATER || NETSTANDARD1_3_OR_GREATER
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (byte* pSoure = &source[sourceIndex])
|
||||||
|
{
|
||||||
|
fixed (byte* pDestination = &destination[destinationIndex])
|
||||||
|
{
|
||||||
|
System.Buffer.MemoryCopy(pSoure, pDestination, length, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Array.Copy(source, sourceIndex, destination, destinationIndex, length);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@
|
|||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<DisableImplicitAspNetCoreAnalyzers>true</DisableImplicitAspNetCoreAnalyzers>
|
<DisableImplicitAspNetCoreAnalyzers>true</DisableImplicitAspNetCoreAnalyzers>
|
||||||
<NoWarn>1591</NoWarn>
|
<NoWarn>1591</NoWarn>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(TargetFramework)'=='uap10.0'">
|
<PropertyGroup Condition="'$(TargetFramework)'=='uap10.0'">
|
||||||
|
|||||||
Reference in New Issue
Block a user