When to start using IL over higher languages

After looking at a few open source projects, most of the stack exchange team. I noticed that several times they just write direct IL code built into the C # function.

A great example that you can see in the dapper file.

My assumption is that the compiler does not generate the code that is considered the most efficient, and sometimes you just need to do this for the compiler.

My curiosity is when does it decide to start using the IL emitter over the usual old C #?

I know the examples are for stackexchange, but I am kidding other developers so that this solution is seen as Sigil .

+5
source share
1 answer

.NET IL is so simple compared to the actual processor assembly languages ​​(x86, etc.) that there is only one way to do almost everything. So, you cannot achieve better performance with better IL, because there is no better IL. (Suppose you do not arbitrarily generate a suboptimal IL that the debug build does.)

.NET performance is much more dependent on JIT-ter. Anyway, the whole idea of ​​a high-level language, JIT-ting, GC-ing, etc. has its own tariffs for performance, so a little optimization in the IL does not matter, anyway.

So why do people use IL? This is when you generate code (for example, when compiling XSLT, regular expression, or dynamic proxy). This is much faster to do in IL than creating C # code in a line buffer and compiling it and that’s it.

Besides being able to generate code on the fly when an IL is required, I don’t think that IL has other advantages. C # is such a rich and fast-paced language, it is just crazy without using it. Work in the IL would be a punishment. You can be sure that C # can be compiled as good a IL as possible.

You can also freely use all kinds of the latest C # features. I agree, things like dynamics may not have better performance, but it's not about performance, but about modern features that just help you do something useful. IL does not give you this. (Of course, some of the new features really help performance, such as async, but in many ways than optimization at the assembly level, at a much higher level.)

And by the way, AFAIK you cannot embed IL inside a C # file. Besides generating new code on the fly, you cannot compile IL into your assembly, at least not in Visual Studio, with the exception of some tricks associated with changing the assembly after they were created from C #.

+9
source

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


All Articles