Prepare for many parsers

This commit is contained in:
2025-08-06 14:32:48 +03:00
parent 2cbcc6b201
commit 304811c964
6 changed files with 84 additions and 21 deletions
+37
View File
@@ -0,0 +1,37 @@
using BenchmarkDotNet.Attributes;
using mROA.Implementation;
namespace mROA.Benchmark;
public class IdGeneration
{
public static int X = 0;
[Benchmark]
public Guid GuidGeneration()
{
return Guid.NewGuid();
}
[Benchmark(Baseline = true)]
public RequestId ReqIdGeneration()
{
return RequestId.Generate();
}
[Benchmark]
public RequestId ReqIdIfGeneration()
{
var reqId = RequestId.Generate();
if (++X % 2 == 0)
{
reqId.P0 = 0;
}
else
{
reqId.P1 = 0;
}
return reqId;
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ using mROA.Implementation;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
var test = new CborTest(); var test = new CborTest();
test.ArrayWrite(); test.ArrayWrite();
BenchmarkRunner.Run<VirtualOverhead>(); BenchmarkRunner.Run<IdGeneration>();
public static class CborExtensions public static class CborExtensions
{ {
+39 -18
View File
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using mROA.Implementation; using mROA.Implementation;
@@ -5,17 +6,15 @@ namespace mROA.Benchmark;
public class VirtualOverhead public class VirtualOverhead
{ {
private ICallRequest _request;
private CallRequest _directRequest; private CallRequest _directRequest;
private RawCallRequest _rawRequest; private RawCallRequest _rawRequest;
public VirtualOverhead() [GlobalSetup]
{ public void Setup() {
_request = new CallRequest _directRequest = new CallRequest
{ {
CommandId = 5, Id = new RequestId(), ObjectId = ComplexObjectIdentifier.Null, Parameters = null CommandId = 5, Id = new RequestId(), ObjectId = ComplexObjectIdentifier.Null, Parameters = null
}; };
_directRequest = (CallRequest)_request;
_rawRequest = new RawCallRequest _rawRequest = new RawCallRequest
{ {
CommandId = _directRequest.CommandId, Id = _directRequest.Id, ObjectId = _directRequest.ObjectId, CommandId = _directRequest.CommandId, Id = _directRequest.Id, ObjectId = _directRequest.ObjectId,
@@ -24,18 +23,6 @@ public class VirtualOverhead
} }
[Benchmark(Baseline = true)] [Benchmark(Baseline = true)]
public long VirtualUsage()
{
var req = _request;
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
[Benchmark]
public long DirectUsage() public long DirectUsage()
{ {
var req = _directRequest; var req = _directRequest;
@@ -58,6 +45,40 @@ public class VirtualOverhead
acc += (req.Parameters ?? []).Length; acc += (req.Parameters ?? []).Length;
return acc; return acc;
} }
[Benchmark]
public long ParamUsage()
{
return Call(_directRequest);
}
[Benchmark]
public long ParamInUsage()
{
return CallIn(in _directRequest);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public long Call(CallRequest req)
{
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public long CallIn(in CallRequest req)
{
long acc = 0;
acc += req.CommandId;
acc += (long)(req.Id.P1 + req.Id.P0);
acc += req.ObjectId.ContextId + req.ObjectId.OwnerId;
acc += (req.Parameters ?? []).Length;
return acc;
}
} }
public struct RawCallRequest public struct RawCallRequest
+6 -1
View File
@@ -14,7 +14,7 @@ namespace mROA.Cbor
{ {
public class CborSerializationToolkit : IContextualSerializationToolKit public class CborSerializationToolkit : IContextualSerializationToolKit
{ {
private readonly CborWriter _writer = new(initialCapacity: 512); private readonly CborWriter _writer = new(initialCapacity: 2048);
private readonly IOrdinaryStructureParser[] _parsers = private readonly IOrdinaryStructureParser[] _parsers =
{ {
@@ -118,6 +118,11 @@ namespace mROA.Cbor
return Convert.ChangeType(nonCasted, type); return Convert.ChangeType(nonCasted, type);
} }
public IContextualSerializationToolKit Clone()
{
return new CborSerializationToolkit();
}
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context) public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
{ {
if (obj is not null && FindParser(obj.GetType(), out var parser)) if (obj is not null && FindParser(obj.GetType(), out var parser))
@@ -10,5 +10,6 @@ namespace mROA.Abstract
object? Deserialize(byte[] rawData, Type type, IEndPointContext? context); object? Deserialize(byte[] rawData, Type type, IEndPointContext? context);
T Deserialize<T>(ReadOnlyMemory<byte> rawMemory, IEndPointContext? context); T Deserialize<T>(ReadOnlyMemory<byte> rawMemory, IEndPointContext? context);
object? Cast(object? nonCasted, Type type, IEndPointContext? context); object? Cast(object? nonCasted, Type type, IEndPointContext? context);
IContextualSerializationToolKit Clone();
} }
} }
@@ -13,7 +13,6 @@ namespace mROA.Implementation
_serialization = serialization; _serialization = serialization;
} }
public IRepresentationModule Produce(int id) public IRepresentationModule Produce(int id)
{ {
var interaction = _hub.GetInteraction(id); var interaction = _hub.GetInteraction(id);