当一个表达式来; T>是编译的,是它隐含缓存? [英] When an Expression<T> is compiled, is it implicitly cached?

查看:164
本文介绍了当一个表达式来; T>是编译的,是它隐含缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个表达式来; T> 编译,是由框架隐式缓存生成的代码?我在静态的思维行正则表达式方法,其中隐含的框架编译和缓存最后几个正则表达式。

When an Expression<T> is compiled, is the resultant code implicitly cached by the framework? I'm thinking along the lines of the static Regex methods where the framework implicitly compiles and caches the last few regexes.

如果编译表达式来; T> 对象的的缓存,可以推荐为保持编译时了一些最佳做法或任何陷阱的可能会引起问题,如果我手动缓存的表达?

If compiled Expression<T> objects are not cached, can you recommend some best practices for keeping the compile-time down or any gotchas that could cause problems if I manually cache an expression?

public MyResultType DoSomething(int arg1, int arg2)
{
    var result = invokeHandler(
        (IDoSomethingHandler h) => h.DoSomething(arg1, arg2)
    );
    return result;
}

private TResult invokeHandler<T, TResult>(Expression<Func<T, TResult>> action)
    where T : class
{
    // Here, I might want to check to see if action is already cached.

    var compiledAction = action.Compile();
    var methodCallExpr = action as MethodCallExpression;

    // Here, I might want to store methodCallExpr in a cache somewhere.

    var handler = ServiceLocator.Current.GetInstance<T>();
    var result = compiledAction(handler);

    return result;
}

在这个例子中,我稍微担心,如果我缓存编译表达,它会使用 ARG1 ARG2 ,因为他们当时的表情被编译,而不是值在堆栈中检索从适当位置的值(即,而不是获取当前值)

In this example, I'm slightly concerned that if I cache the compiled expression, that it will use the values of arg1 and arg2 as they were at the time the expression was compiled, rather than retrieving those values from the appropriate place in the stack (i.e. rather than getting the current values).

推荐答案

没有;我不相信这是;如果你想让它的缓存,你必须守住代表引用(典型值 Func键< ...> 动作< ...> )。同样,如果你想获得最佳的性能,你会编译为一个参数化的表情,这样你就可以在不同的值,当你调用它发送。

No; I do not believe that it is; if you want it cached, you must hold onto the Delegate reference (typically Func<...> or Action<...>). Likewise, if you want to get the best performance, you would compile it as a parameterised expression, so you can send in different values when you invoke it.

在这种情况下, ,重新措辞将有助于:

In this case, re-phrasing would help:

public MyResultType DoSomething(int arg1, int arg2)
{
    var result = invokeHandler(
        (IDoSomethingHandler h, int a1, int a2) => h.DoSomething(a1, a2),
        arg1, arg2);
    return result;
}

private TResult invokeHandler<T, TResult>(Expression<Func<T,int,int,TResult>> action,
    int arg1, int arg2)
    where T : class
{
    // Here, I might want to check to see if action is already cached.

    var compiledAction = action.Compile();
    var methodCallExpr = action as MethodCallExpression;

    // Here, I might want to store methodCallExpr in a cache somewhere.

    var handler = ServiceLocator.Current.GetInstance<T>();
    var result = compiledAction(handler, arg1, arg2);

    return result;
}



即。使表达式的数量的参数,并通过<青霉>实际的那些它在运行时(而不是在表达式的常量)。

i.e. make the numbers parameters of the expression, and pass the actual ones it at runtime (rather than being constants in the expression).

这篇关于当一个表达式来; T&GT;是编译的,是它隐含缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