6

It seems to me that if you have some C++ code like this:

int f()
{
  try {
    if( do_it() != success ) {
      throw do_it_failure();
    }
  } catch( const std::exception &e ) {
    show_error( e.what() );
  }
}

The C++ compiler should be able to optimize the throw and catch into almost a simple goto.

However, it seems to me from my experience viewing disassembly and stepping through code that the compilers always jump through the very messy exception handling libraries.

Why do they do that? Is there some language requirement that prevents optimizing? What if it was:

int f()
{
  try { throw std::runtime_error("Boo!"); }
  catch ( const std::exception &e ) { std::cout << e.what() << std::endl; }
}

Why does the compiler not just rewrite that as

int f()
{
  std::cout << "Boo!" << std::endl;
}
4
  • 6
    Probably because in C/C++, exceptions are not designed for flow control, but for actual exceptions, and thus are viewed as somewhat sacrosanct from an implementation perspective.
    – Amber
    Jul 22, 2010 at 17:37
  • In your last case you assume that std::runtime_error() has a constructor and copy constructor that has no side effects. Even though this is true the compiler may not know this. Jul 22, 2010 at 18:44
  • @Martin: At least copy construction can be optimized away freely by compilers. You must not rely on copy constructors to be called.
    – sbi
    Aug 2, 2010 at 21:15
  • @sbi: Very true. But the other side of the coin you can not assume that they will be either. Aug 2, 2010 at 22:26

3 Answers 3

6

I think the accepted answer is quite uninformative if not wrong, so even after so many years I feel the need to offer a proper answer.

Speculating on why the compiler implementors chose to not put effort on any particular feature is just, well... speculation. The fact that exceptions are thrown only in exceptional circumstances is not generally felt as a reason to not optimize the performance of such code. On the contrary, even though it is true that throwing code is not optimized at the expense of non-throwing code, the exception throwing and handling infrastructure is nevertheless optimized very carefully.

Furthermore, that piece of code might feel so contrived that is not worth considering, but this is not true: it may result from inlining and optimizing of much more complex code, and optimizing it could result into simpler code that allows other optimization passes to fire, or the containing function to be further inlined. Optimization passes like these, when correct and efficient to implement, are always worth of at least being considered, no matter how contrived the original piece of code might look. Otherwise, even fundamental passes like dead code elimination would be avoided because "dead code should not be written in the first place". Which is obviously not the case.

Hence I just do not agree with the accepted answer. The fact that exceptions should be thrown exceptionally is not the reason why this code is not optimized.

The reason is purely technical, and is explained in this email from the clang development mailing list: http://lists.llvm.org/pipermail/cfe-dev/2015-March/042035.html

To summarize, the language allows code called inside the catch block to rethrow the exception in any point "without ever having seen" the exception object:

void g() { throw; }

Hence, consider the OP code:

int f()
{
  try { throw std::runtime_error("Boo!"); }
  catch ( const std::exception &e ) { std::cout << e.what() << std::endl; }
}

For what the compiler is concerned, e.what(), or the two invocations of operator<<, may rethrow the exception, hence optimizing away the exception handling code would break the semantics of the program.

Ensuring this is not the case would require "whole-program knowledge", as written in the above email message. Even simpler cases could be optimized, such as:

int func() {
  try {
    throw 42;
  }catch(int x) {
    return x;
  }
}

The above code can be transformed into return 42. There are no technical reasons that impede it.

Still, most common compilers do not do it (godbolt). This time, we can tell from an actual source, the email linked above, that Clang developers (we cannot say anything for other compilers) do not think this optimization to be worth, probably because it would only apply to catch blocks that do not do function calls.

Anyway, the message says nothing about whether they would accept a patch to do so.

3
  • Now that clang has LTO (link time optimization) in place that allows compiler to look at the program as a whole instead of just as individual object files, I think it is entirely possible for such kind of optimization to be done in clang and optimize every try-catch that happens inside the same program/library into goto.
    – JiaHao Xu
    May 1, 2021 at 9:53
  • @JiaHaoXu: LTO can't see inside pre-compiled functions in shared libraries like libc.so and libstdc++.so. e.what() might just be a template that it can see into, but cout::operator<< in most implementations eventually involves a potential call to a non-inline function. (At the very least, a potential low-level write system call, although in Clang with libstdc++ or libc++, separately-compiled library code provides some higher level functionality as well.) Unless the functions the optimizer can't see are all noexcept, that appears to be a showstopper. Sep 24, 2023 at 12:06
  • That’s true. Indeed even functions of C linkage may throw, because C++ exceptions pass through the C ABI. So the compiler really cannot know. Sep 24, 2023 at 15:05
5

Because do_it() could throw a different exception, before you throw do_it_failure();

As for your second example, a compiler could do it, but it would have to be treated as a special case, so why bother for such a pathological case?

1
  • 1
    The function could throw a different exception. But the compiler knows that at least one execution path does throw and is caught immediately so for that path it could jump directly to the catch block. If it wanted to.
    – Zan Lynx
    Jul 23, 2010 at 15:45
5

Why do they do that?

Because C++ exception are for, well, exceptional circumstances and performance under exceptional circumstances doesn't really matter.

C++' exceptions were designed with that in mind, making sure compiler vendors could deliver near-optimal performance in the common case of no exceptions being thrown, at the cost of worse-than-possible performance in the odd case when exceptions are thrown.

From the very beginning users were encouraged to use exceptions only under exceptional circumstances, and implementers were encouraged to optimize the no-exception case (destructor addresses must be stored somewhere in order to call destructors when an exception comes by) at the cost of the exceptional case.
And while implementers could certainly spend resources at also optimizing the odd exceptional case, most users wouldn't like that, since there's always so much more important things that need improvment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.