Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to start using IL over higher languages [closed]

Tags:

c#

.net

il

After seeing a few open source projects most by the stack exchange team. I've noticed that a few times they just write straight IL code inlined in the c# function.

A perfect example you can see in a dapper file.

My guess is that the compiler doesn't generate the code that is felt to be the most efficient and sometimes you just need to do it for the compiler.

My curiosity lies in when does the decision to start using the IL emitter over regular old C#?

I know the examples are for stackexchange but I'm gussing other developers have had to make this decision seeing as Sigil exists.

like image 663
dbarnes Avatar asked Jan 08 '15 20:01

dbarnes


People also ask

What is the optimum age to start learning a second language?

According to this study, the best age to start learning a second language was at around 11-13 years, when the brain was further developed.

Are regular languages closed under reversal?

4.2: The family of regular languages is closed under reversal.

How do you show that a class of languages is closed?

Closure property is a technique to understand the class of the resulting language when we are performing an operation on two languages of the same class. That means, suppose L1 and L2 belong to regular language and if regular language is closed under operation ∪, then L1∪L2 will be a Regular language.

Are languages closed under intersection?

Regular Languages are closed under intersection, i.e., if L1 and L2 are regular then L1 ∩ L2 is also regular.


1 Answers

.NET IL is so simple compared to actual CPU assembly languages (x86, etc) that there is only one way to do pretty much everything. Meaning, you can't achieve better performance with better IL, because there is no better IL. (Let's assume you don't generate suboptimal IL intentionally, which a debug build does.)

.NET performance depends a lot more on the JIT-ter. Anyway, the whole idea of a high-level language, JIT-ting, GC-ing, etc,. has its performance tolls, so slight optimizations in IL don't matter too much anyway.

So why do people use IL? It's when you generate code (like when you compile an XSLT, a regular expression, or a dynamic proxy). It's a whole lot faster to do in IL than creating a C# code in a string buffer and compiling that, and that's all.

Apart from the ability to generate code on the fly, when IL is a must, I don't believe IL has any other advantages. C# is such a feature rich and fast evolving language, it would be just crazy not using it. Having to work in IL would be a punishment. You can rest assured that C# can be compiled to as good IL as possible.

You can also feel free to use all kinds of the newest C# features. I agree, things like dynamic may not have the best performance, but it's not about performance, it's about modern features which just help you get stuff done. IL doesn't give you that. (Of course, some new features really help performance, like async, but in very different ways than assembly-level optimizations, on a much higher level.)

And by the way, AFAIK you can not embed IL inside a C# file. Apart from generating new code on the fly, you cannot compile IL into your assembly, at least not in Visual Studio, except with certain tricks involving changing assemblies after they've been built from C#.

like image 90
fejesjoco Avatar answered Sep 30 '22 10:09

fejesjoco