When does F# do constant folding?

163 views Asked by At

I just wrote some inlined maths utility functions and, while testing, noticed that

let quat = (3, 0, 0, 3)
(quat |||~||| quat)
|> ijk
|> printAn

is completely folded into printAn (18, 0, 0) while starting with (3.0f, 0.0f, 0.0f, 3.0f) only inserts the original 0.0f and 3.0f constant values without evaluating any of the float operators and with some Tuples being instantiated (but not nearly as many as in debug mode).

(I use Visual Studio 2013 Update 4 targeting the 4.5 framework, with the default Release configuration. Undefining the TRACE constant doesn't seem to make a difference and neither does open Unchecked.)


I'm not too concerned with the performance implications since it should be easy to fold the expression through IL weaving if it becomes an issue, but I'm nonetheless interested in what exactly the compiler can constant-fold.

0

There are 0 answers