diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 3115c1b..63cf36b 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -50,6 +50,7 @@ class Program builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.Configure(o => o.DistributionType = EDistributionType.ExtractorFirst); var host = builder.Build(); // diff --git a/mROA.Benchmark/MethodAccess.cs b/mROA.Benchmark/MethodAccess.cs index d064b59..40c818b 100644 --- a/mROA.Benchmark/MethodAccess.cs +++ b/mROA.Benchmark/MethodAccess.cs @@ -6,12 +6,13 @@ using mROA.Implementation; namespace mROA.Benchmark; [MemoryDiagnoser] -[DisassemblyDiagnoser] +// [DisassemblyDiagnoser] public class MethodAccess { - private CollectableMethodRepository _current = new(); + private CollectableMethodRepository _current = new(); private FastMethodRepository _fast = new(); private readonly int _count = new GeneratedInvokersCollection().Count; + [GlobalSetup] public void SetupMethodAccess() { @@ -53,8 +54,8 @@ public class MethodAccess // { // return _fast.GetMethodBaked(-1); // } - - + + [Benchmark(Baseline = true)] public IMethodInvoker CurrentAll() { @@ -63,9 +64,10 @@ public class MethodAccess { inv = _current.GetMethod(i); } + return inv; } - + [Benchmark] public IMethodInvoker FastAll() { @@ -74,9 +76,10 @@ public class MethodAccess { inv = _fast.GetMethod(i); } + return inv; } - + [Benchmark] public IMethodInvoker FastAllBaked() { @@ -85,26 +88,46 @@ public class MethodAccess { inv = _fast.GetMethodBaked(i); } + + return inv; + } + + [Benchmark] + public IMethodInvoker FastAllBakedPreicrement() + { + IMethodInvoker inv = null; + for (int i = -1; i < _count; i++) + { + inv = _fast.GetMethodBakedPreicrement(i); + } + return inv; } } -internal class FastMethodRepository: IMethodRepository +internal class FastMethodRepository : IMethodRepository { private readonly List _methods = [MethodInvoker.Dispose]; private IMethodInvoker[] _baked = []; + public void AppendInvokers(IEnumerable methodInvokers) { _methods.AddRange(methodInvokers); _baked = _methods.ToArray(); } + public IMethodInvoker GetMethod(int id) { return _methods[id + 1]; } - + public IMethodInvoker GetMethodBaked(int id) { return _baked[id + 1]; } + + public IMethodInvoker GetMethodBakedPreicrement(int id) + { + return _baked[++id]; + } } \ No newline at end of file diff --git a/mROA.Benchmark/TaskWaiting.cs b/mROA.Benchmark/TaskWaiting.cs index c13ede7..fbb544a 100644 --- a/mROA.Benchmark/TaskWaiting.cs +++ b/mROA.Benchmark/TaskWaiting.cs @@ -18,7 +18,7 @@ public class TaskWaiting private readonly FastRepresentationModule _representationModule; public TaskWaiting() { - _executionModule = new BasicExecutionModule(new CancellationRepository(), new TestMethodRepo(), new CborSerializationToolkit()); + _executionModule = new BasicExecutionModule(new CancellationRepository(), new TestMethodRepo(), new CborSerializationToolkit(null)); _syncRequest = new CallRequest{CommandId = 0, Parameters = null, Id = RequestId.Generate(), ObjectId = new ComplexObjectIdentifier(-1, 0)}; _asyncRequest = new CallRequest{CommandId = 1, Parameters = null, Id = RequestId.Generate(), ObjectId = new ComplexObjectIdentifier(-1, 0)}; _instanceRepo = new InstanceRepository(null); diff --git a/mROA.Benchmark/VirtualOverhead.cs b/mROA.Benchmark/VirtualOverhead.cs index fea1854..3da144a 100644 --- a/mROA.Benchmark/VirtualOverhead.cs +++ b/mROA.Benchmark/VirtualOverhead.cs @@ -68,7 +68,7 @@ public class VirtualOverhead acc += (req.Parameters ?? []).Length; return acc; } - + [MethodImpl(MethodImplOptions.NoInlining)] public long CallIn(in CallRequest req) { diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index ef0bfb6..571bbef 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -110,7 +110,7 @@ namespace mROA.Cbor } catch (Exception) { - Console.WriteLine($"Bad deserialization. Bytes: {BitConverter.ToString(rawMemory.ToArray())}"); + Console.WriteLine($"Bad deserialization for type {type}. Bytes: {BitConverter.ToString(rawMemory.ToArray())}"); throw; } } diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index 4dbef5e..25f6d1f 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -143,17 +143,13 @@ namespace mROA.Implementation public class StreamExtractor { private const int BufferSize = ushort.MaxValue + 19; - private readonly Stream _ioStream; private readonly Memory _buffer = new byte[BufferSize]; - public StreamExtractor(Stream ioStream) { _ioStream = ioStream; } - public Action MessageReceived = _ => { }; - public async Task SingleReceive(CancellationToken token = default) { var firstRead = await _ioStream.ReadAsync(_buffer, token); @@ -170,12 +166,9 @@ namespace mROA.Implementation } var message = meta.ToMessage(_buffer.Span); -#if TRACE - Console.WriteLine("RECV " + message); -#endif + MessageReceived(message); } - public async Task LoopedReceive(CancellationToken token = default) { while (token.IsCancellationRequested == false && IsConnected) @@ -183,7 +176,6 @@ namespace mROA.Implementation await SingleReceive(token); } } - private async Task Send(NetworkMessage message, CancellationToken token = default) { var meta = message.ToMeta(); @@ -191,12 +183,7 @@ namespace mROA.Implementation message.Data.CopyTo(_buffer.Span[19..]); var sendingSpan = _buffer[..(19 + meta.BodyLength)]; await _ioStream.WriteAsync(sendingSpan, token); -#if TRACE - Console.WriteLine("SEND " + message); -#endif - // _logger.LogTrace("SEND {0}", message.ToString()); } - public async Task SendFromChannel(ChannelReader channel, CancellationToken token = default) { @@ -206,7 +193,6 @@ namespace mROA.Implementation await Send(message, token); } } - public bool IsConnected => _ioStream is { CanRead: true, CanWrite: true }; } } diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index 5a8572b..440d9a7 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -50,8 +50,7 @@ namespace mROA.Implementation.Frontend throw new Exception( $"Incorrect message type. Must be IdAssigning, current : {idMessage.MessageType.ToString()}"); } - - + _ = Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); var assignment = _serialization.Deserialize(idMessage.Data, _context); diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index 3452277..01b2128 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -33,8 +33,7 @@ namespace mROA.Implementation { var writer = _interaction.ReceiveChanel.Writer; var reader = _interaction.ReceiveChanel.Reader; - - + await foreach (var message in reader.ReadAllAsync(token)) { if (!rule(message))