I was wondering if there\'s any way to change the behavior of the .NET JIT compiler, by specifying a preference for more in-depth optimizations. Failing that, it would be nice
This is set when you compile your assembly. There are two types of optimizations:
The default setting is this
/optimize- /debug-
This means unoptimized IL, and optimized native code.
/optimize /debug(+/full/pdbonly)
This means unoptimized IL, and unoptimized native code (best debug settings).
Finally, to get the fastest performance:
/optimize+ /debug(-/+/full/pdbonly)
This produces optimized IL and optimized native code.
When producing unoptimized IL, the compiler will insert NOP instructions all over the code. This makes code easier to debug by allowing breakpoints to be set on control flow instructions such as for, while,if,else, try, catch etc.
The CLR does a remarkably good job of optimizing code regardless. Once a method is JIT'ed, the pointer on a call or a callvirt instruction is pointed directly to the native code.
Additionally, the CLR will take advantage of any architecture tricks available when JIT'ing your code. This means that an assembly ran through the JIT will run faster than an assembly pre-compiled by using Ngen (albeit with a slightly slower start up time), as NGen will compile for all platforms, and not take advantage of any tricks.