Bug 15581 – foreach should avoid bounds checking

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-01-18T19:10:01Z
Last change time
2020-08-25T09:48:52Z
Keywords
performance, pull
Assigned to
No Owner
Creator
Johannes Pfau

Comments

Comment #0 by johannespfau — 2016-01-18T19:10:01Z
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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/b2cfb9bd01c33b873ca9e7a6c9be986a5feac00a Issue 15581: Disabling bounds checking in ForEachStatement https://github.com/D-Programming-Language/dmd/commit/3002a9683619957ce0d3f1379f99970f1664d087 Merge pull request #5470 from gchatelet/disable_bounds_checking_in_foreach Issue 15581: Disabling bounds checking in ForEachStatement
Comment #2 by dlang-bot — 2020-08-25T09:48:52Z
dlang/dmd pull request #11623 "[dmd-cxx] Remove redundant contract checking" was merged into dmd-cxx: - df78f842e8f03519bc8f286f2c8f0a8053b3a9be by Iain Buclaw: [dmd-cxx] Remove redundant contract checking - Removes prelude assert for constructors and destructors. - Disables bounds checking in foreach statements (fixes Issue 15581) https://github.com/dlang/dmd/pull/11623