Improve your application startup time using multicore JIT

Take advantage of multicore JIT to reduce the application's startup time

Improve your application startup time using multicore JIT

The Just in Time (JIT) compiler is a component of the .Net Framework that converts the intermediate compiled code (MSIL) to machine code that can be executed by the host operating system. The JIT compilation does have some overhead though when executed with one system that has a single processor. With the advent of multicore systems and Microsoft’s .Net Framework 4.5, you have support for multicore JIT compilation. This new version of the .Net framework leverages the multiple cores in your system by allowing the JIT compilation to run in two processors in parallel -- hence, reducing the compilation time. This also reduces the application startup time considerably; we will explore more on this as we progress through this post. It is a great new feature that can provide an appreciable performance boost. This article discusses in depth how multicore JIT compilation works.

As discussed, multicore JIT is a new feature added to .Net framework 4.5 that helps you to have the methods compile in two cores in parallel. This helps boost the application's performance by reducing the application startup time considerably. Before we explore further on multicore JIT, let's take a quick tour of what all we need to get started.

To work with multicore JIT compiler, you should have the following available:

  1. A multicore system (the system should have a minimum of two cores) with recommended memory installed
  2. .Net Framework 4.5 or later installed in the system

You would need to call the methods of the ProfileOptimization class to configure the folder in the disk where the profiling data needs to be stored.

Let's understand how multicore JIT works with a real-time example. Assume you have two methods method1 and method2 that have to be executed sequentially. If you are not using multicore JIT, the runtime would need to have to JIT compile the methods one after the other to execute them. If method1 is in execution, the runtime would need to stop after execution of method1 is over, JIT compile the next method at the time when the next method is to be executed (method2 in our example) before it can resume execution. With multicore JIT, the runtime need not stop -- it would JIT compile the methods that are to be executed in parallel. This would ensure there aren't any stoppages and hence the application would be more responsive and have improved performance.

The new versions of the CLR (from .Net Framework 4.5 and beyond) leverage the multicore processors to speed up the compilation process. The multicore JIT compiler improves the performance by paralyzing some of the JIT compilations at application start-up. Multicore JIT compilation can work only on systems that have multiple processors. Note that multicore JIT works in two modes: the recording mode and the playback mode. Let's understand how both of them work.

In the recording mode of operation, the multicore JIT compiler records all the methods it needs to compile. Note that the recording mode works only once; for all subsequent calls to the multicore JIT compiler the playback mode is used. The runtime stores a profile of all the methods that have been executed in the disk. In the playback mode of operation, the runtime loads the saved profile (it was saved in the recording mode of operation) and the metadata stored in the profile is then used to JIT compile methods in the background. In essence, the methods are JIT compiled before they are asked for -- this ensures that the main thread doesn't have to do much compilation and the application loads much faster.

Note that multicore JIT compilation is enabled by default for ASP.Net 4.5 and Silverlight 5 applications that run on .Net Framework 4.5 and beyond. However, you may turn this feature off if you want to, by using the compilation option in the application's web.config file as shown below.


  <compilation profileGuidedOptimizations = "None" />



To enable multicore JIT (when using windows applications or console applications) you can use the following statements at the entry point of your application (typically the main() method if you are using a console application).



While the SetProfileRoot() method of the ProfileOptimization class sets the path where the profile information would be stored, the StartProfile() method starts the multicore JIT compilation for you.You can take a look at this MSDN article to learn more about profile optimization:

I will discuss multicore JIT more in my future posts here.