I'm looking at some old code I inherited, and I really don't like the style at places. One of the things I really don't like the look of, is something like:
bool func() {
bool ret = true;
ret &= test1();
homemade_assert_like_function(ret == true);
ret &= test2();
homemade_assert_like_function(ret == true);
return ret;
}
Where I think the following looks much cleaner:
bool func() {
homemade_assert_like_function(test1());
homemade_assert_like_function(test2());
return true; // just to keep the interface
}
However, the latter produces more assembler code. Not surprisingly, if I change the &=
from the first example to =
the result is the same as assert(test());
, but still &=
seems to be better
- With &= (63 lines of asm): https://godbolt.org/z/DJWoJw
- Without &= (81 lines of asm): https://godbolt.org/z/kisyR4
- With = (81 lines of asm): https://godbolt.org/z/CaZsNe
I use gcc5.4 for mips with -O2 for all three examples, but the result is similar if I use gcc for pc. If I compile with -std=c++14 (and a newer compiler) the three examples produce the same code.
Can anyone explain this to me? Is it just my test code that is really bad?
EDIT:
I made a new code example, fixing the broken assert. It did have an interesting side effect. The size difference is much smaller now, indicating to me that it must be that the optimizer can do some magic in the &=
case.
As having been pointed out multiple times already, the example code is somewhat obfuscated. But it shows the same as I see on the real code - that changing nothing but removing the &=
style of code, make the code base increase.