~ruther/NosSmooth

16838dfe4371d1a0ce447a11fab2bbb6f625f561 — František Boháček 3 years ago e11fb25
feat: store compiled fill functions in list type converter
1 files changed, 6 insertions(+), 3 deletions(-)

M Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs
M Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs => Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs +6 -3
@@ 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<Type, Func<IEnumerable<object?>, object>> _fillFunctions;

    /// <summary>
    /// Initializes a new instance of the <see cref="ListTypeConverter"/> class.


@@ 30,11 32,12 @@ public class ListTypeConverter : ISpecialTypeConverter
    public ListTypeConverter(TypeConverterRepository typeConverterRepository)
    {
        _typeConverterRepository = typeConverterRepository;
        _fillFunctions = new ConcurrentDictionary<Type, Func<IEnumerable<object?>, object>>();
    }

    /// <inheritdoc />
    public bool ShouldHandle(Type type)
        => typeof(IEnumerable).IsAssignableFrom(type);
        => type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(type);

    /// <inheritdoc />
    public Result<object?> 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);
    }

    /// <inheritdoc />

Do not follow this link