Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance when exceptions are not thrown (C++)

Tags:

c++

exception

I have already read a lot about C++ exceptions and what i see, that especially exceptions performance is a hard topic. I even tried to look under the g++'s hood to see how exceptions are represented in assembly.

I'm a C programmer, because I prefer low level languages. Some time ago I decided to use C++ over C because with small cost it can make my life much easier (classes over structures, templates etc.).

Returning back to my question, as I see exceptions do generate overhead bud only when they occur, because it require a long sequence of jumps and comparisons instructions to find a appropriate exception handler. In normal program execution (where is no error) exceptions overhead equals to normal return code checking. Am I right?

like image 256
Goofy Avatar asked Sep 19 '10 08:09

Goofy


People also ask

What will happen if a thrown exception is not handled in CPP?

Explanation: As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped).

Does exception handling slow down code?

If not used correctly, exceptions can slow down your program, as it takes memory and CPU power to create, throw, and catch exceptions. If overused, they make the code difficult to read and frustrating for the programmers using the API.

Why are exceptions slow?

What does it mean that exceptions are “slow”? Mainly it means that a throw can take a Very Long Time™ compared to e.g. an int assignment, due to the search for handler.

How do I avoid exceptions?

Another way to avoid exceptions is to return null (or default) for extremely common error cases instead of throwing an exception. An extremely common error case can be considered normal flow of control. By returning null (or default) in these cases, you minimize the performance impact to an app.


2 Answers

Please see my detailed response to a similar question here.

Exception handling overhead is platform specific and depends on the OS, the compiler, and the CPU architecture you're running on.

For Visual Studio, Windows, and x86, there is a cost even when exceptions are not thrown. The compiler generates additional code to keep track of the current "scope" which is later used to determine what destructors to call and where to start searching for exception filters and handlers. Scope changes are triggered by try blocks and the creation of objects with destructors.

For Visual Studio, Windows, and x86-64, the cost is essentially zero when exceptions are not thrown. The x86-64 ABI has a much stricter protocol around exception handling than x86, and the OS does a lot of heavy lifting, so the program itself does not need to keep track of as much information in order to handle exceptions.

When exceptions occur, the cost is significant, which is why they should only happen in truly exceptional cases. Handling exceptions on x86-64 is more expensive than on x86, because the architecture is optimized for the more common case of exceptions not happening.

like image 109
Chris Schmich Avatar answered Oct 04 '22 00:10

Chris Schmich


Here's a detailed review of the cost of the exception handling when no exceptions are actually thrown:

http://www.nwcpp.org/old/Meetings/2006/10.html

In general, in every function that uses exception handling (has either try/catch blocks or automatic objects with destructor) - the compiler generates some extra prolog/epilog code to deal with the expcetion registration record.

Plus after every automatic object is constructed and destructed - a few more assembler commands are added (adjust the exception registration record).

In addition some optimizations may be disabled. Especially this is the case when you work in the so-called "asynchronous" exception handling model.

like image 31
valdo Avatar answered Oct 04 '22 00:10

valdo