Is there any way to change the .NET JIT compiler to favor performance over compile time?
Asked Answered
R

1

14

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 if it could do some kind of profile-guided optimization, if it doesn't already.

Rack answered 19/9, 2008 at 1:25 Comment(0)
K
21

This is set when you compile your assembly. There are two types of optimizations:

  • IL optimization
  • JIT Native Code quality.

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.

Kalimantan answered 19/9, 2008 at 1:29 Comment(2)
In VS2010 (and earlier versions IIRC), enable the "Optimize code" project setting, which is on by default in the Release configuration. (This is equivalent to /optimize, which is equivalent to /optimize+.) Note that the optimizer is far from perfect.Hollywood
There are no per-machine tricks, AFAIK. I've been told that the .NET team felt that maintaining multiple versions of the JIT optimizer (which is a VERY complex beast) was too costly compared to the potential benefit.Bosky

© 2022 - 2024 — McMap. All rights reserved.