📜 ⬆️ ⬇️

Multicore JIT compilation in .NET 4.5


Historically, .NET developers have used the image generator in Ngen’s machine code. 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 perform a JIT compilation as needed, which will slow it down. The CLR developers have provided a solution in the new version of .NET — a multi-core JIT compilation with the ability to create optimization profiles.

With 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 the multi-core JIT. You can get a 20-50% reduction in load times, which is good news for those who are developing large applications and do not have the ability to use Ngen. For this you need to add to your code just a couple of lines of code.

How it works


In general, you will need to call only two methods in your application:

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


When the application is launched the next time, the .NET runtime will check for the presence of a ready profile and, having found it, will start a JIT compilation in the sequence in which they were stored in the profile.

')

We use multi-core JIT compilation


Using multi-core JIT compilation is easy. In .NET 4.5 there is a class System.Runtime.ProfileOptimization , which we can use to start recording a profile when the application starts. This code looks 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 recorded in 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 compilation to your web.config:

 <compilation profileGuidedOptimizations="None" /> 

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

If your application has multistage 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 on, you can use different profiles for different parts of the code. This way you optimize JIT compilation based on user actions.

Efficiency


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


This is definitely a fairly simple and effective way to reduce the load time of .NET applications that do not use Ngen.

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

Source: https://habr.com/ru/post/163725/


All Articles