问题描述
以下来自Scott Meyers的C ++ 11新书的 (第2页第7-21行)
The following draft from Scott Meyers new C++11 book says(page 2, lines 7-21)
对比, 5.4
的描述了实现异常处理的代码和表方式。特别是,表方法显示没有时间开销,当没有异常抛出,只有一个空间开销。
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.
我的问题是这是什么优化斯科特·迈尔斯谈到他什么时候谈到放松与可能放松?为什么这些优化不适用于 throw()
?他的注释仅适用于2006年TR?
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?
推荐答案
中提到的代码方法。 开销。你可以用不同的方式来考虑编译器:
There's "no" overhead and then there's no overhead. You can think of the compiler in different ways:
- 生成一个执行某些动作的程序。
- 它产生一个满足一定约束的程序。
TR说在表驱动appraoch中没有开销,只要没有发生抛弃。非异常执行路径很简单。
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.
基于表的展开仅适用于遵循ABI调用约定的函数和堆栈框架。没有可能的异常,编译器可以自由地忽略ABI并省略该框架。
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.
空间开销,又称为膨胀,以表格的形式和单独异常代码路径,可能不会影响执行时间,但它仍然可能会影响下载程序并将其加载到RAM中所花费的时间。
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.
这是相对的,但 noexcept
削减编译器一些松弛。
It's all relative, but noexcept
cuts the compiler some slack.
这篇关于noexcept,堆叠展开和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!