Freigeben über


Not-so-new C# Compiler Features

So a while back somebody asked what new compiler features were coming out for Whidbey that weren't part of new language features.  Well you've already heard about Edit and Continue.  There's also the really cool Refactoring built into the IDE and built off of the compiler's source-code analysis.  As an attempt to improve the debugging experience for those of you with 500+ default.aspx files in your solution, they've added some hashes to the PDB files and improved file lookups to the debugger.  Partly to improve E&C but also to improve the overall debugging experience, the compiler has done some 'de-optimization' to the code (basically adding NOPs and storing more temporaries to locals).  Don't worry none of this should impact the performance of your relase code as long as you remember to use /optimize+.

This does bring me to another question that somebody asked: what optimizations does the C# compiler do?  The answer is very few.  Most of them would fall into the category of flow-graph optimizations: remove branches to next, short circuit branches to branches, remove dead code, invert conditionals to eliminate branches around unconditional branches, etc.  It also does very basic constant folding, although you can easily thwart this by reordering the math (e.g. "4 + 5 + a" folds to  "9 + a", but "4 + a + 5" stays the same).

You might ask why the compielr doesn't do more.  Well I can see 2 reasons: if the compiler optimizes too much it will make the JIT work harder to perform its optimizations and any optimization that applies ot the C# compiler should be written in a way that all compilers targeting MSIL could share.  For the second reason most people prefer to put the optimization into the JIT.  My personal optinion is that this was a bad choice.  Why reoptimize stuff every time the code is run?  NGEN helps, but wouldn't be nicer to just have a simple MSIL optimizer that performed classic dragon-book style machine independent optimizations?

Mike Montwill wrote a nice piece about the exact optimizations with real examples here.

Well enough for this post.  I'm still looking for things to write about.

--Grant

[Edit: Added link to Mike's aritcle]

Comments

  • Anonymous
    March 17, 2005
    just curious, but is there a reason why "4+a+5" is not folded to "9+a" ?

    (i'm somewhat familiar with compilers and ASTs)
  • Anonymous
    March 17, 2005
    Because the compiler does not attempt to do any commutings of operands, thus it sees no binary operator with all constant operands.
  • Anonymous
    March 30, 2005
    There are some ideas for using Cecil http://www.mono-project.com/Cecil
    to do IL level optimizations.


  • Anonymous
    June 20, 2005
    will ngen be available to end users?
  • Anonymous
    June 20, 2005
    Yes. It is already availble as part of the CLR redistributible.