Eliding of slice range checking

Stefan Koch uplink.coder at googlemail.com
Fri Oct 25 12:37:13 UTC 2019


On Wednesday, 23 October 2019 at 12:01:47 UTC, Per Nordlöw wrote:
> On Wednesday, 23 October 2019 at 11:33:56 UTC, kinke wrote:
>> For your example, the template is inferred to be @safe, and 
>> `-release` only elides bounds checks in @system functions 
>> (corresponding to `-boundscheck=safeonly`). Use 
>> `-boundscheck=off` to elide it in all functions.
>
> Thanks. But I'm talking about the compiler being able to figure 
> out that the expression
>
>     haystack[0 .. needle.length]
>
> _never_ (regardless of compiler flags) needs any range checking 
> because it is _only_ run when
>
>     haystack.length >= needle.length
>
> . Do you follow?

Actually what you want is that the compiler uses a loop-invariant 
to only to bounds-checking once on the first loop entry?

That's quite tricky to do for all cases.
What you can do manually is to index the .ptr property which will 
decay the array to a pointer, and on a pointer you cannot and 
therefore will not do boundschecking

just replace x = a[i] with x = a.ptr[i];


More information about the Digitalmars-d-learn mailing list