feat: Parallel loops for initialization
This commit is contained in:
parent
0d0b27ba49
commit
0c2accd72c
1 changed files with 7 additions and 8 deletions
|
@ -4,7 +4,6 @@ using System.Collections.Concurrent;
|
|||
using System.Collections.Immutable;
|
||||
using System.Reflection;
|
||||
using System.Threading.Channels;
|
||||
using DotNext.Collections.Generic;
|
||||
using DotNext.Linq.Expressions;
|
||||
using DotNext.Metaprogramming;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
@ -47,16 +46,16 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
|||
}
|
||||
|
||||
var tempDeserializationMap =
|
||||
new Dictionary<TPacketIdEnum, Func<byte[], IIncomingPacket>>();
|
||||
new ConcurrentDictionary<TPacketIdEnum, Func<byte[], IIncomingPacket>>();
|
||||
_packetHandlersInstantiation = new ConcurrentDictionary<TPacketIdEnum, IPacketHandler<TSession>?>();
|
||||
packetHandlers.ForEach(x =>
|
||||
Parallel.ForEach(packetHandlers, packetHandlerPair =>
|
||||
{
|
||||
var packetHandler =
|
||||
ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider,
|
||||
x.Value);
|
||||
_packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler<TSession>);
|
||||
packetHandlerPair.Value);
|
||||
_packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler<TSession>);
|
||||
});
|
||||
foreach (var packetsType in packetDictionary)
|
||||
Parallel.ForEach(packetDictionary, packetsType =>
|
||||
{
|
||||
var lambda = CodeGenerator.Lambda<Func<byte[], IIncomingPacket>>(fun =>
|
||||
{
|
||||
|
@ -69,8 +68,8 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
|||
|
||||
CodeGenerator.Return(packetVariable);
|
||||
}).Compile();
|
||||
tempDeserializationMap.Add(packetsType.Key, lambda);
|
||||
}
|
||||
tempDeserializationMap.TryAdd(packetsType.Key, lambda);
|
||||
});
|
||||
|
||||
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue