允许编译器优化异常抛出吗?

Are compilers allowed to optimize-out exception throws?(允许编译器优化异常抛出吗?)

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

问题描述

我们今天在工作中一直在讨论这个话题,但我们谁也无法对这个问题给出明确的答案.考虑以下情况:

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 ?

这是我的意思的说明:

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 enough" 似乎暗示异常在你的项目中没有任何作用,如果是这样,你不应该使用它们首先(当然,除非您确实有可能获得异常).

"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.

旁注:
一篇论文写在 <代码>Optimizing Away 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.

更新(其他来源)
进一步研究该主题,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.

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

本文标题为:允许编译器优化异常抛出吗?

基础教程推荐