Runtime c ++ try blocks

Possible duplicate:
C ++ Exception Handling Measurement
Performance if exceptions are not thrown (C ++)

I heard anecdotally that using try blocks in C ++ slows down the code at runtime, even if no exceptions occur. I searched, but could not find any explanation or justification for this. Does anyone know if this is true, and if so, why?

+6
source share
2 answers

The answer, as usual, is "dependent on."

It depends on how your exception handler is implemented by your compiler.

If you use MSVC and target 32-bit Windows, it uses a stack-based mechanism that requires a specific installation code each time you enter a try block, so yes, that means you are fined anytime you enter such a block, even if no exception is thrown.

Almost every other platform (other compilers, as well as MSVC targeting for 64-bit Windows) uses a tabular approach, in which static tables are generated during compilation, and when an exception is thrown, a simple table lookup and no try code are needed in the try block.

+17
source

There are two general ways to implement exceptions.

One, sometimes called a “table” or “DWARF,” uses static data to indicate how to unwind the stack from anywhere; this does not have excessive run-time costs, unless an exception is thrown.

Another, sometimes called "stack-based", "setjmp-longjmp" or "sjlj", supports dynamic data to indicate how to disable the current call stack. Each time you start or exit the try block, it has certain overhead at runtime, and whenever you create or destroy an automatic object with a non-trivial destructor.

The first one is more common in modern compilers (of course, GCC has been doing this by default for many years); you will need to check your compiler documentation to see what it uses and whether it is customizable.

+6
source

Source: https://habr.com/ru/post/905629/


All Articles