r/cpp • • Nov 02 '26

Catastrophic misoptimization of byte-range equality comparison in all major C++ compilers (and Rust). Language issue or compiler issue?

Someone brought a C++ snippet that reduced to the following (std::array used instead of char* for consistency for later)

bool eq2(const std::array<char, 64>& lhs, const std::array<char, 64>& rhs) {
    for (int i = 0; i < 64; ++i)
        if (lhs[i] != rhs[i])
            return false;
    return true;
}

It basically returns true iff two 64 byte memory regions are a bitwise match. You'd think this can be optimized by modern compilers flawlessly, right? Right?! And the worst thing that can happen is it doesn't get autovectorized, right? Right?!

Well, to everybody's surprise... https://godbolt.org/z/v4d8G5ovh

GCC attempts to partially conditionally vectorize but doesn't want to use unaligned loads. Moreover its vectorization is way more cumbersome than it needs to be. Then after it finds a mismatch it... reverts to the unvectorized version?! Clang and MSVC do not even attempt to vectorize it - and in a way it makes them end up better than GCC, which is wild.


Now, to try to investigate we've checked how it looks in Rust. https://godbolt.org/z/K7xE3qc94. It does what GCC does but fully vectorized.

At this point we're confused as to what the source of the problem is. Are we missing something with respect to what transformations are allowed? Is it really that much to ask from the as-if rule?

As a sanity check we use std::array's operator==. https://godbolt.org/z/xPhPYMjvz. Oh would you look at that? Now the compilers can do well (ok not you MSVC. GCC also fumbles the flag register slightly). The situation is the same in Rust - https://godbolt.org/z/3GjWorze8. Not only does it vectorize correctly but the vectorized code is better - using vpxor.


Ultimately this isn't just academical. Situations like this undermine the trust in the compiler at the most fundamental level. Not every place can use std::array, not everyone can use ranges and span yet. We could use std::equal but why does it matter so much? What if != changes to < and there's no good solution anymore? To what degree are we supposed to be paranoid about basic optimizations being done or not?

The similarity of codegen between Rust and C++ would point to it being either a compiler issue or a fundamental code correctness issue. The fact that C++ Clang differs from Rust (LLVM) would suggest it's at least partially a language issue. Why is this extremely simple code so nasty?


edit. some more [not so] fun findings https://www.reddit.com/r/cpp/comments/1xlyti8/catastrophic_misoptimization_of_byterange/19s1i09srxdaa/

100 Upvotes

122 comments sorted by

View all comments

Show parent comment

4

u/13steinj 29d ago

I consider such a missed optimization a catastrophic result. I have too many engineers (read: more than 0 normally, more than 10 because of LLMs) writing manual ASM and/or intrinsics rather than trusting the compiler.

If it can't do the (seemingly) simple cases, it gives this group a pass to continue doing what they are doing.

12

u/CocktailPerson 29d ago

Those people will always find a way to justify writing assembly and intrinsics, because they think writing assembly and intrinsics is fun. They have a vested interest in distrusting the compiler.

If someone uses this as a reason to write assembly or intrinsics when == is right there and does what they want, then they are not a serious person and shouldn't be treated as such.

4

u/13steinj 29d ago

Firstly, this is a reduced case. Does == work if on a struct that contains one character but does not itself have an operator==? This code is reasonable and can exist-- private methods that understand the internal layout and semantic of related library types, but has no external semantic for equality. In such cases, == is not "right there and does what they want."

Secondly, it's usually not vested interest, but rather paranoia, that comes from cases like these.

2

u/CocktailPerson 28d ago

First, doesn't matter. Reduced cases the only ones for which writing assembly is a feasible alternative. If you dismiss enough of these silly "reduced cases" with "write idiomatic C++," you eventually get cases that are too complex to justify writing in assembly anyway. And then you just make them write non-idiomatic but performant C++ instead.

I'm not sure I get the point of your char-struct example either. Would people really suggest writing something with non-primitive types like that in assembly? If somebody uses that as an example of why you can't blindly trust the compiler, then sure, but if someone uses that as an example of why you can't blindly trust the compiler and therefore need to write it in assembly instead, then again, they're not a serious person.

Second, it is vested interest, expressed as paranoia. They want to write assembly, so they concern-troll about cases like these instead of just inspecting the compiler output and reasoning about how to change the source code to improve the output. Again, these are not serious people. You can just reject their silly little assembly routines, tell them to write idiomatic C++, and ignore them.

1

u/FrogNoPants 28d ago

I write C++ code that uses intrinsics every day, and I rarely drop down to "assembly" or even raw intrinsics, we can wrap intrinsics in libraries easily enough these days, so it isn't just for reduced cases--it applies to pretty much anything that is a bottleneck.
I'm not going to inspect the compilers output on every update or minor code change, that is a waste of time. Also I have far too much code that uses intrinsics under the hood to be bothered with that.

3

u/cd_fr91400 29d ago

AO wrote "Someone brought a C++ snippet that reduced to the following...".

The important word here is "reduced".
It does not mean their actual case is a == case. It means they could simplify their case to such a simple one, so we, as redditors, can understand it.

I am not strongly opposed to the first part. I am not advocating for writing assembly and intrinsics. I just say these persons *may* have good reasons to do so. I do not know their case, I have no opinion about their seriousness.