Proposal for SentinelInputRange

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 28 06:31:13 PST 2013


On Thu, 28 Feb 2013 06:23:45 -0500, deadalnix <deadalnix at gmail.com> wrote:

> On Thursday, 28 February 2013 at 10:57:29 UTC, Walter Bright wrote:
>> Notice the double read of *p. This is what SentinelInputRange is trying  
>> to fix.
>>
>
> But it will be optimized away.

I have to say I agree with deadalnix.

you have essentially in lexer.c:

outer:
while(1)
{
    switch(r.front)
    {
       case 0:
         break outer;
       ...
    }
}

whereas a range where empty is defined as r.front == 0:

while(!r.empty)  // inlined to r.front != 0
{
    switch(r.front) // why would another load occur here?
    {
       // no need to check for 0, already done
       ...
    }
}

If this doesn't translate to the same code, I don't know why not.  The  
second implementation looks more elegant/straightforward.  I think others  
have pointed out that other compilers already do this.  I hope we aren't  
introducing library components to get around DMD optimizer shortcomings.

Note, r cannot be a D string in order to achieve the desired efficiency.   
With a sentinel range, length is never checked, and should not have to be  
updated.

I think it's not necessary to add extra isSentinelRange, etc. checks, just  
define your sentinel range like any other input range.

-Steve


More information about the Digitalmars-d mailing list