noexcept,堆叠展开和性能 [英] noexcept, stack unwinding and performance

查看:120
本文介绍了noexcept,堆叠展开和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下来自Scott Meyers的C ++ 11新书的草稿 (第2页第7-21行)

The following draft from Scott Meyers new C++11 book says(page 2, lines 7-21)


解开调用栈和可能解开调用栈之间的区别是令人惊讶的是
对代码生成的巨大影响。在noexcept函数中,优化器
不需要将运行时栈保持在不可解释状态,如果一个异常将
传播出函数,也不能确保noexcept
函数中的对象被破坏在相反的构造顺序应该有一个异常
离开函数。结果是更多的优化机会,不仅在noexcept函数体内的
,而且在函数
调用的站点。这种灵活性仅存在于noexcept函数。函数
throw()异常规范缺少它,函数没有任何异常规范。

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 a noexcept 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 a noexcept function, but also at sites where the function is called. Such flexibility is present only for noexcept functions. Functions with "throw()" exception specifications lack it, as do functions with no exception specification at all.

对比, 5.4 技术C ++性能报告描述了实现异常处理的代码和表方式。特别是,表方法显示没有时间开销,当没有异常抛出,只有一个空间开销。

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,堆叠展开和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