Re:Python 3.11: “Zero cost” exception handling
by bregma on Wednesday October 06, 2021 @11:03AM
(#28773992)
Yeah, noexcept(true) is identical to wrapping the function with a try-catch construct in which the catch clause simply calls std::terminate(). Your godbolt example doesn't include the definition of n() so it doesn't show that. Adding noexcept(true) has a cost (because it's a kind of exception handling) but also allows the compiler to optimize out some of that cost under some circumstances.
Nevertheless, an example that demonstrates the exception path doesn't say much about the "zero-cost" non-exception path. In C++ exception handling is expensive. Outside of actually directly handling exceptions they have zero cost. There are no generated instructions. They do not consume cache. They do not hinder further optimizations.
Re:Python 3.11: “Zero cost” exception handling
by zeotroph on Wednesday October 06, 2021 @12:23PM
(#28774968)
Indeed, in the following case of a 4-deep call stack, each with its own exception, the bulk of the handling code can be moved elsewhere (already marked as cold by the compiler), but nonetheless there are a few instructions which won't matter in most cases but are still required to jump there and thus end up in the instruction cache.
Re:Python 3.11: “Zero cost” exception handling
by vlovich123 on Wednesday October 06, 2021 @11:03AM
(#28773985)
Isn’t exception handling code placed in the cold section of the binary these days so that the impact on cache is nonexistent?
Yup, the generated assembly does this. There’s some minimal extra instructions still to setup the frame but the bulk of the exception handling lives elsewhere.
1: https://godbolt.org/z/bKfG14P64 - the difference between `e()` and `n()` is that one is marked `noexcept`. Both `f()` and `g()` run directly to `ret` (return) in the normal case.