如何创建具有参数和 return 类型的私有类型的快速调用委托,加速 DynamicInvoke

How to create a fast-call delegate that has parameters and return type of private types, speeding up DynamicInvoke

我正在努力创建对私有 ImmutableDictionary.Add 的调用,它允许我利用 KeyCollisionBehavior 进行更精细的控制(Add 方法仅在键和值时抛出是不同的,我需要它总是扔)。

我可以通过基本反射到达我想去的地方,但是,在 MethodInfo 上调用 Invoke 或在委托上调用 DynamicInvoke 的开销很大(事实上,每次通话的时间几乎增加了三倍,这在我的场景中太重要了)。

我需要调用的函数签名:

/// call property Origin, this returns a private MutationInput<,>
private MutationInput<TKey, TValue> Origin {get; }

/// call ImmutableDictionary<,>.Add, this takes MutationInput<,> and returns private MutationResult<,>
private static MutationResult<TKey, TValue> Add(TKey key, TValue value, KeyCollisionBehavior<TKey, TValue> behavior, MutationInput<TKey, TValue> origin);

/// then call MutationResult<,>.Finalize
internal ImmutableDictionary<TKey, TValue> Finalize(ImmutableDictionary<TKey, TValue> priorMap);

这里的挑战是我需要传递私有类型,并且私有类型是签名的一部分。

通常,在调用 CreateDelegate 之后,您可以简单地将其转换为 Func<X, Y, Z>,这样可以提供近乎直接的调用速度。但是我不知道如何创建 Func<,> 如果泛型类型是私有的 and/or 在编译时未知。使用 object 不起作用,在转换时给出运行时异常。

这是我目前拥有的代码的简化版本(删除了很​​多 try/catch 和检查)。这有效:

/// Copy of enum type from Github source of ImmutableDictionary
type KeyCollisionBehavior =
    /// Sets the value for the given key, even if that overwrites an existing value.
    | SetValue = 0
    /// Skips the mutating operation if a key conflict is detected.
    | Skip = 1
    /// Throw an exception if the key already exists with a different value.
    | ThrowIfValueDifferent = 2
    /// Throw an exception if the key already exists regardless of its value.
    | ThrowAlways = 3

/// Simple wrapper DU to add type safety
type MutationInputWrapper = 
    /// Wraps the type ImmutableDictionary<K, V>.MutationInput, required as 4th param in the internal Add#4 method
    | MutationInput of obj

/// Simple wrapper to add type-safety
type MutationResultWrapper =
    /// Wraps the type ImmutableDictionary<K, V>.MutationResult, which is the result of an internal Add#4 operation
    | MutationResult of obj

/// Type abbreviation
type BclImmDict<'Key, 'Value> = System.Collections.Immutable.ImmutableDictionary<'Key, 'Value>

