@@ -0,0 +1,35 @@
|
||||
using System.Formats.Cbor;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using mROA.Implementation;
|
||||
|
||||
public static class CborExtensions
|
||||
{
|
||||
public static unsafe void WriteToCbor(this RequestId id, CborWriter writer)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void WriteToCborInline(this RequestId id, CborWriter writer)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
public static void WriteToDest(this RequestId id, Span<byte> destination)
|
||||
{
|
||||
MemoryMarshal.Write(destination, ref id);
|
||||
}
|
||||
|
||||
[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,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,44 +1,8 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
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<VirtualOverhead>();
|
||||
|
||||
public static class CborExtensions
|
||||
{
|
||||
public static unsafe void WriteToCbor(this RequestId id, CborWriter writer)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void WriteToCborInline(this RequestId id, CborWriter writer)
|
||||
{
|
||||
Span<byte> span = stackalloc byte[16];
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
public static void WriteToDest(this RequestId id, Span<byte> destination)
|
||||
{
|
||||
MemoryMarshal.Write(destination, ref id);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
BenchmarkRunner.Run<IdGeneration>();
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using mROA.Implementation;
|
||||
|
||||
@@ -5,17 +6,15 @@ namespace mROA.Benchmark;
|
||||
|
||||
public class VirtualOverhead
|
||||
{
|
||||
private ICallRequest _request;
|
||||
private CallRequest _directRequest;
|
||||
private RawCallRequest _rawRequest;
|
||||
|
||||
public VirtualOverhead()
|
||||
{
|
||||
_request = new CallRequest
|
||||
[GlobalSetup]
|
||||
public void Setup() {
|
||||
_directRequest = new CallRequest
|
||||
{
|
||||
CommandId = 5, Id = new RequestId(), ObjectId = ComplexObjectIdentifier.Null, Parameters = null
|
||||
};
|
||||
_directRequest = (CallRequest)_request;
|
||||
_rawRequest = new RawCallRequest
|
||||
{
|
||||
CommandId = _directRequest.CommandId, Id = _directRequest.Id, ObjectId = _directRequest.ObjectId,
|
||||
@@ -24,18 +23,6 @@ public class VirtualOverhead
|
||||
}
|
||||
|
||||
[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()
|
||||
{
|
||||
var req = _directRequest;
|
||||
@@ -58,6 +45,40 @@ public class VirtualOverhead
|
||||
acc += (req.Parameters ?? []).Length;
|
||||
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
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace mROA.Cbor
|
||||
{
|
||||
public class CborSerializationToolkit : IContextualSerializationToolKit
|
||||
{
|
||||
private readonly CborWriter _writer = new(initialCapacity: 512);
|
||||
private readonly CborWriter _writer = new(initialCapacity: 2048);
|
||||
|
||||
private readonly IOrdinaryStructureParser[] _parsers =
|
||||
{
|
||||
@@ -118,6 +118,11 @@ namespace mROA.Cbor
|
||||
return Convert.ChangeType(nonCasted, type);
|
||||
}
|
||||
|
||||
public IContextualSerializationToolKit Clone()
|
||||
{
|
||||
return new CborSerializationToolkit();
|
||||
}
|
||||
|
||||
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
|
||||
{
|
||||
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);
|
||||
T Deserialize<T>(ReadOnlyMemory<byte> rawMemory, IEndPointContext? context);
|
||||
object? Cast(object? nonCasted, Type type, IEndPointContext? context);
|
||||
IContextualSerializationToolKit Clone();
|
||||
}
|
||||
}
|
||||
@@ -25,15 +25,15 @@ namespace mROA.Implementation.Backend
|
||||
|
||||
public IRequestExtractor this[int id] => _producedExtractors[id];
|
||||
|
||||
public IRequestExtractor HubOnOnConnected(IRepresentationModule interaction)
|
||||
public IRequestExtractor HubOnOnConnected(IRepresentationModule representationModule)
|
||||
{
|
||||
var extractor = CreateExtractor(interaction);
|
||||
var extractor = CreateExtractor(representationModule);
|
||||
if (_mode.DistributionType == EDistributionType.Channeled)
|
||||
{
|
||||
extractor.StartExtraction().ContinueWith(_ => OnDisconnected(interaction));
|
||||
extractor.StartExtraction().ContinueWith(_ => OnDisconnected(representationModule));
|
||||
}
|
||||
|
||||
_producedExtractors[interaction.Id] = extractor;
|
||||
_producedExtractors[representationModule.Id] = extractor;
|
||||
return extractor;
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ namespace mROA.Implementation.Backend
|
||||
_extractorsTokenSources[interaction.ConnectionId] = cts;
|
||||
|
||||
_hub.RegisterInteraction(interaction);
|
||||
var requestExtractor = _hre.HubOnOnConnected(new RepresentationModule(interaction, _serialization));
|
||||
var requestExtractor = _hre.HubOnOnConnected(new RepresentationModule(interaction, _serialization.Clone()));
|
||||
|
||||
if (_distribution.DistributionType != EDistributionType.Channeled)
|
||||
{
|
||||
|
||||
@@ -13,12 +13,11 @@ namespace mROA.Implementation
|
||||
_serialization = serialization;
|
||||
}
|
||||
|
||||
|
||||
public IRepresentationModule Produce(int id)
|
||||
{
|
||||
var interaction = _hub.GetInteraction(id);
|
||||
|
||||
var produced = new RepresentationModule(interaction, _serialization);
|
||||
var produced = new RepresentationModule(interaction, _serialization.Clone());
|
||||
|
||||
return produced;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user