tags:

views:

950

answers:

11

[This question is related to but not the same as this one.]

My compiler warns about implicitly converting or casting certain types to bool whereas explicit conversions do not produce a warning:

long t = 0;
bool b = false;
b = t;                        // performance warning: forcing long to bool
b = (bool)t;                  // performance warning
b = bool( t );                // performance warning
b = static_cast< bool >( t ); // performance warning
b = t ? true : false;         // ok, no warning
b = t != 0;                   // ok
b = !!t;                      // ok

This is with Visual C++ 2008 but I suspect other compilers may have similar warnings.

So my question is: what is the performance implication of casting/converting to bool? Does explicit conversion have better performance in some circumstance (e.g., for certain target architectures or processors)? Does implicit conversion somehow confuse the optimizer?

Microsoft's explanation of their warning is not particularly helpful. They imply that there is a good reason but they don't explain it.

+4  A: 

The performance is identical across the board. It involves a couple of instructions on x86, maybe 3 on some other architectures.

On x86 / VC++, they all do

cmp DWORD PTR [whatever], 0
setne al

GCC generates the same thing, but without the warnings (at any warning-level).

Two instructions on ARM and MIPS as well :-)
Nils Pipenbrinck
+4  A: 

As far as I know, there is no warning on any other compiler for this. The only way I can think that this would cause a performance loss is that the compiler has to compare the entire integer to 0 and then assign the bool appropriately (unlike a conversion such as a char to bool, where the result can be copied over because a bool is one byte and so they are effectively the same), or an integral conversion which involves copying some or all of the source to the destination, possibly after a zero of the destination if it's bigger than the source (in terms of memory).

It's yet another one of Microsoft's useless and unhelpful ideas as to what constitutes good code, and leads us to have to put up with stupid definitions like this:

template <typename T>
inline bool to_bool (const T& t)
  { return t ? true : false; }
coppro
Or you could disable the warning.
I think MS included the error because it is an implicit conversion to bool and thus isn't type safe. They could have certainly chosen a better error message though. I prefer the `b = t != 0` method for suppressing this, as it is clearly an operation that takes a certain type and returns bool.
rmeador
A: 

Based on your link to MS' explanation, it appears that if the value is merely 1 or 0, there is not performance hit, but if it's any other non-0 value that a comparison must be built at compile time?

warren
+3  A: 

Sounds like premature optimization to me. Are you expecting that the performance of the cast to seriously effect the performance of your app? Maybe if you are writing kernel code or device drivers but in most cases, they all should be ok.

Craig
As a matter of fact I am writing a device driver. And I am also curious.
jwfearn
Even in device drivers it's not really important, but curiosity is a valid reason as well :)
Ilya
A: 

I don't think performance is the issue here. The reason you get a warning is that information is lost during conversion from int to bool.

Dima
all the conversions lose info but only some warn
jwfearn
It explicitly calls itself a *performance* warning
jwfern, no, not all conversions. How about conversions to longer types (short to int, int to long, float to double)?
Dima
Dima -- I meant all the conversions in this Q, which are from long to bool
jwfearn
A: 

In C++ a bool ISA int with only two values 0 = false, 1 = true. The compiler only has to check one bit. To be perfectly clear, true != 0, so any int can override bool, it just cost processing cycles to do so.

By using a long as in the code sample, you are forcing a lot more bit checks, which will cause a performance hit.

No this is not premature optimization, it is quite crazy to use code that takes more processing time across the board. This is simply good coding practice.

WolfmanDragon
A: 

Unless you're writing code for a really critical inner loop (simulator core, ray-tracer, etc.) there is no point in worrying about any performance hits in this case. There are other more important things to worry about in your code (and other more significant performance traps lurking, I'm sure).

JesperE
+2  A: 

The performance warning does actually make a little bit of sense. I've had it as well and my curiousity led me to investigate with the disassembler. It is trying to tell you that the compiler has to generate some code to coerce the value to either 0 or 1. Because you are insisting on a bool, the old school C idea of 0 or anything else doesn't apply.

