serialization improves and benchmarks
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using System.Formats.Cbor;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
|
||||
namespace mROA.Benchmark;
|
||||
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class CborTest
|
||||
{
|
||||
private CborWriter _writer;
|
||||
private CborReader _reader;
|
||||
|
||||
private Memory<byte> FlatEncoded;
|
||||
private Memory<byte> ArrayEncoded;
|
||||
public CborTest()
|
||||
{
|
||||
_writer = new CborWriter(initialCapacity:512);
|
||||
|
||||
_writer.WriteStartArray(2);
|
||||
_writer.WriteByteString([1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
_writer.WriteStartArray(2);
|
||||
_writer.WriteInt32(12);
|
||||
_writer.WriteTextString("tralala");
|
||||
_writer.WriteEndArray();
|
||||
_writer.WriteEndArray();
|
||||
|
||||
ArrayEncoded = _writer.Encode();
|
||||
|
||||
_writer.Reset();
|
||||
|
||||
_writer.WriteStartArray(3);
|
||||
_writer.WriteByteString([1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
_writer.WriteInt32(12);
|
||||
_writer.WriteTextString("tralala");
|
||||
_writer.WriteEndArray();
|
||||
FlatEncoded = _writer.Encode();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int FlatWrite()
|
||||
{
|
||||
_writer.Reset();
|
||||
_writer.WriteStartArray(3);
|
||||
_writer.WriteByteString([1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
_writer.WriteInt32(12);
|
||||
_writer.WriteTextString("tralala");
|
||||
_writer.WriteEndArray();
|
||||
var len = _writer.Encode(FlatEncoded.Span);
|
||||
return len;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int ArrayWrite()
|
||||
{
|
||||
_writer.Reset();
|
||||
_writer.WriteStartArray(2);
|
||||
_writer.WriteByteString([1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
_writer.WriteStartArray(2);
|
||||
_writer.WriteInt32(12);
|
||||
_writer.WriteTextString("tralala");
|
||||
_writer.WriteEndArray();
|
||||
_writer.WriteEndArray();
|
||||
var len = _writer.Encode(ArrayEncoded.Span);
|
||||
return len;
|
||||
}
|
||||
[Benchmark]
|
||||
public int FlatRead()
|
||||
{
|
||||
var reader = new CborReader(FlatEncoded);
|
||||
reader.ReadStartArray();
|
||||
var arr = reader.ReadByteString();
|
||||
var i = reader.ReadInt32();
|
||||
var text = reader.ReadTextString();
|
||||
reader.ReadEndArray();
|
||||
|
||||
return arr.Length;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int ArrayRead()
|
||||
{
|
||||
var reader = new CborReader(ArrayEncoded);
|
||||
reader.ReadStartArray();
|
||||
var arr = reader.ReadByteString();
|
||||
reader.ReadStartArray();
|
||||
var i = reader.ReadInt32();
|
||||
var text = reader.ReadTextString();
|
||||
reader.ReadEndArray();
|
||||
reader.ReadEndArray();
|
||||
|
||||
return arr.Length;
|
||||
}
|
||||
}
|
||||
+34
-41
@@ -1,51 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
namespace mROA.Benchmark
|
||||
using System.Formats.Cbor;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using BenchmarkDotNet.Running;
|
||||
using mROA.Benchmark;
|
||||
using mROA.Implementation;
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
var test = new CborTest();
|
||||
test.ArrayWrite();
|
||||
BenchmarkRunner.Run<CborTest>();
|
||||
|
||||
public static class CborExtensions
|
||||
{
|
||||
class Program
|
||||
public static unsafe void WriteToCbor(this RequestId id, CborWriter writer)
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Console.WriteLine("Hello, World!");
|
||||
// var summary = BenchmarkRunner.Run<CollectionsSpeed>();
|
||||
}
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
public class CollectionsSpeed
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void WriteToCborInline(this RequestId id, CborWriter writer)
|
||||
{
|
||||
private const int N = 1000;
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
private readonly List<int> _immutable;
|
||||
private readonly int[] _array;
|
||||
|
||||
public CollectionsSpeed()
|
||||
{
|
||||
_array = Enumerable.Range(0, N).ToArray();
|
||||
// _immutable = [.._array];
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int DefaultArray()
|
||||
{
|
||||
var sum = 0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
sum += _array[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public static void WriteToDest(this RequestId id, Span<byte> destination)
|
||||
{
|
||||
MemoryMarshal.Write(destination, ref id);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int ImmutableArray()
|
||||
{
|
||||
var sum = 0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
sum += _immutable[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static unsafe void WriteToCborOpt(this RequestId id, CborWriter writer)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Formats.Cbor;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using mROA.Implementation;
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class RequestWriter
|
||||
{
|
||||
private const int N = 1000;
|
||||
public RequestId Id = RequestId.Generate();
|
||||
private CborWriter _writer;
|
||||
|
||||
public RequestWriter()
|
||||
{
|
||||
_writer = new CborWriter(initialCapacity: 512);
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public int DefaultCbor()
|
||||
{
|
||||
_writer.Reset();
|
||||
_writer.WriteByteString(Id.ToByteArray());
|
||||
return _writer.BytesWritten;
|
||||
}
|
||||
[Benchmark]
|
||||
public int DirectCbor()
|
||||
{
|
||||
_writer.Reset();
|
||||
Id.WriteToCbor(_writer);
|
||||
return _writer.BytesWritten;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int Stackalloc()
|
||||
{
|
||||
_writer.Reset();
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
Id.WriteToDest(span);
|
||||
_writer.WriteByteString(span);
|
||||
return _writer.BytesWritten;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int DirectCborInline()
|
||||
{
|
||||
_writer.Reset();
|
||||
Id.WriteToCborInline(_writer);
|
||||
return _writer.BytesWritten;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int DirectCborOpt()
|
||||
{
|
||||
_writer.Reset();
|
||||
Id.WriteToCborOpt(_writer);
|
||||
return _writer.BytesWritten;
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,24 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
|
||||
<ProjectReference Include="..\mROA\mROA.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Formats.Cbor">
|
||||
<HintPath>..\..\..\..\.nuget\packages\system.formats.cbor\9.0.7\lib\net9.0\System.Formats.Cbor.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user