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.
[Edit: Added link to Mike's aritcle]