Skip to content

[JIT] Elide bounds check for reverse iteration over arrays and spans #9505

@GrabYourPitchforks

Description

@GrabYourPitchforks

Currently the JITter is smart enough to elide bounds checks when forward-iterating over arrays or spans, as in the below sample.

byte[] array;
for (int i = 0; i < array.Length; i++) {
  // consume array[i]
}

It would help certain tight loops if the JITter could also recognize the reverse-iteration pattern and elide the bounds checks in that scenario as well.

byte[] array;
for (int i = array.Length - 1; i >= 0 /* or any non-negative const */; i--) {
  // consume array[i]
}

This optimization would save a cmp and jae instruction in these tight loops, and in certain cases it would also allow omitting the stack frame setup that normally accompanies methods that might throw exceptions.

category:cq
theme:bounds-checks
skill-level:expert
cost:medium
impact:small

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions