opEquals needs to return bool

bearophile bearophileHUGS at lycos.com
Fri May 16 13:21:00 PDT 2008


Edward Diener:
> IMO, even if it were so, one does not twist programming design for a 
> performance gain.

In some situations bools are slower, for example you can try this small program:
http://codepad.org/pD22tteK

if at line 8 you replace:
int is_valid, went_off_right_side;

With a logically more correct:
bool is_valid, went_off_right_side;

The program runs slower (with DMD on my CPU about 4% slower).

Bools are bytes, while ints are 4 bytes, so sometimes ints are managed faster.
I agree that it's better for opEquals to return a bool.
In theory the compiler may become a bit smarter and use a size_t to represent a bool where this doesn't change the meaning of the program, but this looks not easy in general. Another possibility is to define a second faster bool type, but having two different bools looks bad in a language like D.

Bye,
bearophile



More information about the Digitalmars-d mailing list