410 likes | 846 Views
Just-In-Time Compilation. Introduction. Just-in-time compilation ( JIT ), also known as dynamic translation , is a method to improve the runtime performance of computer programs.
E N D
Introduction • Just-in-time compilation (JIT), also known as dynamic translation, is a method to improve the runtime performance of computer programs. • Traditionally, computer programs had two modes of runtime operation, either interpreted or static (ahead-of-time) compilation. • JIT compilers represent a hybrid approach, with translation occurring continuously, as with interpreters, but with caching of translated code to minimize performance degradation.
Just-In-Time Compilation Functional Duties: • Class Loader • Resolves type references and loads assemblies into memory to be consumed by the verifier and JIT Compiler • JIT Compiler • Runtime component that compiles the MSIL stream into native code • Compares MSIL stream to metadata and verifies that the code is safe and legal (Verification)
JIT Options for Application Developers .NET provides two JIT compilers and an install-time JIT option: • A “normal” JIT compiler, designed to provide the expected optimization when compiling MSIL to native code • The same level of optimization you might expect from a C compiler • This makes the compilation process take longer than otherwise • Ngen.exe, MSIL code is “pre-JIT’ed” or “zapped” and the native image is cached for future use
Just-In-Time Compilation • IL must be compiled to native code in order to run • No files with *.il extensions involved • Multiple JIT compilers for differing processor architectures • Code compiled on an “as needed” basis • Method cached and subsequent calls are not recompiled • Can revert back to a stub using code pitching • Reduces memory footprint
Calling a method for the first time Console Shared Sub WriteLine() Shared Sub WriteLine(String) (remaining members) Managed EXE Shared Sub Main() Console.WriteLine(“Paul”) Console.WriteLine(“Cross”) End Sub Jitter Jitter Native code … MSCorEE.dll • Function Jitter • In the assembly that implements the type (Console), look up the method (WriteLine) being called in the metadata. • From the metadata, get the IL for this method. • Allocate a block of memory. • Compile the IL into native code; save the code in the memory allocated in step 3. • Modify the method’s entry in the Type’s table so that it now points to the memory block allocated in step 3. • Jump to the native code contained inside the memory block. • End Function
JIT Features • Platform Independence • Realized when high-level language compilers convert source code to platform agnostic MSIL code • The application or software component is distributed in this form • JIT compiles to native code either at runtime or at install time
JIT Features • Language Interoperability • Occurs when different language compilers compile to language-agnostic MSIL code • Metadata and the Common Type System play a major role in cross-language and platform independence
JIT Features • Runtime Stack Manipulation • The JIT Compiler populates important data structures for object tracking and specific stack-frame construction • The JIT Compiler can be used to identify specific code elements as they are consumed, i.e., exception handlers and security descriptors
JIT Optimizations • Small Memory Footprint • JIT compilation takes advantage of the possibility that some code may never be used • The JIT Compiler compiles methods only as needed
JIT Optimization • Contiguous Native Code Alignment • The runtime method is optimized for fast data access • The execution engine has the option of aligning native function images in memory • Lends for a smaller footprint • Speeds up CPU access to memory where code is stored
JIT Compilation must be slow • The “Nays”: • Unmanaged code is pre-compiled and can just execute • Managed code requires 2 compilation phases • Compiler produces IL • IL compiled to native code at runtime, requiring more memory to be allocated, and additional CPU cycles
JIT Compilation is actually pretty fast • Reasons why this may be the case: • JIT compiler knows more about the execution environment than an unmanaged compiler would know • JIT compiler can take advantage of instructions offered by the chip that the unmanaged compiler knows nothing about • JIT compiler could detect that a certain test is always false, and short-circuit • The CLR could profile the code’s execution and recompile the IL on the fly reducing branching, etc.
The End …… Thank You ……