noexcept, stack unwinding and performance

后端 未结 4 1548
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 09:47

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 i

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 10:36

    There's "no" overhead and then there's no overhead. You can think of the compiler in different ways:

    • It generates a program which performs certain actions.
    • It generates a program satisfying certain constraints.

    The TR says there's no overhead in the table-driven appraoch because no action needs to be taken as long as a throw doesn't occur. The non-exceptional execution path goes straight forward.

    However, to make the tables work, the non-exceptional code still needs additional constraints. Each object needs to be fully initialized before any exception could lead to its destruction, limiting the reordering of instructions (e.g. from an inlined constructor) across potentially throwing calls. Likewise, an object must be completely destroyed before any possible subsequent exception.

    Table-based unwinding only works with functions following the ABI calling conventions, with stack frames. Without the possibility of an exception, the compiler may have been free to ignore the ABI and omit the frame.

    Space overhead, a.k.a. bloat, in the form of tables and separate exceptional code paths, might not affect execution time, but it can still affect time taken to download the program and load it into RAM.

    It's all relative, but noexcept cuts the compiler some slack.

提交回复
热议问题