From 16838dfe4371d1a0ce447a11fab2bbb6f625f561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 30 Dec 2021 10:57:26 +0100 Subject: [PATCH] feat: store compiled fill functions in list type converter --- .../Converters/Special/ListTypeConverter.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs b/Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs index 7473aee..9aa2637 100644 --- a/Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs +++ b/Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs @@ -6,6 +6,7 @@ using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -22,6 +23,7 @@ namespace NosSmooth.Packets.Converters.Special; public class ListTypeConverter : ISpecialTypeConverter { private readonly TypeConverterRepository _typeConverterRepository; + private readonly ConcurrentDictionary, object>> _fillFunctions; /// /// Initializes a new instance of the class. @@ -30,11 +32,12 @@ public class ListTypeConverter : ISpecialTypeConverter public ListTypeConverter(TypeConverterRepository typeConverterRepository) { _typeConverterRepository = typeConverterRepository; + _fillFunctions = new ConcurrentDictionary, object>>(); } /// public bool ShouldHandle(Type type) - => typeof(IEnumerable).IsAssignableFrom(type); + => type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(type); /// public Result Deserialize(Type type, PacketStringEnumerator stringEnumerator) @@ -44,7 +47,7 @@ public class ListTypeConverter : ISpecialTypeConverter do { - if (stringEnumerator.PushPreparedLevel()) + if (!stringEnumerator.PushPreparedLevel()) { return new ArgumentInvalidError(nameof(stringEnumerator), "The string enumerator has to have a prepared level for all lists."); } @@ -60,7 +63,7 @@ public class ListTypeConverter : ISpecialTypeConverter } while (!(stringEnumerator.IsOnLastToken() ?? false)); - return GetAndFillListMethod(genericType)(data); + return _fillFunctions.GetOrAdd(genericType, GetAndFillListMethod)(data); } /// -- 2.48.1