Quote:
Original post by Mike.Popoloski
Quote:
The IL instructions get converted to processor specific code by the JIT compiler. Is this code saved so it doesn't have to keep getting recompiled, and is it possible to pre-compile the code so the JIT doesn't have to when it is run? If so, what are the pros/cons of letting the JIT compile it at runtime?
It is possible to precompile an assembly using the ngen.exe tool. This tool does the JIT process and then saves the result as a native executable, so that the JIT step is skipped when the application is run. This provides greater speed for the application. There aren't really any problems using this method, but you do have to run ngen on the client's machine, because it takes into account the processor and other hardware details when creating the native image.
Allow me to disagree just a little bit. .NET applications are programs which run via the CLR, and which use the .NET Framework Library. By running ngen on an assembly, native machine code is generated for the assembly based upon the current .NET Framework Library, the current CPU, and the current Operating system of the person running ngen.exe.
This means that any patches, updates, or upgrades to the .NET Framework Library or the CLR will have no effect on the native image. This isn't a huge problem as one would hope the .NET version was stable to begin with, but it does make it more difficult for end-users to benefit from improvements to the framework itself.
Also, ngen compiles machine code based on the CPU and OS. So switches from Intel to AMD, or vice versa, could have negative impacts on performance, as the machine code created may have been taking advantage of unique speed improvements. Does this mean the image wont run on competing processors? No, it probably doesn't mean that, so long as the architecture is the same. However, again, there may be performance implications. And the above is true for the operating system as well. Switching from XP to Vista may have side effects upon performance, as the native image may have been generated with a different OS's libraries.
And finally, the CLR does things for the user beyond just generating JIT code. It does things such as perform security checks, etc...So by compiling it into native code you lose those benefits. And worse, if your native image was generated with certain required security permissions, there's no way to change that later. Whereas normally, the CLR could request different security permissions, etc...and the user could grant or deny as necessary.
Ok, one last thing. As Intermediate Language isn't machine specific, code written for the .NET CLR is highly portable. Like Java, any machine which runs a CLR could run the application. Although there are not many commercial CLR's available, there are a number of OpenSource projects currently creating CLR implementations on Linux, Mac, etc...This means that in the near future, code written for the CLR will easily run on multiple Operating Systems.
By running ngen, you lose that, as your code goes from highly portable, to barely portable.
Cheers!