Improvements in the CLR Core in .NET Framework 4.5

September 17, 2011

5 comments

Alongside all the exciting advents in Windows 8 and Metro apps, the .NET CLR is marching on. The next version of the CLR will feature several “internals” improvements, mostly in the performance area. Read on to learn about changes to the garbage collector, the JIT, and the native image generator engine in the next CLR.

Background mode for Server GC
Background GC is a neat feature introduced in CLR 4.0 to the Workstation GC flavor. It’s a little hard to explain without any background (pun intended), but the general gist is the following: while performing a full gen2 collection, the GC checkpoints at well-defined locations to see if a gen0/gen1 GC has been requested*. If a lower generation GC has been requested, the gen2 collection is paused, and the low-generation collection runs to completion before the gen2 collection resumes. This allows the code that caused the low-generation collection to resume its execution.

The new feature in the next CLR is that background GC is now supported on Server GC. (Recall that under Server GC, there is a GC heap for each processor and a separate GC thread for each processor.) There are additional changes in the GC, including a work-stealing mode in Server GC that allows GC threads to steal parts of the graph for marking if the workload is not distributed evenly.

Managed Profile Guided Optimization
If you have done any C++ work since Visual Studio 2003, you probably know about Profile Guided Optimization (PGO). In the C++ compiler, PGO is a special multi-step optimization mode, which relies on exercising the application through a set of scenarios with data collection enabled, and then recompiling the application with this data to optimize based on runtime information.

The same idea is now applied to managed code that has been NGEN-ed. A special command-line tool called mpgo.exe (which I can see integrated into Visual Studio like the C++ counterpart) instruments your .exe and embeds into it some runtime data that is subsequently used by ngen.exe to generate an optimized image.

You might wonder what kind of optimizations are enabled by MPGO. The only two that were disclosed at the time have to do with reducing the number of pages loaded from disk by organizing hot code together on the minimal number of pages, and with reducing the number of copy-on-write pages by organizing together on the minimal number of pages any data that will likely be overwritten at runtime.

Automatic NGEN
Integrating NGEN in your application’s installer might be a challenge; auto-NGEN addresses this challenge by performing NGEN automatically if runtime-generated profiling information deems fit. When your .NET 4.5 application runs, the runtime generates assembly usage logs which are examined by an automatic maintenance task that runs in the background when the system is idle, and generates native images for frequently used assemblies. (This maintenance task might also choose to evict native images for assemblies that have not been used for a long time.)

Multi-core background JIT
Developers can now opt-in to an optimized JIT that uses otherwise unused processing time to perform multi-threaded JIT compilation of frequently used methods speculatively. The first time the application is launched, data is collected on frequently used methods, and on subsequent launches this data is used to guide the background JIT engine.

This feature requires adding a few lines of code to your application – calls to the ProfileOptimization.SetProfileRoot() and ProfileOptimization.StartProfile(filename) methods. However, this feature is enabled by default in Silverlight 5 and ASP.NET applications.

Re-JIT
This last feature allows recompiling a method (running the JIT again) at runtime. This is a feature most likely suitable for profilers that replace the IL code with an instrumented version at runtime to collect information without restarting the process. It is exposed through the ICorProfileInfo4::RequestReJIT and ICorProfilerCallback4::GetReJITParameters methods.


* How come a low-generation GC was requested while a full collection was running? Recall that Workstation GC can be non-blocking (concurrent), enabling application threads to run during significant parts of the collection itself, including most of the GC mark phase.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

5 comments

  1. OohSeptember 22, 2011 ב 2:16 PM

    Sasha, Managed PGO is a huge step forwards for us application developers. The .NET Framework has always had it, but now we’ll finally be able to apply the same optimizations to our applications!

    Furthermore I’d like to add that these two MPGO optimizations are HUGE. Just organizing hot pages together can easily give a >25% perf gain.

    Even with only this single feature the upgrade is totally worth it.

    Reply
  2. OohOctober 5, 2011 ב 4:46 PM

    Sasha, Managed PGO is a huge step forwards for us application developers. The .NET Framework has always had it, but now we’ll finally be able to apply the same optimizations to our applications!

    Furthermore I’d like to add that these two MPGO optimizations are HUGE. Just organizing hot pages together can easily give a >25% perf gain.

    Even with only this single feature the upgrade is totally worth it.

    Reply
  3. VincentMarch 22, 2012 ב 3:42 PM

    Only one question: who is that dumb who made this tool? It starts in separate console and after it exits, you CANNOT READ any output, because console is closed! Stupids…

    Reply
  4. KeithMay 22, 2012 ב 11:32 PM

    It is unfortunate that MPGO doesn’t work on signed assemblies e.g. from MPGO usage:

    Any of the assemblies in the list that are in the GAC or have been signed
    will not be properly updated to contain the profile information.
    Please un-GAC (or do not GAC to begin with) and do not yet sign any
    assemblies you wish to collect profile information about.

    Our use of strong naming and the InternalsVisibleToAttribute makes it nigh impossible to use MPGO.

    Reply
  5. WoAxOctober 30, 2012 ב 4:58 PM

    Hi,
    Does anybody know if ProfileOptimization.SetProfileRoot() and ProfileOptimization.StartProfile(filename)
    Methods work correctly?

    And as I understood they perform only the 2nd time better?

    regards

    Reply