Click here to Skip to main content
Click here to Skip to main content

Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time

, 27 Nov 2008
Rate this:
Please Sign up or sign in to vote.
This article describes how to easily trigger pre-JITting of your heavy assemblies.

Introduction

All .NET developers know that one of the best features of the CLR is JIT-compilation: Just-In-Time compilation. Its name describes how it works: right before calling your method (just in time), the CLR triggers the JIT-compiler to produce native code from MSIL. JIT compilation is perfect for server-side applications: the JIT compiler produces highly optimized native code based on the execution environment data. The generated native code is stored in the application domain, and is used during consequent calls to this method. Though JIT compilation is not always the best choice for client-side applications with a rich user interface - users are pretty often complaining about lazy start-up of applications. In spite of developer explanations like "guys, it's JITting, we can't do anything here", users continue to complain, making our development managers unhappy. Obviously, unhappy managers tend to be unhappy developers.

So, what is the solution here? Develop the client-side application in C++ or Delphi? Some might say that we could use the Native Image Generation tool in .NET (ngen.exe). ngen.exe is not a silver bullet here, due to the following reasons:

    1. MSDN states regarding native images: Performance of native images depends on a number of factors that make analysis difficult, such as code and data access patterns, how many calls are made across module boundaries, and how many dependencies have already been loaded by other applications. The only way to determine whether native images benefit your application is by careful performance measurements in your key deployment scenarios. Let me rephrase this: it means that it does not guarantee you better performance.

    2. Native images installation/creation requires administrative privileges. This slightly reduces the set of suitable scenarios.

So, suddenly, I've come to the main point of my article. The main point is how to trigger JIT compilation in advance, how to pre-compile your assembly when it's more preferable for your application.

Let me start from an example:

Consider a scenario: We have a rich desktop client application with a huge number of different forms and and fancy UI controls (like DevExpress or Infragistics). Of course, during the first opening of each form, users are going to experience a delay due to JITting of all these fancy control libraries. How can we avoid this? We can trigger JIT compilation of heavy UI control libraries at some starting point of the application (for instance, during user log-in) in a separate background thread with a low priority. Or, we can pre-JIT all other forms while the user is working with the first one... I can imagine here a lot of Use Cases, but I am sure you can do it better in the scope of your particular project.

The code

We have a very nice namespace in BCL called System.Runtime.CompilerServices which provides advanced developers with the ability to influence runtime behavior. But, here, we need only one class from this namespace - RuntimeHelpers, and its method PrepareMethod, in particular. So, what do we do here: I just load some heavy assembly by using the Assembly.Load method (you can load it in a different way, of course), then walk through its types, and force JITting of all the methods of each type. That's it. As easy as possible. One more point, I do it in a separate low priority thread: don't worry about it, PrepareMethod is thread safe.

Thread jitter = new Thread(() =>
{
  foreach (var type in Assembly.Load("MyHavyAssembly, Version=1.8.2008.8," + 
           " Culture=neutral, PublicKeyToken=8744b20f8da049e3").GetTypes())
  {
    foreach (var method in type.GetMethods(BindingFlags.DeclaredOnly | 
                        BindingFlags.NonPublic | 
                        BindingFlags.Public | BindingFlags.Instance | 
                        BindingFlags.Static))
    {
      System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle);
    }
  }
});
jitter.Priority = ThreadPriority.Lowest;
jitter.Start();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vitaliy Liptchinsky
Technical Lead bwin Interactive Entertainment AG
Austria Austria
The views expressed in my articles are mine and do not necessarily reflect the views of my employer.
 
if(youWantToContactMe)
{
SendMessage(string.Format("{0}@{1}.com", "liptchinski_vit", "yahoo"));
}
 
More info in my LinkedIn profile:
http://www.linkedin.com/in/vitaliyliptchinsky

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSuper Lloyd4-Apr-11 16:18 
GeneralGreat post PinmemberAndriy Buday14-May-10 13:10 
GeneralComments PinmemberDmitri Nesteruk18-Dec-08 4:24 
GeneralRe: Comments PinmemberVitaliy Liptchinsky18-Dec-08 4:53 
GeneralError PinsitebuilderUwe Keim14-Dec-08 22:35 
GeneralRe: Error PinmemberVitaliy Liptchinsky15-Dec-08 0:13 
GeneralRe: Error PinsitebuilderUwe Keim15-Dec-08 1:18 
GeneralRe: Error PinsitebuilderUwe Keim15-Dec-08 1:26 
GeneralRe: Error PinmemberChris Richner18-Dec-08 4:00 
GeneralRe: Error PinmemberVitaliy Liptchinsky18-Dec-08 4:05 
GeneralRe: Error PinmemberChris Richner20-Dec-08 2:59 
GeneralA very useful post, thanks - you might consider the following PinmemberNeil Lamka3-Dec-08 13:14 
GeneralCompiler as Service... PinmemberSwader2-Dec-08 17:50 
GeneralRe: Compiler as Service... PinmemberAbel Braaksma25-Mar-12 10:42 
GeneralNice solution Pinmembercokkiy2-Dec-08 1:49 
GeneralThanks + Comments [modified] Pinmemberittay ophir1-Dec-08 21:49 
GeneralRe: Thanks + Comments PinmemberVitaliy Liptchinsky1-Dec-08 21:55 
GeneralRe: Thanks + Comments Pinmemberittay ophir1-Dec-08 22:40 
GeneralRe: Thanks + Comments PinmemberVitaliy Liptchinsky1-Dec-08 23:11 
GeneralRe: Thanks + Comments Pinmemberittay ophir1-Dec-08 22:46 
GeneralRe: Thanks + Comments PinmemberMember 46152963-Dec-08 5:08 
GeneralERRORS Pinmemberseeblunt28-Nov-08 2:08 
GeneralRe: ERRORS PinmemberNicola Costantini28-Nov-08 2:41 
GeneralRe: ERRORS PinmemberVitaliy Liptchinsky28-Nov-08 3:08 
GeneralRe: ERRORS PinmemberNicola Costantini28-Nov-08 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140902.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by Vitaliy Liptchinsky
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid