Re:C++'s `Noexcept` Can (Sometimes) Help (Or Hurt) Performance
by jzwinck on Monday August 05, 2024 @04:36PM
(#41165344)
There is another standard library related scenario: hash tables. The std unordered containers will store the hash of each key unless your hash function is noexcept. Analogous to how vector needs noexcept move for fast reserve and resize, unordered containers need noexcept hash to avoid extra memory usage. See https://gcc.gnu.org/onlinedocs/libstdc++/manual/unordered_as...
Re:C++'s `Noexcept` Can (Sometimes) Help (Or Hurt) Performance
by anonymoushn on Tuesday August 06, 2024 @02:58AM
(#41168437)
For many key types and access patterns, storing the hash is faster anyway. I assume people who care about performance are already not using std::unordered_map though.
Re:C++'s `Noexcept` Can (Sometimes) Help (Or Hurt) Performance
by 10tacobytes on Monday August 05, 2024 @06:58PM
(#41166393)
This is the correct analysis. The article's author could have saved themselves (and the reader) a good amount of blind data diving by learning more about exception processing beforehand.
Platforms with setjmp-longjmp based exceptions benefit greatly from noexcept as there’s setup code required before calling functions which may throw. Those platforms are now mostly gone, though. Modern “zero cost” exceptions don’t execute a single instruction related to exception handling if no exceptions are thrown (hence the name), so there just isn’t much room for noexcept to be useful to the optimizer.
Outside of those two scenarios there isn’t any reason to expect noexcept to improve performance.