De Morgan’s Law optimization with overloaded operators

Note that: Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands. … Because the short-circuiting properties of operator&& and operator|| do not apply to overloads, and because types … Read more

Why can’t GCC optimize the logical / bitwise AND pair in “x && (x & 4242)” to “x & 4242”?

Exactly why should it be able to optimize the code? You’re assuming that any transformation that works will be done. That’s not at all how optimizers work. They’re not Artificial Intelligences. They simply work by parametrically replacing known patterns. E.g. the “Common Subexpression Elimination” scans an expression for common subexpressions, and moves them forwards, if … Read more

Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

The overarching difference here is that the first version is branchless. 16 isn’t the length of any string here (the longer one, with NUL, is only 15 bytes long); it’s an offset into the return object (whose address is passed in RDI to support RVO), used to indicate that the small-string optimization is in use … Read more

Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?

If you generate code for a little-endian platform, optimizing four-byte memcmp for inequality to a single DWORD comparison is invalid. When memcmp compares individual bytes it goes from low-addressed bytes to high-addressed bytes, regardless of the platform. In order for memcmp to return zero all four bytes must be identical. Hence, the order of comparison … Read more

Compiler stops optimizing unused string away when adding characters

This is due to the small string optimization. When the string data is less than or equal 16 characters, including the null terminator, it is stored in a buffer local to the std::string object itself. Otherwise, it allocates memory on the heap and stores the data over there. The first string “ABCDEFGHIJKLMNO” plus the null … Read more

Why is “while (i++ < n) {}" significantly slower than "while (++i < n) {}"

As others have pointed out, the test is flawed in many ways. You did not tell us exactly how you did this test. However, I tried to implement a “naive” test (no offense) like this: class PrePostIncrement { public static void main(String args[]) { for (int j=0; j<3; j++) { for (int i=0; i<5; i++) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)