D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13976 - Value range propagation to disable some slice bound tests
Summary: Value range propagation to disable some slice bound tests
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: performance, pull
Depends on:
Blocks:
 
Reported: 2015-01-13 12:56 UTC by Kenji Hara
Modified: 2015-02-18 03:42 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2015-01-13 12:56:05 UTC
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.
Comment 2 github-bugzilla 2015-01-14 21:04:07 UTC
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
Comment 3 Per Nordlöw 2015-01-15 18:33:31 UTC
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 Per Nordlöw 2015-01-16 13:15:02 UTC
Correction:

x[0 .. $/2, $2..$]

should of course be

x[0 .. $/2, $/2..$]
Comment 5 github-bugzilla 2015-02-18 03:42:39 UTC
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