between and among: worth Phobosization?

Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com> Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com>
Tue Dec 17 07:35:33 PST 2013


On Tuesday, 17 December 2013 at 00:53:07 UTC, Walter Bright wrote:
> It definitely is a good idea for the compiler to recognize:
>
>    if (s == "foo" || s == "bar" || s == "baz")
>
> and generate special code for it.

I guess that would work in this case if among() is inlined since
the integer is then used as a boolean and can be reduced to the
same value (true). If it isn't inlined you would deal with 
(pseudo):

r=0;
if(s=="foo"){ r=1; goto e;}
if(s=="bar"){ r=2; goto e;}
if(s=="baz"){ r=3; goto e;}
e:
return r

So it would be more general to optimize conditionals that test
the same (unchanged) variable into a switch() and then if
possible reduce that to a table lookup if each basic block in the
switch sets the same variables to a constant value…

I guess a fast bool variation could be something like (pseudo):

// skip the length test if s[2] on an empty string doesn't 
core-dump.
if(s.length<3) return false;
return lut[ (s[2]-'o') & MASK] == s;


(the NNTP server or the web-interface seemed to be dead for 12 
hours?)


More information about the Digitalmars-d mailing list