www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Avoiding Range Checks in Slice Expressions

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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?
Mar 30 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 30 2014
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 30 Mar 2014 15:40:43 -0400, Nordl=C3=B6w <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
Mar 31 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 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.
Mar 31 2014
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 If not, what would it require to implement it?
That would be an interesting task to fix :) DMD source references anyone?
Mar 31 2014