You can avoid that tiny performance hit if you really want to. The best way is to avoid the cast altogether and use a bool from the start. If you must have an int, you could just use if( int ) instead of if( bool ). The code generated will simply check whether the int is 0 or not. No extra code to make sure the value is 1 if it's not 0 will be generated.

Bill Forster
sounds plausible, but i wonder why the explicit conversions don't produce the warning
jwfearn
There are warnings on all casts / conversions. The cases where there is no warning are just code constructs that effect conversions. The compiler can't read your mind and it doesn't know that you are just converting and nothing else.
Bill Forster
makes sense, thanks!
jwfearn
A: 

Microsoft's explanation seems to be that what they're trying to say is:

Hey, if you're using an int, but are only storing true or false information in it, make it a bool!

I'm skeptical about how much would be gained performance-wise, but MS may have found that there was some gain (for their use, anyway). Microsoft's code does tend to run an awful lot, so maybe they've found the micro-optimization to be worthwhile. I believe that a fair bit of what goes into the MS compiler is to support stuff they find useful themselves (only makes sense, right?).

And you get rid of some dirty, little casts to boot.

Michael Burr
+14  A: 
paercebal
Don't shoot the messenger... :-p ... I'm just quoting a Microsoft Developer...
paercebal
Was aiming at Microsoft, not you :)
:-p ... No harm done... :-p
paercebal
By the way, thanks to Mike B for his edition: I was searching how to "quote" the text, and failing... :-p
paercebal
A: 
long t;
bool b;
int i;
signed char c;
...

You get a warning when you do anything that would be "free" if bool wasn't required to be 0 or 1. b = !!t is effectively assigning the result of the (language built-in, non-overrideable) bool operator!(long)

You shouldn't expect the ! or != operators to cost zero asm instructions even with an optimizing compiler. It is usually true that int i = t is usually optimized away completely. Or even signed char c = t; (on x86/amd64, if t is in the %eax register, after c = t, using c just means using %al. amd64 has byte addressing for every register, BTW. IIRC, in x86 some registers don't have byte addressing.)

Anyway, b = t; i = b; isn't the same as c = t; i = c; it's i = !!t; instead of i = t & 0xff;

Err, I guess everyone already knows all that from the previous replies. My point was, the warning made sense to me, since it caught cases where the compiler had to do things you didn't really tell it to, like !!BOOL on return because you declared the function bool, but are returning an integral value that could be true and != 1. e.g. a lot of windows stuff returns BOOL (int).

This is one of MSVC's few warnings that G++ doesn't have. I'm a lot more used to g++, and it definitely warns about stuff MSVC doesn't, but that I'm glad it told me about. I wrote a portab.h header file with stubs for the MFC/Win32 classes/macros/functions I used. This got the MFC app I'm working on to compile on my GNU/Linux machine at home (and with cygwin). I mainly wanted to be able to compile-test what I was working on at home, but I ended up finding g++'s warnings very useful. It's also a lot stricter about e.g. templates...

On bool in general, I'm not sure it makes for better code when used as a return values and parameter passing. Even for locals, g++ 4.3 doesn't seem to figure out that it doesn't have to coerce the value to 0 or 1 before branching on it. If it's a local variable and you never take its address, the compiler should keep it in whatever size is fastest. If it has to spill it from registers to the stack, it could just as well keep it in 4 bytes, since that may be slightly faster. (It uses a lot of movsx (sign-extension) instructions when loading/storing (non-local) bools, but I don't really remember what it did for automatic (local stack) variables. I do remember seeing it reserve an odd amount of stack space (not a multiple of 4) in functions that had some bools locals.)

Using bool flags was slower than int with the Digital Mars D compiler as of last year: http://www.digitalmars.com/d/archives/digitalmars/D/opEquals_needs_to_return_bool_71813.html (D is a lot like C++, but abandons full C backwards compat to define some nice new semantics, and good support for template metaprogramming. e.g. "static if" or "static assert" instead of template hacks or cpp macros. I'd really like to give D a try sometime. :)

For data structures, it can make sense, e.g. if you want to pack a couple flags before an int and then some doubles in a struct you're going to have quite a lot of.