繁体   English   中英

C#speedup方法使用表达式调用泛型类

[英]C# speedup method call of a generic class using expressions

我需要调用泛型类的实例方法。 签名如下所示:

public class HandlerFactory
{
    public static IHandler<T> Create<T>();
}

public interface IHandler<T>
{
    T Read(Stream s);

    void Write(Stream s, T v);
}

我设法通过使用表达式和DynamicInvoke使其工作。 可悲的是,DynamicInvoke的性能并不是那么好。 我无法将委托转换为Action<MemoryStream, T>因为我不知道编译时的类型。

public class Test
{
    public static void Write(MemoryStream s, object value)
    {
        var del = GetWriteDelegateForType(value.GetType());

        // TODO: How to make this faster?
        del.DynamicInvoke(s, value);
    }

    private static object GetHandlerForType(Type type)
    {
        var expr = Expression.Call(typeof(HandlerFactory), "Create", new[] { type });
        var createInstanceLambda = Expression.Lambda<Func<object>>(expr).Compile();
        return createInstanceLambda();
    }

    private static Delegate GetWriteDelegateForType(Type type)
    {
        var handlerObj = GetHandlerForType(type);
        var methodInfo = handlerObj.GetType().GetMethod("Write", new[] { typeof(MemoryStream), type });

        var arg1 = Expression.Parameter(typeof(MemoryStream), "s");
        var arg2 = Expression.Parameter(type, "v");

        var handlerObjConstant = Expression.Constant(handlerObj);
        var methodCall = Expression.Call(handlerObjConstant, methodInfo, arg1, arg2);

        var lambda = Expression.Lambda(methodCall, arg1, arg2);

        return lambda.Compile();
    }
}

请注意,我没有对lambda生成进行基准测试,只调用了DynamicInvoke。

有没有办法用更快的东西替换DynamicInvoke?

更新:我评估了包含代码示例的3个答案,并选择与Lasse V. Karlsen一起回答因为简单。 (注意Grax的代码:尽管缓存了MakeGenericMethod调用,但它似乎比在委托中包装Invoke慢)

             Method |        Median |     StdDev |
------------------- |-------------- |----------- |
           MyLambda | 1,133.2459 ns | 25.1972 ns |
       ExplicitCall |     0.6450 ns |  0.0256 ns |
 Test2DelegateLasse |    10.6032 ns |  0.2141 ns |
         LambdaGroo |    10.7274 ns |  0.1099 ns |
         InvokeGrax |   349.9428 ns | 14.6841 ns |

执行此操作的方法是通过适当的泛型方法,将object转换包装到T ,并跳过整个动态调用。

从您在pastebin中的代码,这是Test类的新版本:

public class Test2
{
    private static readonly Action<MemoryStream, object> del;

    static Test2()
    {
        var genericCreateMethod = typeof(Test2).GetMethod("CreateWriteDelegate", BindingFlags.Static | BindingFlags.NonPublic);
        var specificCreateMethod = genericCreateMethod.MakeGenericMethod(typeof(Model));
        del = (Action<MemoryStream, object>)specificCreateMethod.Invoke(null, null);
    }

    public static void Write(MemoryStream s, object value)
    {
        del(s, value);
    }

    private static Action<MemoryStream, object> CreateWriteDelegate<T>()
    {
        var handler = HandlerFactory.Create<T>();
        return delegate (MemoryStream s, object value)
        {
            handler.Write(s, (T)value);
        };
    }
}

在我的机器上,您的代码与上面的代码一样执行:

你的测试:1285ms
我的测试:20ms
明确:4ms

编写泛型方法并使用Invoke的MakeGenericMethod来调用它。

将要调用的方法存储在静态变量中,以便GetMethod调用只需要发生一次。

然后在该MethodInfo上调用MakeGenericMethod并在结果上调用Invoke。

private static MethodInfo GenericWriteMethod =
    typeof(Test).GetMethod("GenericWrite", BindingFlags.NonPublic | BindingFlags.Static);

public static void Write(MemoryStream s, object value)
{
    GenericWriteMethod
        .MakeGenericMethod(value.GetType())
        .Invoke(null, new object[] { s, value });
}

private static void GenericWrite<T>(MemoryStream s, T value)
{
    HandlerFactory.Create<T>().Write(s, value);
}

在我的测试中,这使它快了100多倍。

您应该简单地创建一个Action<Stream, object>

static Action<Stream, object> GetWriteDelegateForType(Type type)
{
    // get the actual generic method
    var handlerObj = GetHandlerForType(type);
    var methodInfo = handlerObj
                .GetType()
                .GetMethod("Write", new[] { typeof(MemoryStream), type });

    // but use (Stream, object) parameters instead
    var streamArg = Expression.Parameter(typeof(Stream), "s");
    var objectArg = Expression.Parameter(typeof(object), "v");

    // this will cast object to T
    var tCast = Expression.Convert(objectArg, type);

    var handlerObjConstant = Expression.Constant(handlerObj);
    var body = Expression.Call(handlerObjConstant, methodInfo, streamArg, tCast);
    var lambda = Expression.Lambda<Action<Stream, object>>(body, streamArg, objectArg);

    // and compile to an actual Action<Stream, object>
    return lambda.Compile();
}

然后你就像一个普通的代表那样称呼它:

static void Write(MemoryStream s, object value)
{
    var action = GetWriteDelegateForType(value.GetType());
    action(s, value);
}

缓存委托也是个好主意:

static readonly ConcurrentDictionary<Type, Action<Stream, object>> _cache = 
   new ConcurrentDictionary<Type, Action<Stream, object>>();

static void Write(MemoryStream s, object value)
{
    var type = value.GetType();
    var action = _cache.GetOrAdd(type, GetWriteDelegateForType);
    action(s, value);
}

您可以将其设置为Action<MemoryStream, object>并使用Expression.Convert()v类型从object更改为type

为了获得性能提升,您可以将这些Action存储在某些Type -keyed字典中(并发?),但表达式需要进行一些更改才能Create其中的处理程序。

添加另一个答案,因为这个与我的第一个明显不同。

TLDR:添加非泛型接口,创建字典以缓存类型的处理程序,使用非泛型Write方法调用处理程序。


向ExplicitHandler添加非泛型接口,以便更容易与非泛型代码进行交互。

public interface IHandler
{
    void Write(Stream s, object v);
}

在ExplicitHandler中实现非泛型Write方法以强制转换为T并调用泛型Write

    void IHandler.Write(Stream s, object v)
    {
        Write(s, (T)v);
    }

将处理程序缓存在字典中

public class Test
{
    static Dictionary<Type, IHandler> delegates = new Dictionary<Type, IHandler>();

    public static void Write(MemoryStream s, object value)
    {
        IHandler handler;

        var type = value.GetType();
        if (!delegates.TryGetValue(type, out handler))
        {
            handler = (IHandler)typeof(HandlerFactory).GetMethod(nameof(HandlerFactory.Create)).MakeGenericMethod(type).Invoke(null, null);
            delegates[type] = handler;
        }

        handler.Write(s, value);
    }
}

在这里重写你的来源: http//pastebin.com/hmfj2Gv2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM