Quick Q: Why can noexcept generate faster code than throw()? -- StackOverflow
Quick A: Because noexcept
doesn't have to keep track of stack unwinding.
noexcept, stack unwinding, and performance
The following draft from Scott Meyers new C++11 book says (page 2, lines 7-21)
The difference between unwinding the call stack and possibly unwinding it has a surprisingly large impact on code generation. In a
noexcept
function, optimizers need not keep the runtime stack in an unwindable state if an exception would propagate out of the function, nor must they ensure that objects in anoexcept
function are destroyed in the inverse order of construction should an exception leave the function. The result is more opportunities for optimization, not only within the body of anoexcept
function, but also at sites where the function is called. Such flexibility is present only fornoexcept
functions. Functions with “throw()
” exception specifications lack it, as do functions with no exception specification at all.In contrast, section 5.4 of "Technical Report on C++ Performance" describes the "code" and "table" ways of implementing exception handling. In particular, the "table" method is shown to have no time overhead when no exceptions are thrown and only has a space overhead.
My question is this - what optimizations is Scott Meyers talking about when he talks of unwinding vs possibly unwinding? Why don't these optimizations apply for
throw()
? Do his comments apply only to the "code" method mentioned in the 2006 TR?