. Net Discovery series 5-in-depth introduction to. net real-time compilation mechanism (I)

Source: Internet
Author: User
Tags hosting

Welcome to the ". Net Discovery series"ArticleIn this article, I will explain the. NET JIT knowledge in two parts.

 

JIT (just in time) is. net is a mechanism for compiling while running. This mechanism is named by a production method implemented by Toyota in 1960s ".

. Net jit compiler, in terms of its original design intention and running method, is very similar to the "punctual production" system of Toyota Motor, so let's first understand it through "punctual production. net jit mechanism.

The basic idea of "punctual production" can be summarized as "the products required for mass production as needed", which is exactly the same. net jit compiler is designed to meet the needs of compilation.Code.

 

Section 1.Me JIT

Taking C # as an example, C # code is usually compiled twice before it is run. The first stage is the compilation of C # code to msil, and the second stage is the compilation of Il to local code. The compilation result of the first stage is to generate the hosting module, and the compilation result of the second stage is to generate local code for running. As you can see here, the msil generated in the first stage cannot be directly run.

Here we need to explain what msil and managed modules are.

Msil:

Microsoft intermediate language (msil) is a pseudo-assembly language between a high-level language and an assembly language, you don't have to be excited if you have different opinions ). When you compile and run a. netProgramThe advanced language compiler willSource codeTranslate into a group of CPU-independent commands.

It can be seen that IL includes commands for loading (ldstr), storage (pressure stack, bullet stack), initialization object (locals), and call object method (CALL, it also includes commands for arithmetic and logical operations, control flow, Direct Memory Access, exception handling, and other operations.

C # code:

  StringStr_test= "Test";
System. String str_test= "Test";

Corresponding il code:

    //  Code size 14 (0xe)  
. Maxstack 1
. Locals Init ([ 0 ] String Str_test,
[ 1 ] String Str_test)
Il_0000: NOP
Il_0001: ldstr " Test "
Il_0006: stloc. 0
Il_0007: ldstr " Test "
Il_000c: stloc. 1
Il_000d: Ret

Hosting module:

The managed module is a standard 32-bit or 64-bit Microsoft Windows portable executable body (pe32 or pe32 +) file. The managed module must be executed by CLR, it contains the above-mentioned il code and metadata, PE Header, and CLR header.

Metadata (metadata) can be understood as a hashtable. The table maps built-in types and members, as well as referenced types and members. These types and members are used by IL, therefore, metadata always needs to be associated with the corresponding il code. The Compiler also generates metadata and IL at the same time to ensure self-description synchronization.

The PE Header (portable executable) includes pe32 and pe32 +, indicating the runtime environment of the hosting module and the information required for JIT to optimize local code, this will be discussed later.

The CLR header mainly includes the entry address mark of the method, as well as information such as resources and strong naming. This information is an important parameter basis of GAC.

It indicates the JIT Intervention time:

 

Figure 1 JIT working time

JIT is an important module in runtime. It converts Il to a local CPU command. As you can see, you may not believe that the real-time compilation process occurs at runtime, will this affect performance? In fact, the answer is yes, but this overhead is worth the money:

    1. The performance overhead caused by JIT is not significant.
    2. JIT follows two classic theories in Computer System Theory: Local principle and 8020 principle. The Locality Principle points out that the program always tends to use recently used data and commands, which include space and time. It can be derived from the Locality Principle, programs tend to use recently used data and commands, as well as the data and commands that are currently in use (by impressions, but without misinterpreting the original intention ); the 8020 principle points out that the system always spends 80% of its time executing the 20% code.Based on these two principles, JIT will optimize the code in real time before and after the runtime, so that only the work can be done at the runtime.
    3. JIT only compiles the required code, not all, which saves unnecessary memory overhead.
    4. JIT optimizes the Il code based on the runtime environment, that is, the same il code runs on different CPUs, and the Local Code Compiled by JIT is different, these different codes are optimized for your CPU.
    5. JIT checks the running status of the code and recompiles the special code to optimize the code.

In fact, JIT has more than this advantage (. Net Discovery series 4-deep understanding of. Net garbage collection mechanism (II)Including the description of the policy engine), CLR feedback, code collection (non-spam collection, which will be introduced in section 2) and other aspects will have an indelible contribution.

It must be noted that after JIT compiles il for the first time, it will modify the memory address entry corresponding to the corresponding method (bypass ~~), The next time you need to execute this method, CLR will directly access the corresponding memory address without passing through JIT.

 

Section 2. Compilation and execution

In the previous section, we discussed some jit-related elements and JIT advantages. This section will focus on the compilation principles of JIT.

C # and other advanced languages must be compiled into il before execution. Before Execution, il must be considered as local code to run. There are two ways to obtain local code, JIT Mode and native image generator mode. This section mainly discusses JIT Mode.

It must be noted that after JIT compiles il for the first time, it will modify the corresponding method's memory address entry. The CLR will directly access the corresponding memory address the next time it needs to execute this method, without passing through JIT, this will undoubtedly speed up the program running. What kind of process is this?

Take the load () method as an example. If the load () method calls two methods of the same type:

 

  Void load ()

{

A. A1 ( " First " );

A. A1 ( " Second " );

}
Static Class A

{

Public Void A1 ( String Str ){}

Public Void A2 ( String Str ){}

Public Void A3 ( String Str ){}

}

 

 During running, the operating system loads the corresponding runtime framework based on various header information in the managed module. Load () is loaded. because it is the first time to load () JIT will detect all types referenced in load (), combine the metadata to traverse all the methods defined in these types for implementation, and use a special hashtable (only for understanding) store the entry addresses corresponding to these types of methods (this entry address is a pre-compiled proxy (prejitstub) before JIT is passed, which triggers JIT compilation). Based on these addresses, you can find the corresponding method implementation.

 

To be continued

I amAicken)Please keep an eye on my next article.

 

". Net Discovery series" is recommended:

. Net Discovery series 3-deep understanding of. Net garbage collection mechanism (I)

. Net Discovery series 4-deep understanding of. Net garbage collection mechanism (II)

One of the. NET Discovery series-string from entry to mastery (on)

. Net Discovery series II -- string from entry to mastery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.