diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 034cd25..542c800 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -6,20 +6,23 @@ using System.Net; using System.Text; using Example.Shared; using mROA.Codegen; +using mROA.Implementation; using mROA.Implementation.Bootstrap; using mROA.Implementation.Frontend; var mixer = new FullMixBuilder(); - -mixer.Modules.Add(new FrontendContextRepository()); +new RemoteTypeBinder(); +mixer.Modules.Add(new RemoteContextRepository()); mixer.Modules.Add(new JsonFrontendSerialisationModule()); mixer.Modules.Add(new StreamBasedFrontendInteractionModule()); mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); mixer.Build(); +TransmissionConfig.RealContextRepository = mixer.GetModule(); + mixer.GetModule().Connect(); -var context = mixer.GetModule(); +var context = mixer.GetModule(); var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; diff --git a/Example.Shared/ILoadTest.cs b/Example.Shared/ILoadTest.cs index 6da94c6..6cec93f 100644 --- a/Example.Shared/ILoadTest.cs +++ b/Example.Shared/ILoadTest.cs @@ -10,4 +10,5 @@ public interface ILoadTest int Last(int next); void C(); void A(); -} \ No newline at end of file +} + diff --git a/Example.Shared/IPrinterFactory.cs b/Example.Shared/IPrinterFactory.cs index 707ce20..6ea8b0c 100644 --- a/Example.Shared/IPrinterFactory.cs +++ b/Example.Shared/IPrinterFactory.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using mROA.Implementation; using mROA.Implementation.Attributes; @@ -7,4 +8,5 @@ namespace Example.Shared; public interface IPrinterFactory { TransmittedSharedObject Create(string printerName); -} \ No newline at end of file +} + diff --git a/mROA.Benchmark/Program.cs b/mROA.Benchmark/Program.cs index 24fe190..562a3b7 100644 --- a/mROA.Benchmark/Program.cs +++ b/mROA.Benchmark/Program.cs @@ -1,9 +1,51 @@ -namespace mROA.Benchmark; +using System.Collections.Immutable; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Running; + +namespace mROA.Benchmark; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); + var summary = BenchmarkRunner.Run(); + + } +} + +public class CollectionsSpeed +{ + private const int N = 1000; + + private readonly List _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; + } + + [Benchmark] + public int ImmutableArray() + { + var sum = 0; + for (int i = 0; i < N; i++) + { + sum += _immutable[i]; + } + return sum; } } \ No newline at end of file diff --git a/mROA.Benchmark/mROA.Benchmark.csproj b/mROA.Benchmark/mROA.Benchmark.csproj index 85b4959..78eb323 100644 --- a/mROA.Benchmark/mROA.Benchmark.csproj +++ b/mROA.Benchmark/mROA.Benchmark.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 61a597c..f9cbd4d 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -255,61 +255,15 @@ using mROA.Abstract; namespace mROA.Codegen; -public class FrontendContextRepository : IContextRepository +public sealed class RemoteTypeBinder {{ - private ISerialisationModule.IFrontendSerialisationModule _serialisationModule; - private static readonly FrozenDictionary _remoteTypes = new Dictionary {{ + static RemoteTypeBinder(){{ + RemoteContextRepository.RemoteTypes = new Dictionary {{ {string.Join(", \r\n\t\t", frontendContextRepo)}}}.ToFrozenDictionary(); - - public int ResisterObject(object o) - {{ - throw new NotSupportedException(); - }} - - public void ClearObject(int id) - {{ - throw new NotSupportedException(); - }} - - public object GetObject(int id) - {{ - throw new NotSupportedException(); - }} - - public T GetObject(int id) - {{ - if (_remoteTypes.TryGetValue(typeof(T), out var remoteType)) - {{ - var remote = (T)Activator.CreateInstance(remoteType, id, _serialisationModule)!; - return remote; - }} - throw new NotSupportedException(); - }} - - public object GetSingleObject(Type type) - {{ - return Activator.CreateInstance(_remoteTypes[type], -1, _serialisationModule)!; - }} - - public int GetObjectIndex(object o) - {{ - if (o is IRemoteObject remote) - {{ - return remote.Id; - }} - throw new NotSupportedException(); - }} - - public void Inject(T dependency) - {{ - if (dependency is ISerialisationModule.IFrontendSerialisationModule serialisationModule) - _serialisationModule = serialisationModule; - - TransmissionConfig.DefaultContextRepository = this; }} }} "; - context.AddSource("FrontendContextRepository.g.cs", SourceText.From(fronendRepoCode, Encoding.UTF8)); + context.AddSource("RemoteTypeBinder.g.cs", SourceText.From(fronendRepoCode, Encoding.UTF8)); } } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicConfigurationExtensions.cs b/mROA/Implementation/Backend/BasicConfigurationExtensions.cs index c8cba70..30aee2a 100644 --- a/mROA/Implementation/Backend/BasicConfigurationExtensions.cs +++ b/mROA/Implementation/Backend/BasicConfigurationExtensions.cs @@ -31,7 +31,7 @@ public static class BasicConfigurationExtensions { var repo = new ContextRepository(); repo.FillSingletons(assemblies); - TransmissionConfig.DefaultContextRepository = repo; + TransmissionConfig.RealContextRepository = repo; builder.Modules.Add(repo); } diff --git a/mROA/Implementation/TransmittedSharedObject.cs b/mROA/Implementation/TransmittedSharedObject.cs index f64f79c..698e1d7 100644 --- a/mROA/Implementation/TransmittedSharedObject.cs +++ b/mROA/Implementation/TransmittedSharedObject.cs @@ -5,17 +5,19 @@ namespace mROA.Implementation; public static class TransmissionConfig { - public static IContextRepository? DefaultContextRepository { get; set; } + public static IContextRepository? RealContextRepository { get; set; } + public static IContextRepository? RemoteEndpointContextRepository { get; set; } + public static int ProcessOwnerId { get; set; } } public class TransmittedSharedObject where T : notnull { - private static IContextRepository GetDefaultContextRepository() => TransmissionConfig.DefaultContextRepository ?? + private IContextRepository GetDefaultContextRepository() => (OwnerId == TransmissionConfig.ProcessOwnerId ? TransmissionConfig.RealContextRepository : TransmissionConfig.RemoteEndpointContextRepository) ?? throw new NullReferenceException( "DefaultContextRepository was not defined"); private int _contextId = -1; - + public int OwnerId { get; init; } // ReSharper disable once MemberCanBePrivate.Global public int ContextId { @@ -43,5 +45,5 @@ public class TransmittedSharedObject where T : notnull public static implicit operator T(TransmittedSharedObject value) => value.Value; public static implicit operator TransmittedSharedObject(T value) => - new() { ContextId = GetDefaultContextRepository().GetObjectIndex(value) }; + new() { ContextId = value is IRemoteObject ro ? TransmissionConfig.RemoteEndpointContextRepository!.GetObjectIndex(ro) : TransmissionConfig.RealContextRepository!.GetObjectIndex(value) }; } \ No newline at end of file