When compiling any. NET application, the compiler converts the source code to Microsoft intermediate language (MSIL ). MSIL is not executed on any specific processor. In fact, MSIL is designed to be independent of the processor. To execute an application in a specific environment, a JIT compiler accepts the MSIL output and converts the commands to the native commands of the host processor. the JIT compiler compiles the code in real time, so that the application starts to run.
Unlike traditional compilers, such as C ++ compilers, JIT compilers have so much time for optimization. Unlike traditional compilers running on machines during development, the end user is waiting for the JIT compiler to complete so that the program can run. The JIT compiler still takes time to perform a certain amount of optimization. It only compiles a few small parts of the program at a time. Whenever the execution reaches a method in the Assembly that has not been compiled by JIT, the JIT compiler only compiles the method currently waiting for execution, and saves the result for future calls to the same method, knowing that the application is over. This technology means that applications will never waste Compilation Time on unused methods.
The JIT compiler performs some optimization when generating local commands, including clearing sub-expressions, expanding loops, and inline methods. These optimizations are built-in. NET optimizations that we cannot control.
Inlining is an optimization that the compiler uses to call a method in place of its own instructions. The compiler concatenates a method when it sees a chance to significantly reduce the overhead. A small method is the best inline object, because the overhead of stack frame settings, branches, and return is often greater than the amount of code in the method.
Compilation before execution also has some advantages. The compiler will understand the specific characteristics of the runtime environment and provide specific instructions for the host CPU. The compiler can greatly optimize the memory on a RAM-limited platform, and use the processing capability of faster machines to optimize the speed.
There are many opportunities to optimize programs from source code to MSIL and then to the local code of the host CPU.