Compiler optimizations
I have said it before and will repeat it here: don’t try to outsmart the compiler. The C# compiler is a fantastic piece of software that can do tricks we can’t even think of. But sometimes, we can help the compiler make choices that affect performance in a good way.
Aggressive optimization
Look at the following method:
private int AddUp(int a, int b) { return a + b; }
I am sure you agree that this is not an exciting method. Calling this, however, does take a lot of time: the calling method has to store the return address, move all parameters (the integer values, a
and b
) to the right place, jump to the method, retrieve the parameters, do the actual work, store the return value in the right place, retrieve the return address, jump to that return address, and assign the result to the variable in the calling method.
The compiler knows this. So, in this particular case, it will probably optimize it and “inline...