L
L
ldmitriy2020-05-17 13:12:49
C++ / C#
ldmitriy, 2020-05-17 13:12:49

How does .NET work?

When I click build project (green arrow) in Visual Studio, what happens?
I understood this: the compiler converts the code into an intermediate byte code, which is called CIL (MSIL). This code is executed by the CLR, which includes the JIT compiler, into native code. It turns out exe.

Does CLR not include CIL?
Does the JIT run every time the project is built, or just once to remember the architecture of the platform?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ayazer, 2020-05-17
@ldmitriy

the code turns into IL, which will already turn into machine language as needed (see JIT compilation). Those. in fact, the first method call will take a little longer. IL code will be optimized and compiled for a real hardware platform. After that, all subsequent calls will be executed faster. If for some reason this option is not suitable, there is ngen that allows you to immediately compile the code for a known platform (winning on cold launches, but losing portability). And in this case, jit compilation will not be performed while the application is running.

Does CLR not include CIL?

did not quite understand the question. cil/msil/il - just an intermediate one into which C#/F#/VN.NET/etc code turns into. It can be regarded as an assembler for .net. Moreover, no one forbids writing your own DSL, which will generate IL, which in turn will work wherever it works. no
Does the JIT run every time the project is built, or just once to remember the architecture of the platform?

both options are not. jit compilation = just-in-time and runs right while the application is running. once it turns the IL code into the desired set of machine codes.

V
Vasily Bannikov, 2021-02-01
@vabka

When you press the "green button" the following happens:

  1. Visual studio calls .NET SDK
  2. .NET SDK calls msbuild
  3. msbuild calls csc.exe at some point (C# Compiler)
  4. csc.exe generates IL (aka MSIL, aka CIL) and pushes it into DLLs
  5. ^---- This happens when building the project
  6. <-- At this point, on the .net framework, it was possible to set Ngen and skip the JIT compilation part
  7. V---- And this is every time you start
  8. The .NET Runtime then takes these assemblies and passes the IL code to the JIT compiler
  9. While JIT in the background compiles IL to machine code - runtime interprets IL
  10. When IL is compiled - runtime takes the generated machine code
  11. At the same time, in the background, JIT can collect information about which methods are called more often in order to further optimize them.

Even during the assembly of the project, you can pre-set the JIT on the code.
At startup, this will allow you to skip the interpretation step and immediately run the executable code.
This feature is called ReadyToRun Compilation
Here, in one of the answers, CLR via C # was advised - I think that this is not the most relevant book, as much has changed / added to .NET Core and nuances may differ.
If you want to learn more about JIT, start with this report:
https://www.youtube.com/watch?v=H1ksFnLjLoY

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question