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:
老九
2022-12-03 13:17:02 +01:00
committed by GitHub
co-authored by Christian
parent 27f2474876
commit 862d76471b
7 changed files with 81 additions and 10 deletions
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>Exe</OutputType>
<DebugType>Full</DebugType>
<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);
}
}
}
+2 -3
View File
@@ -36,7 +36,7 @@ namespace MQTTnet.Formatter
ValidateReceiveBuffer(length);
var result = new byte[length];
Array.Copy(_buffer, _position, result, 0, length);
Memory.Copy(_buffer, _position, result, 0, length);
_position += length;
return result;
@@ -80,8 +80,7 @@ namespace MQTTnet.Formatter
}
var buffer = new byte[bufferLength];
Array.Copy(_buffer, _position, buffer, 0, bufferLength);
Memory.Copy(_buffer, _position, buffer, 0, bufferLength);
_position += bufferLength;
return buffer;
+3 -2
View File
@@ -6,6 +6,7 @@ using System;
using System.Runtime.CompilerServices;
using System.Text;
using MQTTnet.Exceptions;
using MQTTnet.Internal;
using MQTTnet.Protocol;
namespace MQTTnet.Formatter
@@ -125,7 +126,7 @@ namespace MQTTnet.Formatter
EnsureAdditionalCapacity(count);
Array.Copy(buffer, offset, _buffer, _position, count);
Memory.Copy(buffer, offset, _buffer, _position, count);
IncreasePosition(count);
}
@@ -149,7 +150,7 @@ namespace MQTTnet.Formatter
_buffer[_position] = (byte)(valueLength >> 8);
_buffer[_position + 1] = (byte)valueLength;
Array.Copy(value, 0, _buffer, _position + 2, valueLength);
Memory.Copy(value, 0, _buffer, _position + 2, valueLength);
IncreasePosition(valueLength + 2);
}
}
+4 -4
View File
@@ -43,8 +43,8 @@ namespace MQTTnet.Formatter
}
var buffer = new byte[Length];
Array.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
Array.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
Memory.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
Memory.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
return buffer;
}
@@ -57,8 +57,8 @@ namespace MQTTnet.Formatter
}
var buffer = new byte[Length];
Array.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
Array.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
Memory.Copy(Packet.Array, Packet.Offset, buffer, 0, Packet.Count);
Memory.Copy(Payload.Array, Payload.Offset, buffer, Packet.Count, Payload.Count);
return new ArraySegment<byte>(buffer);
}
+29
View File
@@ -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
}
}
}
+1
View File
@@ -42,6 +42,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<DisableImplicitAspNetCoreAnalyzers>true</DisableImplicitAspNetCoreAnalyzers>
<NoWarn>1591</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='uap10.0'">