Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiled Expression Trees misunderstanding?

I have this expression :

Expression<Func<string, bool>> f = s => s.Length < 5;

enter image description here

ParameterExpression p = Expression.Parameter (typeof (string), "s");
MemberExpression stringLength = Expression.Property (p, "Length");
ConstantExpression five = Expression.Constant (5);
BinaryExpression comparison = Expression.LessThan (stringLength, five);
Expression<Func<string, bool>> lambda= Expression.Lambda<Func<string, bool>> (comparison, p);

//lets : test

Func<string, bool> runnable = lambda.Compile();
Console.WriteLine (runnable ("kangaroo")); // False
Console.WriteLine (runnable ("dog")); //True

I want to ask about the .Compile()

What does it compile ? And what is the difference between the first execution vs later executions...?

Compile should be something that happens once and not happens again later ....

What / How does it help me ?

like image 677
Royi Namir Avatar asked Feb 26 '12 13:02

Royi Namir


People also ask

Are expression trees slow?

Expression tree compilation for example has a built in interpreter so it still "works". The problem is that its often slower than just using reflection, especially since Native AOT has "faster reflection".

What is an expression tree and how is it useful?

When you want to have a richer interaction, you need to use Expression Trees. Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.

Why do we need expression tree?

Expression trees are often used to generate code dynamically at runtime. Lets say that you have to create lots of unknown type insatnces. You could create them using reflection and you'll suffer from poor performance, or you can create an expression tree and compile it to a method.

What is the role of expression trees in LINQ?

You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to use expression trees to build dynamic queries (C#).


2 Answers

When you are building the expression tree at runtime there's no code emitted. It's a way to represent .NET code at runtime.

Once you call the .Compile method on the expression tree the actual IL code is emitted to convert this expression tree into a delegate (Func<string, bool> in your case) that you could invoke at runtime. So the code that this expression tree represents can be executed only after you compile it.

Calling Compile is an expensive operation. Basically you should be calling it once and then caching the resulting delegate that you could use to invoke the code many times.

like image 188
Darin Dimitrov Avatar answered Sep 22 '22 04:09

Darin Dimitrov


The Expression<Func<string,bool>> is only a representation of an expression, it cannot be executed. Calling Compile() gives you a compiled delegate, a piece of code that you can call. Essentially, your program composes a small code snippet at runtime, and then call it as if it were processed by the compiler. This is what the last two lines of your code do: as you can see, the compiled snippet can analyze the length of the string that you pass in - when the length is less than five, you get a True back; when it's five or more, you get a False.

What happens on first execution of the compiled snippet is platform-dependent, and should not be detectable by programmers using the .NET platform.

like image 35
Sergey Kalinichenko Avatar answered Sep 23 '22 04:09

Sergey Kalinichenko