What are the advantages and disadvantages of pre-jitting assemblies in .NET?

"Pre-jitting" or pre-compiling will improve performance, at start up, because you would be skipping that step. The reason that .NET JITs every time an app and its libraries load is so that it can run on many platforms and architectures with the best possible optimizations without the need for managing your builds.

So you have to weigh whether it's worth the admin headaches to save a few seconds on app start up and library loads. I think the most common use case for doing this is for server installs where you tend to manage few machines and the environment is very stable. E.g. you would not pre-compile for client apps because the target environments are much less predictable.


"PRE-JIT" is done through NGen (the process of precompiling from CIL to a native image). It will convert the compiled .NET code from the platform-independent intermediate state to a platform specific stage. In plain English, it converts the .NET application that can run on both Windows, Mac and Linux 32-bit and 64-bit to an old-school EXE file that can only run on one of these.

.NET applications are compiled into a intermediate binary format called MSIL that is platform independent. This means that the application can be run by any CPU on any platform as long as the platform supports .NET. What .NET does during execution is called JIT. JIT will compile the code once per execution just before it is actually used. This also means that only the code used will be compiled.

NGen will give your application a performance boost (mostly startup time), sometimes very noticeable. It is safe to NGen just about anything as long as you target the correct platform. For example, if your application uses 32-bit DLL files you should not NGen it to 64-bit, and if your DLL file is in use by other applications you should not NGen it.

I'd recommend running NGen after installation, not before distribution, so that you know the application will work on the target computer.

Tags:

C#

.Net

Jit