Get the latest tech news

Noexcept affects libstdc++’s unordered_set


The other day I learned a new place where adding or removing noexcept can change the performance of your program: GNU libstdc++’s hash-based associative containers change the struct layout of their nodes depending on the noexceptness of your hash function. This is laid out fairly clearly in the docs; it’s simply bizarre enough that I’d never thought to look for such a thing in the docs!

But woe betide the hapless programmer who defines YourHash::operator() const noexcept — their hash function will be called many more times than it should, resulting in a massive slowdown (on this workload, anyway). The third column illustrates the reverse effect: libstdc++ does not recognize that hashing a vector<bool> is expensive, so they save 8 bytes and take a massive slowdown by default. (In short: They should get rid of the blacklist; pay the 8 bytes by default; introduce a whitelist for trivial hashers specifically; stop checking noexceptness for any reason.)

Get the Android app

Or read this on Hacker News

Read more on:

Photo of libstdc++

libstdc++