Search code examples
c#programming-languagesexpressioncompiler-optimization

Why are arithmetical expressions not optimized for multiplication by 0 in C#


In order to evaluate a multiplication you have to evaluate the first term, then the second term and finally multiply the two values.

Given that every number multiplied by 0 is 0, if the evaluation of the first term returns 0 I would expect that the entire multiplication is evaluated to 0 without evaluating the second term.

However if you try this code:

var x = 0 * ComplexOperation();

The function ComplexOperation is called despite the fact that we know that x is 0.

The optimized behavior would be also consistent with the Boolean Operator '&&' that evaluates the second term only if the first one is evaluated as true. (The '&' operator evaluates both terms in any case)

I tested this behavior in C# but I guess it is the same for almost all languages.


Solution

  • Firstly, for floating-point, your assertion isn't even true! Consider that 0 * inf is not 0, and 0 * nan is not 0.

    But more generally, if you're talking about optimizations, then I guess the compiler is free to not evaluate ComplexOperation if it can prove there are no side-effects.

    However, I think you're really talking about short-circuit semantics (i.e. a language feature, not a compiler feature). If so, then the real justification is that C# is copying the semantics of earlier languages (originally C) to maintain consistency.