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
Comment #1 by github-bugzilla — 2016-02-22T11:39:48Z