Get the latest tech news
Compilers are too smart
6/Jun 2024 Last week I noticed some unexpected assembly code in a function calling std::unordered_map::find (yes, I know, friends don’t let friends use STL, but that is a topic for another discussion). I decided to investigate a bit and it turned out to be quite a journey.
It was not for me either, but it was familiar enough and ‘bit-twiddly’ enough to find an answer with some quick web search - it basically counts the number of set bits (1s) in a word. find calls constrain_hash(multiple times) – it basically converts a potentially very big hash to 0->bucket count range… MSVC does a decent job, but it repeats some of the calculations, but if you can convince Clang to drop popcnt(for example by using ‘old’ constrain_hash) it generates a pretty nice code with basically 2 distinct loops.
Or read this on Hacker News