www.digitalmars.com         C & C++   DMDScript  

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


          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)

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:

Jan 18 2016