начало разделенных контекстов

This commit is contained in:
2025-02-12 23:37:06 +03:00
parent 932f915f28
commit 6b794e1cd9
8 changed files with 69 additions and 61 deletions
+6 -3
View File
@@ -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<RemoteContextRepository>();
mixer.GetModule<NetworkFrontendBridge>().Connect();
var context = mixer.GetModule<FrontendContextRepository>();
var context = mixer.GetModule<RemoteContextRepository>();
var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory;
+2 -1
View File
@@ -10,4 +10,5 @@ public interface ILoadTest
int Last(int next);
void C();
void A();
}
}
+3 -1
View File
@@ -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<IPrinter> Create(string printerName);
}
}
+43 -1
View File
@@ -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<CollectionsSpeed>();
}
}
public class CollectionsSpeed
{
private const int N = 1000;
private readonly List<int> _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;
}
}
+4
View File
@@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
</Project>
+4 -50
View File
@@ -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<Type, Type> _remoteTypes = new Dictionary<Type, Type> {{
static RemoteTypeBinder(){{
RemoteContextRepository.RemoteTypes = new Dictionary<Type, Type> {{
{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<T>(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>(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));
}
}
}
@@ -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);
}
@@ -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<T> 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<T> where T : notnull
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value;
public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = GetDefaultContextRepository().GetObjectIndex(value) };
new() { ContextId = value is IRemoteObject ro ? TransmissionConfig.RemoteEndpointContextRepository!.GetObjectIndex(ro) : TransmissionConfig.RealContextRepository!.GetObjectIndex(value) };
}