[Issue 15581] New: foreach should not do bounds checking

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jan 18 11:10:01 PST 2016


https://issues.dlang.org/show_bug.cgi?id=15581

          Issue ID: 15581
           Summary: foreach should not do bounds checking
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: johannespfau at gmail.com

Consider this code:

----------------------------
void bar(ubyte data){}

void foo(ubyte[] data)
{
    foreach(entry; data)
    {
    bar(entry);
    }
}

void main(){}
----------------------------
dmd test.d
objdump -S test > test.s


We currently emit a bounds check for every element accessed by foreach. This
code should not generate any bounds checks at all. If we trust length to be
correct then all elements are in [0 .. length] and can't be out of bounds.

This problem was found in a murmurhash C++ => D port where it severely affected
performance. We had to fall back to pointer arithmetic+for loops to avoid the
generated bounds checks:
https://github.com/D-Programming-Language/phobos/pull/3916

--


More information about the Digitalmars-d-bugs mailing list