Practical DiskDiff in C#: Optimizations

The C# compiler performs the following optimizations when the /optimizer flag is used:

  • Local variables that are never read are eliminated (even if they’re assigned to).
  • Unreachable code (code alter a return, for example) is eliminated.
  • A try-catch with an empty try block is eliminated.
  • A try-finally with an empty try is converted to normal code.
  • A try-finally with an empty finally is converted to normal code.
  • Branch optimization is performed.
  • Field initializers that set a member variable to its default value are removed; this optimi­zation, new in the 2.0 release of the compiler, means there’s no performance penalty for setting integer values to 0, Booleans to false, and references to null. Any code that does this is optimized, and the compiler relies on the CLR to intialize these fields to their correct value.

Additionally, when optimization is turned on, it enables optimizations by the JIT compiler.

Source: Gunnerson Eric, Wienholt Nick (2005), A Programmer’s Introduction to C# 2.0, Apress; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *