www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15581] New: foreach should not do bounds checking

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 puremagic.com
          Reporter: johannespfau 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

--
Jan 18