Quick post on a great new feature in .NET 4.5: Multicore JIT.
Inspired by Rick Brewster blog post.
I have just updated my Outlook Add-In project from .NET 3.5 to 4.0 client profile to enjoy the latest and greatest of 4.0-4.5 CLR.
Two thinks worth mentioning here:
- I can't move the project to .NET 4.5 because I still have to support windows XP that doesn't support 4.5 at all, and 4.5 it is still less widespread than 4.0 even for Windows 7 machines.
- .NET 4.5 is an in-place update that replaces .NET 4.0 so event when targeting .NET 4.0 if the client machine has .NET 4.5 installed it will load 4.5 framework and CLR to enjoy all 4.5 optimizations.
One of the new features in .NET 4.5 is Multicore JIT:
Multicore JIT idea is to use the second CPU core of the machine to JIT methods before they are actually required by application execution.
Let's take an oversimplified example: If the application is executing Func1 and later will execute Func2 and Func3 for the first time. Without multicore JIT when the application will want to execute Func2 and then Func3 it will have to stop, JIT the methods and then resume execution. With multicore JIT the CLR will know that it will execute Func2 and Func3 so while Func1 is executing it will JIT Func2 and Func3 in parallel. Therefor the application execution won't stop and will gain performance.
For the multicore JIT to know which methods it needs to JIT and the order to JIT them is uses "scenario" technique: the first time the application executes there is no optimization but the multicore JIT records all the methods that are been JIT-ed, this information is stored in scenario file on the disk. The next time the application is executed the multicore JIT reads the scenario file to know which methods to JIT before they are requested.
Multicore JIT requirements:
- The machine CPU needs at least 2 cores, otherwise the multicore JIT just ignores it.
- You must call the two methods of ProfileOptimization class to setup the folder for the scenarios and to start profiling.
- The profiling data needs to be written to disk and there are two options for that:
- Graceful shutdown of the CLR.
- Second call to StartProfile method so the first call scenario is written1.
Multicore JIT is .NET 4.5 feature but my project is targeting .NET 4.0 but as I mentioned in the beginning .NET 4.5 is in-place update so event that my project is targeting .NET 4.0 if it is executed on a machine with .NET 4.5 installed all .NET 4.5 features are available. Using simple reflection to call [ProfileOptimization] you get the same benefits as if you were targeting .NET 4.5!
And the result: 20%-30% Add-In startup optimization!
- In my case the fast shutdown of Outlook 2010 is preventing the graceful CLR shutdown so I found this second option. Shame there is no StopProfile methods. ↩