Multi-core JIT compilation in .NET 4.5


Historically, .NET developers have used the Ngen native image generator. This works great if you have an installer, and you can generate these images during the installation of the application. But in other cases, for example, when you do not have an installer or you do not have access to Ngen, your application will JIT compile as needed, which will slow down its loading. CLR developers have provided a solution in the new version of .NET - a multi-core JIT compilation with the ability to create optimization profiles.

In multi-core JIT compilation, the code is compiled on two cores in parallel. The more code you execute at boot time, the more noticeable will be the acceleration from multi-core JIT. You can get a 20-50% reduction in download time, which is good news for those who develop large applications and are not able to use Ngen. To do this, you need to add a couple of lines of code to your code.

How it works


In general, you only need to call two methods in your application:
  • Indicate the location where multi-core JIT profiles will be stored,
  • And start profiling by calling the StartProfile method.

.NET runtime will create a profile based on the sequence of methods invoked while your application is running. This profile will be saved to disk. Simply put, the first time you start “recording” the profile (the pictures are taken from here ):


When the application is launched the next time, .NET runtime will check for the presence of the finished profile and, finding it, will begin JIT compilation in the sequence in which they were saved to profile.


Using multi-core JIT compilation


Using multi-core JIT compilation is easy. In .NET 4.5, there is the System.Runtime.ProfileOptimization class , which we can use to start recording a profile when the application starts. This code looks something like this:

ProfileOptimization.SetProfileRoot(@"C:\MyApp");
ProfileOptimization.StartProfile("LaunchProfile");

SetProfileRoot - enables optimization for the current AppDomain and sets the directory where the optimization profile will be saved. This method is ignored on single-core computers.

StartProfile - starts profiling and just-in-time compilation for those methods that were written to the profile (if it already exists).

ASP.NET 4.5 includes multi-core JIT compilation by default. If you want to turn off this optimization, add the attribute to compilationto your web.config:


If you cannot upgrade to .NET 4.5, and want to use this functionality in an application copied for .NET 4.0, there is a way to enable optimization on computers that have .NET 4.5 installed .

If your application has multi-stage startup logic, you can call StartProfile in several places. For example, if after the initial launch the user has a choice (for example, in the menu) which part of the program to go to next, you can use different profiles for different parts of the code. Thus, you optimize JIT compilation depending on user actions.

Efficiency


For large applications, the reduction in download time can be around 50%. To demonstrate this, the development team took the Paint.NET editor, deleted the pre-generated Ngen images, and compared processor time and load in two cases:


This is definitely a pretty simple and effective way to reduce the loading time of .NET applications that don't use Ngen.

More information about performance improvements in .NET 4.5 is available in an article on MSDN:
msdn.microsoft.com/en-us/magazine/hh882452.aspx

Also popular now: