. Net compilation technology insider

Source: Internet
Author: User
. Net compilation technology insider
Author: builder.comWww.aspcool.comTime: 14:07:34 views:5221


As a code instruction platform, Microsoft. NET is more complex than other technical platforms launched by Microsoft. As. NET provides support for multiple programming languages and (theoretically) multiple platforms, an intermediate code layer needs to be added to the traditional two code layers. Here, the traditional two layers are the source code layer and the compiled local code layer. The newly added code layer adds extra flexibility to the. NET platform, but in turn increases the complexity of the system. In addition, due to the emergence of this new code layer, a series of new application deployment options were first presented to programmers. The main purpose of this article is to guide the developers of. Net Applications to understand the. NET code compilation process and to understand additional compilation options.

. Net: msil
In the Microsoft. NET Framework, applications can be written and created using a variety of advanced programming languages, such as VB. NET, C #, and Cobol. net. And through every kind of compliance. the program code written by the standard programming language of. NET must first be changed from the source code to an initial compilation step. net public standard language: msil (Microsoft intermediary language: Microsoft intermediate language ). Msil itself is a complete and Object-related language, and it is only possible to create an application. For more information about msil, see "Understanding the operating principles of CLR in msil .. NET applications appear in the form of msil. They are compiled as local code through the real-time Compiler (JIT) only during program execution. Figure A illustrates this process.

Figure



. Net compilation process: from source code to local commands


As long as the assembly code is loaded, JIT compilation is performed. It can be seen that this is an assembly-level compilation. (For more information about the assembly technology, see the recipe for "assembly-cure" DLL hell?" Article ). During the compilation process, the JIT compiler will load the corresponding program matching each method declaration of the object once the index of the object is encountered for the first time. In this way, the Il will be compiled when the method is called, and the corresponding root program of the method will be replaced by the address of the compiled code of the method. This process is performed when the method is called for the first time. The generated native code is buffered so that it can be used when the assembly code is loaded for the next time during the session. Obviously, such a command system requires more processing capability than traditional compilation languages, but its requirements are not as high as you think.

Here, we must clarify a general misunderstanding of the concept that many people think that. NET applications are interpreted rather than compiled programs. In addition, there is a common misunderstanding: the JIT compiled code is stored on the disk and can be executed for the same application. Although this is not impossible, you will soon understand that this is not the default compilation solution. The application's il code is actually re-compiled as the local code every time the application is running.

Two Compilers
In fact, JIT compilers are divided into two types (economical compilers and common compilers), and they are not born equal. The economical JIT compiler represents running. NET applications require minimal functionality. It directly replaces each msil command with equivalent local code without any optimization, resulting in less system load. This also means that it is mainly used on platforms with relatively tight memory and other resources.

On the other hand, the general JIT compiler is the default runtime configuration, which will optimize the generated code in real time. In this way, it gives. Net an advantage over the traditional pre-compilation language: the pre-compilation language can only make a rough pre-estimation of the code it processes on the platform on which it will run. The JIT compiler can be accurately adjusted to the current running state, and the results can complete some tasks that cannot be completed by the Pre-compiled language:

More efficient use and allocation of CPU registers
Low-level code optimization is implemented as appropriate, such as constant overlap, copy Copy, cancel range check, cancel regular secondary expressions, and inline method.
Monitor the current physical and virtual memory requirements during code execution to make more efficient use of memory
Generate specific platform commands to make full use of the actual processor mode accurately

The result of. Net compilation is that the extra load requirements brought by JIT do not produce significant performance loss.

Performance options
This means that msil is compiled by JIT every time an application is run. Remember, this is common sense. Then, based on the principles described above, starting an application and using non-core features for the first time will clearly lead to lower-than-optimized system performance. So what measures should you take to minimize this negative impact?

Microsoft's countermeasure is to provide us with a compiler called pre-jit (also called Native image generator, program name is ngen.exe ). On the surface, at least it is also a cure for any performance problems. The pre-JIT compiler is called before runtime. During installation, it compiles all msil in Assembly form into local code. This kind of local code is subsequently stored in a special part of the Global Assembly Cache for later use, thus completely bypassing the JIT compilation process.

At first glance, this should solve the previous problems, especially for client code. But do you still remember? Common JIT implements a large number of instant optimization operations when compiling msil. Many such optimization operations, especially those involving register and memory usage, are driven by the current requirements of the system. Therefore, the bulk compilation of assembly code will stop these optimizations and generate final code that runs slower. Before you adopt this method, Microsoft recommends that you set your JIT and ngen versions to the same Assembly level on the target platform based on the current condition of normal compilation.

Unless you are an avid fan of Java, the. NET runtime behavior and compiler will be completely different from the same situation you have known before. But they are not mysterious. I hope that I can clarify some questions that have puzzled you.

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.