/// Private extensions to ImmutableDictionary
type ImmutableDictionary<'Key, 'Value>() =
    static let dicType = typeof<System.Collections.Immutable.ImmutableDictionary<'Key, 'Value>>
    static let addMethod = dicType.GetMethod("Add", BindingFlags.NonPublic ||| BindingFlags.Static)
    static let addMethodDelegate = 
        let parameters = addMethod.GetParameters() |> Array.map (fun p -> p.ParameterType)
        let funType = 
            typedefof<Func<_, _, _, _, _>>.MakeGenericType [|
                parameters.[0]
                parameters.[1]
                parameters.[2]
                parameters.[3]
                addMethod.ReturnType
            |]
        Delegate.CreateDelegate(funType, addMethod) // here one would normally cast to Func<X, Y...>

    static let mutationResultFinalizeMethod = 
        if not(isNull addMethod) && not(isNull(addMethod.ReturnParameter)) then
            /// Nested private type MutationResult, for simplicity taken from the return-param type of ImmutableDictionary.Add#4
            let mutationResultType = addMethod.ReturnParameter.ParameterType
            if not(isNull mutationResultType) then
                mutationResultType.GetMethod("Finalize", BindingFlags.NonPublic ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly)
            else
                null
        else
            null

    /// System.Collections.Immutable.ImmutableDictionary.get_Origin  // of valuetype ImmutableDictionary<,>.MutationInput<,>
    static let getOrigin = dicType.GetProperty("Origin", BindingFlags.NonPublic ||| BindingFlags.Instance)

    /// Calls private member ImmutableDictionary<,>.Add(key, value, behavior, origin), through reflection
    static member private refl_Add(key: 'Key, value: 'Value, behavior: KeyCollisionBehavior, MutationInput origin) =
        // use Invoke or DynamicInvoke makes little difference.
        //addMethod.Invoke(null, [|key; value; behavior; origin|])
        addMethodDelegate.DynamicInvoke([|box key; box value; box <| int behavior; origin|])
        |> MutationResult

    /// Gets the "origin" of an ImmutableDictionary, by calling the private property get_Origin
    static member private refl_GetOrigin(this: BclImmDict<'Key, 'Value>) =
        getOrigin.GetValue this
        |> MutationInput

    /// Finalizes the result by taking the (internal) MutationResult and returning a new non-mutational dictionary
    static member private refl_Finalize(MutationResult mutationResult, map: BclImmDict<'Key, 'Value>) =
        mutationResultFinalizeMethod.Invoke(mutationResult, [|map|])
        :?> BclImmDict<'Key, 'Value>

    /// Actual Add, with added control through CollisionBehavior
    static member InternalAddAndFinalize(key: 'Key, value: 'Value, behavior, thisMap) =
        let origin = ImmutableDictionary.refl_GetOrigin(thisMap)
        let mutationResult = ImmutableDictionary.refl_Add(key, value, behavior, origin)
        let finalizedMap = ImmutableDictionary.refl_Finalize(mutationResult, thisMap)
        finalizedMap

我知道上面的代码是用 F# 编写的,但如果您知道如何在 C# 中解决这个问题,我可以毫无问题地将您的答案翻译成我首选的目标语言。

我认为你过于复杂了。来自 the Remarks:

If the given key/value pair already exists in the dictionary, the existing instance of the dictionary is returned.

因此我们可以通过查看 Add 方法是否返回相同的字典引用来判断给定的 key/value 是否已经存在于字典中。

在 C# 中:

public static ImmutableDictionary<TKey, TValue> AddAndThrowIfAlreadyPresent<TKey, TValue>(
    ImmutableDictionary<TKey, TValue> dict,
    TKey key,
    TValue value)
{
    // null checks, etc
    var newDict = dict.Add(key, value);
    if (newDict == dict)
        throw new ArgumentException($"An element with the same key and value already exists. Key: {key}");
    return newDict;
}

我认为其他评论者的观点是,在这种特殊情况下,这种策略可能不是最好的。但是,这里有一个一般性问题,我将从表面上看:如何创建一个方法的委托,该方法具有您无法通过名称访问的类型,因为它们是私有的或内部的?

由于无法引用某些类型名称,因此无法创建比 Invoke/DynamicInvoke 快得多的强类型委托。在这种情况下,想法是在运行时使用 System.Reflection.Emit.DynamicMethod 为包装器方法生成 IL,该方法调用具有不可访问类型的方法,但此包装器仅公开您有权访问的类型。 DynamicMethod 可以通过另一个程序集中的类型 "owned" 从而绕过一些可见性检查。困难在于您必须准确地告诉运行时要为此包装器方法发出什么 IL,因此很难在其中实现复杂的逻辑。在这种情况下,事情很简单,可以手写:获取 属性 (Origin) 并调用两个方法(Add 和 Finalize)。

下面是 C# 中的一个实现:

enum KeyCollisionBehavior
{
    SetValue = 0,
    Skip = 1,
    ThrowIfValueDifferent = 2,
    ThrowAlways = 3,
}

internal static class ImmutableDictionaryHelper<TKey, TValue>
{
    private static readonly MethodInfo OriginPropertyGetter = typeof(ImmutableDictionary<TKey, TValue>)
        .GetProperty("Origin", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(true);

    private static readonly MethodInfo AddMethod = typeof(ImmutableDictionary<TKey, TValue>)
        .GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == "Add" && m.GetParameters().Length == 4).FirstOrDefault();

    private static readonly Type MutationResultType = AddMethod.ReturnType;

    private static readonly MethodInfo FinalizeMethod = MutationResultType
        .GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

    private static readonly Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> AddAndFinalize = CreateAddAndFinalize();

