. NET Code Compilation process

Source: Internet
Author: User

As a platform for code directives, Microsoft. NET is more complex than other technology platforms that Microsoft has previously launched. Because. NET provides support for multiple programming languages and (theoretically) multiple platforms, which requires the addition of an intermediate code layer at the traditional two code layers. Here, the traditional two tiers are the source code layer and the compiled native code layer, respectively. The newly added code layer gives. NET platform provides additional flexibility, but in turn adds complexity to the system. In addition, with the advent of this new code layer, a series of novel application deployment options have been presented to the programmer for the first time.

. NET difference: MSIL

at Microsoft. NET framework, applications can be written and created in several high-level programming languages, such as vb.net, C #, or COBOL. NET and so on can write. NET applications. And through every kind of observance. The programming language of the net specification first has to be a common standard language for. NET from source code through an initial compilation step: MSIL (Microsoft Intermediary Language: Microsoft intermediate Language). MSIL itself is a complete, object-dependent language, and only it is possible to create an application. To get an overview of some of the MSIL, you can see the article "Understanding the workings of the CLR through the MSIL language". NET applications occur in the form of MSIL and are compiled into native code by the instant compiler (JIT) only when the program executes.

. NET compilation process: From source code to native instruction

As long as the Assembly code is loaded and JIT-compiled, this is a compilation-level compilation (see the "assembly-Cure" DLL Hell "for more details on Assembly technology). "article). During the compilation process, the JIT compiler will load the corresponding program for each method declaration of the matching object once it encounters the object's index for the first time. In this way, the IL is compiled when the method is called later, and the corresponding root program of the method is replaced by the address of the compiled code of the method. This process occurs each time the method is called for the first time, and the resulting native code is buffered so that it can be used the next time the assembly code is loaded during the session. Obviously, such a command system requires much more processing power than a traditional compiler language, but its requirements are not as high as you might think.

It is important to clarify the erroneous concept of a common misconception, which is that many people think that. NET application is an interpreted rather than a compiled program. In addition, there are common misconceptions: JIT-compiled code is stored on disk and can be executed for the same application. While this is not an option, you will soon understand that this is not the default compilation scenario. The IL code of the application is actually recompiled to native code every time the application is run.

Two types of compilers

In fact, the JIT compiler is divided into two types (economic compilers and ordinary compilers), and they are not inherently equal. The economic JIT compiler represents the minimal functionality required to run a. NET application, replacing each MSIL instruction directly with the native code of the peer, without any optimizations and thus resulting in less system load. This also means that it is mainly used in memory and other resources on a more tense platform.

On the other hand, the normal JIT compiler is the default runtime configuration, which instantly optimizes the code it produces. Doing so invisibly gives. NET is one of the advantages of a traditional precompiled language: The precompiled language can only make a rough estimate of the platform on which the code it handles will run on. The JIT compiler can be accurately adjusted to reach the current run-time state, and the results can be accomplished by some precompiled languages:

• More efficient use and allocation of CPU registers
• Implement low-level code optimizations where appropriate, such as constant overlap, copy copying, canceling range checking, canceling regular secondary expressions, and method inline
• Monitor current physical and virtual memory requirements during code execution to make more efficient use of memory
• Generate specific platform directives to accurately and fully utilize the actual processor pattern

. NET compilation results in the additional load requirements that JIT brings without significant performance penalty.

Performance Options

This means that MSIL is JIT-compiled each time the application is run. Remember, this is common sense, and then, based on the principles described in the above, it is clear that the performance of performance is lower than the optimization level when starting the application and first using non-core functionality. So what are you going to do to minimize this negative impact?

Microsoft's response is to provide us with a compiler called Pre-jit (also known as the native Image generator: Native image Generator, the program name is therefore Ngen.exe). On the surface, at least it is a treatment for any performance problem. The Pre-jit compiler is called before run time, and when installed, it compiles all assembly forms of MSIL into native code. This native code is then stored in a special section of the global Assembly cache (Assembly) for later use, bypassing the JIT compilation process completely.

At first glance, this should be the solution to the previous problem, especially for client code. But, do you remember? The normal JIT implements a lot of immediate optimization operations when compiling MSIL. Many of these optimizations, especially those involving registers and memory usage, are driven by the current requirements of the system. As a result, the bulk compilation of assembly code prevents these optimizations from being made and actually produces the final code that runs slower. Before you do this, Microsoft's proposal is to set your JIT and NGen versions to the same assembly level on the target platform, in comparison to the current conditions under normal compilation.

. NET Code Compilation process

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.