int opEquals(Object), and other legacy ints

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sun Jul 30 15:42:48 PDT 2006


Walter Bright wrote:
> Bruno Medeiros wrote:
>> But the question remains, is it then less efficient to return a byte 
>> than a int?
> 
> Yes. It's also less efficient to constrain the results to 0 or 1.
> 
>> Why?
> 
> Consider:
> 
>     a = 0x1000;
>     b = 0x2000;
> 
> Now convert (a == b) into a bool. If the result is an int, I can just do 
> (a - b), one instruction. Converting it to a byte, or to 1 or 0, takes 
> more.
> 
>> And if so isn't there a way for the compiler to somehow optimize it?
> 
> The math is inevitable <g>.
> 

Well, let's think about the other way around then. Why should bool be 
constrained to 0 or 1? Why not, same as kris said, 0 would be false, and 
non zero would be true. Then we could have an opEquals or any function 
returning a bool instead of int, without penalty loss.

The only shortcoming I see is that it would be slower to compare two 
bool /variables/:
    (b1 == b2)
that expression is currently just 1 instruction, a CMP, but without the 
0,1 restriction it would be more (3, I think, have to check that). 
However, is that significantly worse? I think not. I think comparison 
between two bool _variables_ is likely very rare, and when it happens it 
is also probably not performance critical. (statistical references?)
Note: this would not affect at all comparisons between a bool variable 
and a bool literal. Like (b == true) or (b == false).

Or is there another reason for the 0,1 restriction?

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-bugs mailing list