You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is a common pattern in the array.fs functions to do something like this:
letlen= array.Length
for i =0to len-1do//etc
I have tested and found that this causes performance issues, probably related to the JIT not identifying that bounds checking is unnecessary. As well, the compiled IL is odd. The C# view of the for loop in Array.rev for instance:
If you refactor the for loop to read for i = 0 to array.Length-1 do you get much more sensible compilation, again the C# view from ILSpy:
for(inti=0;i<array.Length;i++){
Assuming there is no other good reason for this pattern, it should be possible to mechanically go through and replace this pattern and improve the performance of many of the core lib functions.
PR on the way.
For the real fix - a for x downto 0 loop doesn't compile properly either way.
So if/when someone works on that, they should address downto as well.
The JIT eliminates bounds checking for the equivalent IL of this, specifically:
for i in 0 to array.Length - 1 do
array.[i]
Very picky about that, anything slightly different gets bounds checking, including backwards loops. Only way to be sure is to inspect the assembly code (make sure to disable "Suppress JIT optimizations on module load"), if you see a branch and call before each array access that's bounds checking.
So the best you can do is try to stick to that form when you can, but F# can't fix the JIT.
Note F# also compiles this in a form that gets bounds checking elimination, so it's equivalent*:
for elem in array do
elem
*in as much as it gets bounds checking elimination. It does copy the value, so there may be extra cost vs. an array access which is guaranteed to be a reference.
Before merging in the change to the lib let us take a peek at the JIT and see if we can get this case. @JosephTremoulet, you were just looking at bounds checks, can you take a look at this case?
Activity
forki commentedon Aug 11, 2016
jackmott commentedon Aug 11, 2016
PR on the way.
For the real fix - a for x downto 0 loop doesn't compile properly either way.
So if/when someone works on that, they should address downto as well.
asik commentedon Aug 11, 2016
The JIT eliminates bounds checking for the equivalent IL of this, specifically:
Very picky about that, anything slightly different gets bounds checking, including backwards loops. Only way to be sure is to inspect the assembly code (make sure to disable "Suppress JIT optimizations on module load"), if you see a branch and call before each array access that's bounds checking.
So the best you can do is try to stick to that form when you can, but F# can't fix the JIT.
Note F# also compiles this in a form that gets bounds checking elimination, so it's equivalent*:
*in as much as it gets bounds checking elimination. It does copy the value, so there may be extra cost vs. an array access which is guaranteed to be a reference.
russellhadley commentedon Aug 11, 2016
Before merging in the change to the lib let us take a peek at the JIT and see if we can get this case. @JosephTremoulet, you were just looking at bounds checks, can you take a look at this case?
KevinRansom commentedon Aug 11, 2016
@russellhadley . Will do ... thanks mate.
Kevin
jackmott commentedon Aug 12, 2016
@russellhadley If, while you are in there, you could get backward iterations eliding the bounds check in the JIT that would be great!
saul commentedon Jan 6, 2017
Looks like this PR was merged - can this be closed now? @cartermp
jackmott commentedon Jan 6, 2017
fine by be