views:

2581

answers:

5

Has anybody here ever used ngen? Where? why? Was there any performance improvement? when and where does it make sense to use it?

+4  A: 

ngen is mostly known for improving startup time (by eliminating JIT compilation). It might improve (by reducing JIT time) or decrease overall performance of the application (since some JIT optimizations won't be available).

.NET Framework itself uses ngen for many assemblies upon installation.

Mehrdad Afshari
After installing the .net framework and restarting the machine, the ".NET Runtime Optimization Service" is still working hard "ngening" all of its dlls...
Kalmi
Yeah. This was one of the new features in .NET 2.0 installation. I distinctly remember "Generating Native Images" status in .NET 1.1 installation.
Mehrdad Afshari
Well that makes perfect sense. The framework DLLs will be used in every non trivial .Net application on the system. Not JITing the framework were possible will improve the performance of every program written for .net. You might notice that the Visual Studio installer synchronously ngen's the managed sections of visual studio (which incidentally is why it takes SOOOO long to install).
Spence
+7  A: 

I don't use it day-to-day, but it is used by tools that want to boost performance; for example, Paint.NET uses NGEN during the installer (or maybe first use). It is possible (although I don't know for sure) that some of the MS tools do, too.

Basically, NGEN performs much of the JIT for an assembly up front, so that there is very little delay on a cold start. Of course, in most typical usage, not 100% of the code is ever reached, so in some ways this does a lot of unnecessary work - but it can't tell that ahead of time.

The downside, IMO, is that you need to use the GAC to use NGEN; I try to avoid the GAC as much as possible, so that I can use robocopy-deployment (to servers) and ClickOnce (to clients).

Marc Gravell
+1  A: 

i have used it but just for research purpose. use it ONLY if you are sure about the cpu architecture of your deployment environment (it wont change)

but let me tell you JIT compilation is not too bad and if you have deployments across multiple cpu environments (for example a windows client application which is updated often) THEN DO NOT USE NGEN. thats coz a valid ngen cache depends upon many attributes. if one of these fail, your assembly falls back to jit again

JIT is a clear winner in such cases, as it optimizes code on the fly based on the cpu architecture its running on. (for eg it can detect if there are more then 1 cpu)

and clr is getting better with every release, so in short stick with JIT unless you are dead sure of your deployment environment - even then your performance gains would hardly justify using ngen.exe (probably gains would be in few hundred ms) - imho - its not worth the efforts

also check this real nice link on this topic - JIT Compilation and Performance - To NGen or Not to NGen?

Raj
MS says to run ngen locally, so don't understand the prohibition.... or are you assuming you would distribute the results of ngen?
Richard
I don't understand how you would ship `ngen`ed assembly from your development machine. Can you elaborate on that?
Mehrdad Afshari
The idea is that you run NGEN on the machine where it executes, for example during installation. Then the only problem is if you change CPU on the fly (perhaps a restore after hardware failure, or using P2V, for example).
Marc Gravell
@Marc: I think this is the only way. Do you know a way that you ship previously `ngen`ed assemblies to client machines?
Mehrdad Afshari
@mehrdad + @richard - you basically need to run ngen on target computer upon installation of your app. i am not sure if my local pc ngen would work on another pc (never tried this)
Raj
@Raj: Of course. I know that. The point is, you cannot run ngen on your dev machine and ship the executable. And ngen, contrary to what you said, does care about CPU architecture of the target machine. There are other runtime optimizations available to JIT compiler that are not available to `ngen`.
Mehrdad Afshari
@mehrdad - i am sorry, i should have been more clear with my answer. i typed that in a bit of hurry just to push it up :-) i know not a good thing to do always :-)
Raj
@mehrdad: And visa versa, there are optimisations that ngen will do that the JIT won't (e.g. those that would slow down JIT): ngen does not have the same performance limitations.
Richard
@richard which optimisations will Ngen do that the JIT cannot? I've read in CLR via C# that the problem with NGEN is that the code it produced had to be more general to cover 100% of cases, were a JIT compilation could use context to perform optomisations. But it was my understanding that Ngen would do less work than JIT.
Spence
+8  A: 

Yes, I've seen performance improvements. My measurements indicated that it did improve startup performance if I also put my assemblies into the GAC since my assemblies are all strong named. If your assemblies are strong named, NGen won't make any difference without using the GAC.

This wasn't a very good option for my application since we rely on common assemblies from our company (that are also strong named). The common assemblies are used by many products that use many different versions, putting them in the GAC meant that if one of our applications didn't say "use specific version" of one of the common assemblies it would load the GAC version regardless of what version was in its executing directory. We decided that the benefits of NGen weren't worth the risks.

Peter Tate
+4  A: 

Ngen mainly reduces the start-up time of .NET app and application's working set. But it's have some disadvantages (from CLR Via C# of Jeffrey Richter):

No Intellectual Property Protection

NGen'd files can get out of sync

Inferior Load-Time Performance (Rebasing/Binding)

Inferior Execution-Time Performance

Due to all of the issues just listed, you should be very cautious when considering the use of NGen.exe. For server-side applications, NGen.exe makes little or no sense because only the first client request experiences a performance hit; future client requests run at high speed. In addition, for most server applications, only one instance of the code is required, so there is no working set benefit.

For client applications, NGen.exe might make sense to improve startup time or to reduce working set if an assembly is used by multiple applications simultaneously. Even in a case in which an assembly is not used by multiple applications, NGen'ing an assembly could improve working set. Moreover, if NGen.exe is used for all of a client application's assemblies, the CLR will not need to load the JIT compiler at all, reducing working set even further. Of course, if just one assembly isn't NGen'd or if an assembly's NGen'd file can't be used, the JIT compiler will load, and the application's working set increases.

Vimvq1987