In general, with a slice expression: arr[a..b] The array boundaries are checked by the condition at runtime: b <= arr.length && a <= b` But with the following program: void main() { int[10] a; size_t n; auto s = a[n%3 .. n%3 + 3]; assert(s.length == 3); } Compiler can deduce: ValueRangeOf(lower) == ValueRangeOf(n%3) --> [0, 2] ValueRangeOf(upper) == ValueRangeOf(n%3 + 3) --> [3, 5] a.length == 10 and those formulas are satisfied always: ValueRangeOf(upper) <= arr.length ValueRangeOf(lower) <= ValueRangeOf(upper) Finally, compiler can eliminate redundant runtime bound tests. Note that, optimization for the array indexing is already done in issue 9097.
https://github.com/D-Programming-Language/dmd/pull/4293
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d80902bc7afd3be44249b6a15c97afdb42cf79bc fix Issue 13976 - Value range propagation to disable some slice bound tests https://github.com/D-Programming-Language/dmd/commit/68260d6cbaff13e93257bdba97d9d868a76b0dda Merge pull request #4293 from 9rnsr/fix13976 Issue 13976 - Value range propagation to disable some slice bound tests
Does this include avoiding range checking in expressions such as ```D x[0 .. $/2, $2..$] ``` which is a reoccurring pattern in D-style binary divide-and-conquer algorithms. It would be super-cool if it worked for rational variants with slice indexes in the form ```D $*p/q ``` where it can be statically proven that `p <= q`.
Correction: x[0 .. $/2, $2..$] should of course be x[0 .. $/2, $/2..$]
Commits pushed to 2.067 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d80902bc7afd3be44249b6a15c97afdb42cf79bc fix Issue 13976 - Value range propagation to disable some slice bound tests https://github.com/D-Programming-Language/dmd/commit/68260d6cbaff13e93257bdba97d9d868a76b0dda Merge pull request #4293 from 9rnsr/fix13976