Skip to content

array.Length pattern in array.fs causes bounds checking / perf problems #1419

Closed

Description

@jackmott

It is a common pattern in the array.fs functions to do something like this:

let len = array.Length
for i = 0 to len-1 do
   //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:

int num = len - 1;
        if (num >= i)
        {
            do
            {
                res[len - i - 1] = array[i];
                i++;
            }
            while (i != num + 1);
        }

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 (int i = 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.

Activity

  1. forki commented on Aug 11, 2016

    @forki
    Contributor
    1. I think mechanically replacement is a easy and good fix - compiler/unit tests/property based tests are in place as safety net. Please send a PR ;-)
    2. real fix would be to optimize generated IL ;-)
  2. jackmott commented on Aug 11, 2016

    @jackmott
    ContributorAuthor

    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.

  3. asik commented on Aug 11, 2016

    @asik
    Contributor

    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.

  4. russellhadley commented on Aug 11, 2016

    @russellhadley

    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?

  5. KevinRansom commented on Aug 11, 2016

    @KevinRansom
    Contributor

    @russellhadley . Will do ... thanks mate.

    Kevin

  6. jackmott commented on Aug 12, 2016

    @jackmott
    ContributorAuthor

    @russellhadley If, while you are in there, you could get backward iterations eliding the bounds check in the JIT that would be great!

  7. saul commented on Jan 6, 2017

    @saul
    Contributor

    Looks like this PR was merged - can this be closed now? @cartermp

  8. jackmott commented on Jan 6, 2017

    @jackmott
    ContributorAuthor

    fine by be

  9. added this to the milestone on Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions