What does the baking method mean?

I read this post here in micro ORM used for SO.

The author showed this stack trace:

System.Reflection.Emit.DynamicMethod.CreateDelegate System.Data.Linq.SqlClient.ObjectReaderCompiler.Compile System.Data.Linq.SqlClient.SqlProvider.GetReaderFactory System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Compile System.Data.Linq.CommonDataServices+DeferredSourceFactory`1.ExecuteKeyQuery System.Data.Linq.CommonDataServices+DeferredSourceFactory`1.Execute System.Linq.Enumerable.SingleOrDefault System.Data.Linq.EntityRef`1.get_Entity 

Then it is said:

In the above trace, you can see that the “EntityRef” bakes a method that is not a problem unless this happens 100 seconds per second.

Can someone explain the stack trace in relation to what he meant by the “baking method” and why would this be a performance issue?

+6
source share
3 answers

When you do something like:

 int[] items = whatever; IEnumerable<int> query = from item in items where item % 2 == 0 select item; 

then the compiler will turn this into something like:

 static bool Predicate(int item) { return item % 2 == 0; } ... IEnumerable<int> query = Enumerable.Where<int>(items, new Func<int, bool> (Predicate)); 

That is, the compiler generates IL for this method. When you create a query object at runtime, the object returned by Where holds the delegate in the predicate and, if necessary, executes the predicate.

But you can create an IL for the delegate at run time if you want. What you do is save the predicate body as an expression tree. At run time, the expression tree can be dynamically compiled into a new IL; basically, we run a very simplified compiler that knows how to generate IL for expression trees. Thus, you can change the details of the predicate at run time and recompile the predicate without recompiling the entire program.

The author of this comment uses "baking" as a slang for "generating dynamic light code."

+10
source

It refers to the dynamic creation of a method at runtime, for example using an expression tree. Compiling an expression tree to a method and returning a delegate to a compiled method can be called "baking" the method.

+1
source

This is a fantastic way of saying that they emit dynamic code using Refrection.Emit. Slow process is known.

0
source

Source: https://habr.com/ru/post/909023/


All Articles