[cfe-dev] Can noexcept impair performance?
rjmccall at apple.com
Mon Feb 11 12:01:02 PST 2013
On Feb 10, 2013, at 7:11 AM, Stephan Tolksdorf <st at quanttec.com> wrote:
> I'm asking this question on this list because answering it requires knowledge of compiler internals and it may be of general interest to C++ programmers, so I hope you don't consider it off-topic.
> I know that there are situations where a noexcept declaration can improve performance, because such a declaration may enable specialized code paths in library code or additional compiler optimizations.
> I wonder whether there are also situations where a noexcept declaration could impair performance. For example, I could imagine that a noexcept declaration might complicate or even suppress inlining if the compiler can't prove that the function code can never throw. Could this happen with Clang?
Exception specifications can impair performance in two ways:
1. Exception specifications are required to be enforced, so if a noexcept function calls anything that isn't noexcept, the compiler has to emit exception tables and handlers to ensure that std::terminate is called, increasing code size. (GCC-compatible personality functions support an abbreviation for this handler so that, in theory, the compiler need not use an actual landing pad. However, clang does not currently take advantage of this.)
2. When an operator new is noexcept, new expressions are required to check whether the result is null before calling the constructor. This introduces an extra branch on every allocation. Sadly, this applies even to the special global placement operator new.
More information about the cfe-dev