Bug 13976 – Value range propagation to disable some slice bound tests
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-13T12:56:00Z
Last change time
2015-02-18T03:42:39Z
Keywords
performance, pull
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2015-01-13T12:56:05Z
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.
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`.
Comment #4 by per.nordlow — 2015-01-16T13:15:02Z
Correction:
x[0 .. $/2, $2..$]
should of course be
x[0 .. $/2, $/2..$]
Comment #5 by github-bugzilla — 2015-02-18T03:42:39Z