Proposal for SentinelInputRange

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Feb 28 09:13:27 PST 2013


28-Feb-2013 21:09, Steven Schveighoffer пишет:
> On Thu, 28 Feb 2013 11:43:06 -0500, Walter Bright
> <newshound2 at digitalmars.com> wrote:
>
>> On 2/28/2013 6:59 AM, Andrei Alexandrescu wrote:
>>> On 2/28/13 2:37 AM, deadalnix wrote:
>>>> I don't see how defining a specific sentinel range here helps.
>>>
>>> On first blush I agree. It may as well be a range that by convention is
>>> sentinel-terminated, and there's calls to front and popFront but
>>> never to empty.
>>
>>
>> Consider the following code from lexer.c:
>>
>>     p++;
>>     switch (*p)
>>
>> Written using an InputRange:
>>
>>     popFront();
>>     switch (front)
>>
>> That code is INVALID. This is why a SentinelInputRange is necessary.
>> You can't just use an InputRange in an invalid manner by convention.
>
> Does switch(*p) include a case for 0?  If so, wouldn't it be equivalent
> to say if(empty) /* do stuff that case 0 does */ else switch(front)
>
> -Steve

No as a compiler will take it (or may depending on its brain) that 0 is 
what you want to test *first*. It may speed things up if branch is 
almost always taken but its not the case with sentinel. Thus its jsut 
dead code that needs to be decoded, evalutated and skipped (as 
predicated) *before* doing switch jump.

In fact some people avoid the overhead of switch by placing one or two 
of highly-frequent branches with tests before the switch (thus avoiding 
indirect branch it entails in these frequent cases).

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list