Some refactoring

This commit is contained in:
2025-09-24 00:11:10 +03:00
parent dca21339db
commit aae30af664
8 changed files with 38 additions and 30 deletions
+1
View File
@@ -50,6 +50,7 @@ class Program
builder.Services.AddSingleton<ICallIndexProvider, GeneratedCallIndexProvider>(); builder.Services.AddSingleton<ICallIndexProvider, GeneratedCallIndexProvider>();
builder.Services.AddSingleton<ICancellationRepository, CancellationRepository>(); builder.Services.AddSingleton<ICancellationRepository, CancellationRepository>();
builder.Services.Configure<DistributionOptions>(o => o.DistributionType = EDistributionType.ExtractorFirst);
var host = builder.Build(); var host = builder.Build();
// //
+26 -3
View File
@@ -6,12 +6,13 @@ using mROA.Implementation;
namespace mROA.Benchmark; namespace mROA.Benchmark;
[MemoryDiagnoser] [MemoryDiagnoser]
[DisassemblyDiagnoser] // [DisassemblyDiagnoser]
public class MethodAccess public class MethodAccess
{ {
private CollectableMethodRepository _current = new(); private CollectableMethodRepository _current = new();
private FastMethodRepository _fast = new(); private FastMethodRepository _fast = new();
private readonly int _count = new GeneratedInvokersCollection().Count; private readonly int _count = new GeneratedInvokersCollection().Count;
[GlobalSetup] [GlobalSetup]
public void SetupMethodAccess() public void SetupMethodAccess()
{ {
@@ -63,6 +64,7 @@ public class MethodAccess
{ {
inv = _current.GetMethod(i); inv = _current.GetMethod(i);
} }
return inv; return inv;
} }
@@ -74,6 +76,7 @@ public class MethodAccess
{ {
inv = _fast.GetMethod(i); inv = _fast.GetMethod(i);
} }
return inv; return inv;
} }
@@ -85,19 +88,34 @@ public class MethodAccess
{ {
inv = _fast.GetMethodBaked(i); 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; return inv;
} }
} }
internal class FastMethodRepository: IMethodRepository internal class FastMethodRepository : IMethodRepository
{ {
private readonly List<IMethodInvoker> _methods = [MethodInvoker.Dispose]; private readonly List<IMethodInvoker> _methods = [MethodInvoker.Dispose];
private IMethodInvoker[] _baked = []; private IMethodInvoker[] _baked = [];
public void AppendInvokers(IEnumerable<IMethodInvoker> methodInvokers) public void AppendInvokers(IEnumerable<IMethodInvoker> methodInvokers)
{ {
_methods.AddRange(methodInvokers); _methods.AddRange(methodInvokers);
_baked = _methods.ToArray(); _baked = _methods.ToArray();
} }
public IMethodInvoker GetMethod(int id) public IMethodInvoker GetMethod(int id)
{ {
return _methods[id + 1]; return _methods[id + 1];
@@ -107,4 +125,9 @@ internal class FastMethodRepository: IMethodRepository
{ {
return _baked[id + 1]; return _baked[id + 1];
} }
public IMethodInvoker GetMethodBakedPreicrement(int id)
{
return _baked[++id];
}
} }
+1 -1
View File
@@ -18,7 +18,7 @@ public class TaskWaiting
private readonly FastRepresentationModule _representationModule; private readonly FastRepresentationModule _representationModule;
public TaskWaiting() 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)}; _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)}; _asyncRequest = new CallRequest{CommandId = 1, Parameters = null, Id = RequestId.Generate(), ObjectId = new ComplexObjectIdentifier(-1, 0)};
_instanceRepo = new InstanceRepository(null); _instanceRepo = new InstanceRepository(null);
+1 -1
View File
@@ -110,7 +110,7 @@ namespace mROA.Cbor
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine($"Bad deserialization. Bytes: {BitConverter.ToString(rawMemory.ToArray())}"); Console.WriteLine($"Bad deserialization for type {type}. Bytes: {BitConverter.ToString(rawMemory.ToArray())}");
throw; throw;
} }
} }
@@ -143,17 +143,13 @@ namespace mROA.Implementation
public class StreamExtractor public class StreamExtractor
{ {
private const int BufferSize = ushort.MaxValue + 19; private const int BufferSize = ushort.MaxValue + 19;
private readonly Stream _ioStream; private readonly Stream _ioStream;
private readonly Memory<byte> _buffer = new byte[BufferSize]; private readonly Memory<byte> _buffer = new byte[BufferSize];
public StreamExtractor(Stream ioStream) public StreamExtractor(Stream ioStream)
{ {
_ioStream = ioStream; _ioStream = ioStream;
} }
public Action<NetworkMessage> MessageReceived = _ => { }; public Action<NetworkMessage> MessageReceived = _ => { };
public async Task SingleReceive(CancellationToken token = default) public async Task SingleReceive(CancellationToken token = default)
{ {
var firstRead = await _ioStream.ReadAsync(_buffer, token); var firstRead = await _ioStream.ReadAsync(_buffer, token);
@@ -170,12 +166,9 @@ namespace mROA.Implementation
} }
var message = meta.ToMessage(_buffer.Span); var message = meta.ToMessage(_buffer.Span);
#if TRACE
Console.WriteLine("RECV " + message);
#endif
MessageReceived(message); MessageReceived(message);
} }
public async Task LoopedReceive(CancellationToken token = default) public async Task LoopedReceive(CancellationToken token = default)
{ {
while (token.IsCancellationRequested == false && IsConnected) while (token.IsCancellationRequested == false && IsConnected)
@@ -183,7 +176,6 @@ namespace mROA.Implementation
await SingleReceive(token); await SingleReceive(token);
} }
} }
private async Task Send(NetworkMessage message, CancellationToken token = default) private async Task Send(NetworkMessage message, CancellationToken token = default)
{ {
var meta = message.ToMeta(); var meta = message.ToMeta();
@@ -191,12 +183,7 @@ namespace mROA.Implementation
message.Data.CopyTo(_buffer.Span[19..]); message.Data.CopyTo(_buffer.Span[19..]);
var sendingSpan = _buffer[..(19 + meta.BodyLength)]; var sendingSpan = _buffer[..(19 + meta.BodyLength)];
await _ioStream.WriteAsync(sendingSpan, token); await _ioStream.WriteAsync(sendingSpan, token);
#if TRACE
Console.WriteLine("SEND " + message);
#endif
// _logger.LogTrace("SEND {0}", message.ToString());
} }
public async Task SendFromChannel(ChannelReader<NetworkMessage> channel, public async Task SendFromChannel(ChannelReader<NetworkMessage> channel,
CancellationToken token = default) CancellationToken token = default)
{ {
@@ -206,7 +193,6 @@ namespace mROA.Implementation
await Send(message, token); await Send(message, token);
} }
} }
public bool IsConnected => _ioStream is { CanRead: true, CanWrite: true }; public bool IsConnected => _ioStream is { CanRead: true, CanWrite: true };
} }
} }
@@ -51,7 +51,6 @@ namespace mROA.Implementation.Frontend
$"Incorrect message type. Must be IdAssigning, current : {idMessage.MessageType.ToString()}"); $"Incorrect message type. Must be IdAssigning, current : {idMessage.MessageType.ToString()}");
} }
_ = Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token)); _ = Task.Run(async () => await _currentExtractor.LoopedReceive(_rawExtractorCancellation.Token));
var assignment = _serialization.Deserialize<IdAssignment>(idMessage.Data, _context); var assignment = _serialization.Deserialize<IdAssignment>(idMessage.Data, _context);
@@ -34,7 +34,6 @@ namespace mROA.Implementation
var writer = _interaction.ReceiveChanel.Writer; var writer = _interaction.ReceiveChanel.Writer;
var reader = _interaction.ReceiveChanel.Reader; var reader = _interaction.ReceiveChanel.Reader;
await foreach (var message in reader.ReadAllAsync(token)) await foreach (var message in reader.ReadAllAsync(token))
{ {
if (!rule(message)) if (!rule(message))