Thread overview
Avoiding Range Checks in Slice Expressions
Mar 30, 2014
Nordlöw
Mar 30, 2014
bearophile
Mar 31, 2014
Nordlöw
Mar 31, 2014
Nordlöw
March 30, 2014
Does DMD currently avoid range checks in array slice expressions such as

f(x[0..$/2])
f(x[$/2..$])

typically found in divide-and-conquer algorithms such as quicksort?

If not, what would it require to implement it?
March 30, 2014
Nordlöw:

> Does DMD currently avoid range checks in array slice expressions such as
>
> f(x[0..$/2])
> f(x[$/2..$])

You have two simple ways to answer your question: try to go past the slices and look if D raises an error, and look at the asm produced with various compiler switches.

Bye,
bearophile
March 31, 2014
On Sun, 30 Mar 2014 15:40:43 -0400, Nordlöw <per.nordlow@gmail.com> wrote:

> Does DMD currently avoid range checks in array slice expressions such as
>
> f(x[0..$/2])
> f(x[$/2..$])
>
> typically found in divide-and-conquer algorithms such as quicksort?

If they are range-checked, it would be a good addition to the optimizer to remove them. My guess is that it is range-checked.

> If not, what would it require to implement it?

As a hack, you can use the pointer, which will not be range checked:

f(x.ptr[0..x.length/2]) // note you can't use $ because the pointer doesn't have length
f(x.ptr[x.length/2..x.length])

-Steve
March 31, 2014
> If they are range-checked, it would be a good addition to the optimizer to remove them. My guess is that it is range-checked.
>

$/n is of course always within range if n is positive integer >= 1.

But what about in the general indexing/slicing case? In that case it would be useful if we could reuse value range propagation (VRP) in DMD, to figure out which other expressions that don't need range checking.
March 31, 2014
>
>> If not, what would it require to implement it?

That would be an interesting task to fix :)

DMD source references anyone?