I’m profiling a bit of F# code with VS2010, and the profiler shows that about 20% of the time is spent in ‘JIT_ChkCastAny’, but doesn’t go so far as to point to the offending bit of code.
Does anyone know what kinds of things in F# compile into something that invokes this method?
Update: By commenting out chunks of code, I’ve narrowed it down to the following methods:
Array.sumBy, Array.averageBy, Array.minBy, and Array.maxBy. In each case, the numerical type parameter is double, so why is the compiled code so slow?
Here is the implementation of
Array.sumin array.fs (similar forArray.sumBy):It does use
Checked.(+)operator which has a lot of overheads. In case you wonder, the implementation ofChecked.(+)in prim-types.fsuses Reflection. And theJIT_ChkCastAnyterm could result from using thisCheckedmodule.I did the following measurement to compare between
Array.sumand an implementation of sum usingArray.fold:The second version is indeed a magnitude faster than the first one. Therefore, if
Array.sumdoesn’t satisfy your requirement, you could use your own implementation ofsum.