From 0c2accd72cff90f21561296c81de9226ea10b10f Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 5 Feb 2024 16:18:47 +0100 Subject: [PATCH] feat: Parallel loops for initialization --- Rai.PacketMediator/PacketDistributor.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Rai.PacketMediator/PacketDistributor.cs b/Rai.PacketMediator/PacketDistributor.cs index e1861b3..6987afc 100644 --- a/Rai.PacketMediator/PacketDistributor.cs +++ b/Rai.PacketMediator/PacketDistributor.cs @@ -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 where TPacketIdEnum : En } var tempDeserializationMap = - new Dictionary>(); + new ConcurrentDictionary>(); _packetHandlersInstantiation = new ConcurrentDictionary?>(); - packetHandlers.ForEach(x => + Parallel.ForEach(packetHandlers, packetHandlerPair => { var packetHandler = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, - x.Value); - _packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler); + packetHandlerPair.Value); + _packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler); }); - foreach (var packetsType in packetDictionary) + Parallel.ForEach(packetDictionary, packetsType => { var lambda = CodeGenerator.Lambda>(fun => { @@ -69,8 +68,8 @@ public class PacketDistributor where TPacketIdEnum : En CodeGenerator.Return(packetVariable); }).Compile(); - tempDeserializationMap.Add(packetsType.Key, lambda); - } + tempDeserializationMap.TryAdd(packetsType.Key, lambda); + }); _deserializationMap = tempDeserializationMap.ToImmutableDictionary(); }