是否允许编译器优化抛出异常? [英] Are compilers allowed to optimize-out exception throws?

查看:140
本文介绍了是否允许编译器优化抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们今天在工作中一直在讨论这个主题,而我们谁也找不到关于该问题的明确答案。考虑以下情况:

We have been discussing this topic today at work, and none of us could come up with a definitive answer about that question. Consider the following situation:

int foo()
{
  int err;

  err = some_call(1);

  if (err != 0)
    return err;

  err = some_call(2);

  if (err != 0)
    return err;

  err = some_call(3);

  if (err != 0)
    return err;

  err = some_call(4);

  if (err != 0)
    return err;

  bar();

  return err;
}

有很多代码重复。显然,这可以使用宏进行分解,但遗憾的是不能使用模板进行分解(由于return子句)。或者至少不是直接。

There is a lot of code repetition. Obviously, this could be factorized with a macro, sadly not with a template (because of the return clause). Or at least not directly.

现在的问题是,如果我们要用异常替换那些返回错误代码,并立即捕获这些异常,则允许编译器并且很聪明足以检测出模式并避免完全抛出异常?

Now the question is, if we were to replace those return error codes with exceptions, and catching those exceptions right away, are compilers allowed and smart enough to detect the pattern and avoid throwing exceptions altogether ?

这里是我的意思的说明:

Here is an illustration of what I mean:

int foo()
{
  try
  {
    // some_call now throws a ErrorReturned exception that contains the error code upon failure.
    some_call(1);
    some_call(2);
    some_call(3);
    some_call(4);
  }
  catch (ErrorReturned& ex)
  {
    return ex.error_code();
  }

  bar();

  return 0;
}

现在,当前没有性能问题,所以是的,我们不需要优化甚至关心它。

Now, there is no current performance issue and so yes, we don't need to optimize or even care about that. This is more to understand what compilers are allowed to do.

总之,这是一种好的实践吗?如果是这样,编译器可以通过不抛出异常来优化它吗?完全没有? (假设异常构造没有副作用)

In short so, is it a "good" practice and if so, can compilers optimize that by not throwing exceptions at all ? (Assuming the exception construction has no side effect)

推荐答案

编译器足够聪明吗似乎暗示了异常不起作用一个项目中的目标,如果是这种情况,则不应该首先使用它们(除非您实际上确实有可能遇到异常)。

"Are compilers smart enough" seems to hint that Exceptions don't serve a purpose in your project and, if that's the case, you shouldn't use them in the first place (unless of course you actually have the possibility of getting an exception).

简短回答:否,编译器不会根据您使用的模式删除您的异常/异常处理。

Short-answer: No, compilers will not remove your exceptions / exception handling based on the pattern you're using them.

当您使用try / catch时,它处理的异常将添加到主异常表中;该表可以被监视,挂钩,添加和删除。仅仅因为您立即捕获到异常并不意味着其他事情也不会发生。

When you use a try/catch, the exception it handles is added to the main exception table; this table can be monitored, hooked into, added on and removed from. Just because you catch the exception right away doesn't mean other things aren't happening with it as well.

边源

优化C ++异常处理 概述了与异常有关的所有(当前几乎所有)优化实现。贯穿整个过程,它表明当前它们并没有在编译时剥离,而是对其进行了优化。本文本身建议对EH(异常处理)进行增强,以消除不必要的异常,并且总体而言,这是一本不错的文章。

Side Source:
A paper was written on Optimizing Away C++ Exception Handling that outlines all (almost all) current implementations of optimizations pertaining to exceptions. Throughout it, it shows that at the current time they are not stripped at compile-time but optimizations are made to them. The paper itself recommends enhancements to EH (Exception Handling) to remove the unnecessary exceptions and, overall, is a pretty good read.

UPDATE (其他资源)

进一步研究该主题,GCC编译器似乎优化掉异常;但是,它确实提供了这样做的选项: - fno-exceptions 。此选项将删除所有异常,并直接用 abort()调用替换它们。

UPDATE (additional sources)
Looking further into the topic, the GCC compiler appears to not optimize away exceptions; however, it does offer an option to do so: -fno-exceptions. This option will remove all exceptions and directly replace them with abort() calls.

另一个来源(在此处的StackOverflow 上)没有直接提及删除异常,而是概述了对异常进行的两种优化: setjmp / longjmp 和零成本。可以通过突出显示实际的增强功能来推断出,而没有提到完全消除异常,但没有这样的优化(但是,至少对于上述编译器而言)。可以在此处

Another source (here on StackOverflow) doesn't directly mention "removing exceptions" but does outline the two optimizations made to exceptions, setjmp/longjmp and Zero-Cost. It can be inferred by the highlighting of the actual enhancements without mention of "completely removing the exception" that there is no such optimization (yet, at least with the mentioned compilers). Another source with more detailed info on these optimizations can be found here.

这篇关于是否允许编译器优化抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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