    private static Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> CreateAddAndFinalize()
    {
        var method = new DynamicMethod(
            nameof(AddAndFinalize),
            typeof(ImmutableDictionary<TKey, TValue>),
            new[] { typeof(TKey), typeof(TValue), typeof(KeyCollisionBehavior), typeof(ImmutableDictionary<TKey, TValue>) },
            typeof(ImmutableDictionary<TKey, TValue>));

        var ilGen = method.GetILGenerator();
        ilGen.DeclareLocal(OriginPropertyGetter.ReturnType);
        ilGen.DeclareLocal(AddMethod.ReturnType);

        // var origin = dictionary.Origin;
        ilGen.Emit(OpCodes.Ldarg_3);
        ilGen.Emit(OpCodes.Callvirt, OriginPropertyGetter);
        ilGen.Emit(OpCodes.Stloc_0);

        // var result = Add(key, value, behavior, origin)
        ilGen.Emit(OpCodes.Ldarg_0);
        ilGen.Emit(OpCodes.Ldarg_1);
        ilGen.Emit(OpCodes.Ldarg_2);
        ilGen.Emit(OpCodes.Ldloc_0);
        ilGen.Emit(OpCodes.Call, AddMethod);
        ilGen.Emit(OpCodes.Stloc_1);

        // var newDictionary = result.Finalize(dictionary);
        ilGen.Emit(OpCodes.Ldloca_S, 1);
        ilGen.Emit(OpCodes.Ldarg_3);
        ilGen.Emit(OpCodes.Call, FinalizeMethod);

        // return newDictionary;
        ilGen.Emit(OpCodes.Ret);

        var del = method.CreateDelegate(typeof(Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>));
        var func = (Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>)del;
        return func;
    }

    public static ImmutableDictionary<TKey, TValue> Add(ImmutableDictionary<TKey, TValue> source, TKey key, TValue value, KeyCollisionBehavior behavior)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        return AddAndFinalize(key, value, behavior, source);
    }
}

需要指出的一个细节是 CLR 将枚举作为整数处理,因此我们可以创建自己的 KeyCollisionBehavior 枚举,它与我们在没有任何显式转换的情况下无法访问的私有枚举兼容。

这是另一种方法,它与@Mike 的回答做同样的事情,但使用编译表达式代替(更容易读写):

enum KeyCollisionBehavior
{
    SetValue = 0,
    Skip = 1,
    ThrowIfValueDifferent = 2,
    ThrowAlways = 3,
}

internal static class ImmutableDictionaryHelper<TKey, TValue>
{
    private static readonly Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> AddAndFinalize = CreateAddAndFinalize();

    private static Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> CreateAddAndFinalize()
    {
        var originPropertyGetter = typeof(ImmutableDictionary<TKey, TValue>)
            .GetProperty("Origin", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(true);
        var addMethod = typeof(ImmutableDictionary<TKey, TValue>)
            .GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == "Add" && m.GetParameters().Length == 4).FirstOrDefault();
        var finalizeMethod = addMethod.ReturnType
            .GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        var key = Expression.Parameter(typeof(TKey), "key");
        var value = Expression.Parameter(typeof(TValue), "value");
        var behavior = Expression.Parameter(typeof(KeyCollisionBehavior), "behavior");
        var dictionary = Expression.Parameter(typeof(ImmutableDictionary<TKey, TValue>), "dictionary");

        // var convertedBehavior = (ImmutableDictionary<TKey, TValue>.KeyCollisionBehavior)behavior
        var convertedBehavior = Expression.Convert(behavior, addMethod.GetParameters()[2].ParameterType);
        // var origin = dictionary.Origin;
        var origin = Expression.Property(dictionary, originPropertyGetter);
        // var result = Add(key, value, behavior, origin)
        var result = Expression.Call(addMethod, key, value, convertedBehavior, origin);
        // var newDictionary = result.Finalize(dictionary);
        var newDictionary = Expression.Call(result, finalizeMethod, dictionary);

        var func = Expression.Lambda<Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>>(
            newDictionary, key, value, behavior, dictionary).Compile();
        return func;
    }

    public static ImmutableDictionary<TKey, TValue> Add(ImmutableDictionary<TKey, TValue> source, TKey key, TValue value, KeyCollisionBehavior behavior)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        return AddAndFinalize(key, value, behavior, source);
    }
}