views:

356

answers:

3

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.

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;
}

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).

+2  A: 

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).

Marc Gravell
A: 

Lambda experssions are not cached automatically. You will need to implement you own caching/memoization algorithms for that. Check the related Stackoverflow question:

http://stackoverflow.com/questions/66382/is-it-possible-to-cache-a-value-evaluated-in-a-lambda-expression-c-linq

It is important to note that lambda expressions are lazy evaluated in C#.

DrJokepu
A: 

It's not cached but you can cache it yourself based on expression string representation:

static class FuncCache<T>
{
    static readonly Dictionary<string, Func<T>> _funcCache = new Dictionary<string, Func<T>>();

    public static Func<T> GetFunc(Expression<Func<T>> accessor)
    {
        Func<T> func;

        var key = accessor.ToString();

        if (!_funcCache.TryGetValue(key, out func))
        {
            func = accessor.Compile();
            _funcCache[key] = func;
        }

        return func;
    }
}
Konstantin Spirin