Re: C++ exception context
jg wrote:
I also played with it and looked at the code generated. It looks
that the variables, even locals, will be stored into memory before
calling bar(), which is within try block and may throw an exception.
In a C program (not C++), a local variable usually stays
within a register accross a call (a performance advantage over
C++); whereas in C++, a local variable cannot stay in a
register accross a call within a try block.
It depends totally on the implementation, and exceptions have
very little effect here. Different implementations have
different conventions regarding what must be saved across the
context of a function call. Back when I was regularly working
on Intel architecture, for example, the convention was that a
function could use any register it wanted: it only had to
restore BP (the frame pointer), SP (obviously:-)) and the
segment registers. A compiler could not leave a value in a
register, and expect it to be there after a function call.
Today, on my Sparc, 16 registers are guaranteed safe, and a
compiler can leave a value in one of them even when calling a
function which may throw.
Exceptions affect optimization in two ways. First, they add
control flow paths which the compiler must take into account.
There are probably cases where this will result in saving a
value to memory, where it would otherwise be left in a register,
but they probably aren't very frequent. Secondly, it means that
any time a function which may throw is called, the stack
structure must be "clean"; it must be possible to walk back up
the stack using standard mechanisms. In practice, I don't think
that this ever affects optimization; I'm not aware of a compiler
that doesn't systematically build a clean stack structure in a
non-leaf function.
Exceptions can also have a positive effect on optimization.
First, because the test for the error condition simply isn't
present in the code of the intermediate functions, and secondly,
because the data used by the exception handling is out of line,
possibly in a separate segment, so the resulting function is
smaller, and more likely to fit into cache.
In practice, both effects seem to be pretty negligeable, and the
choix between exceptions or another error reporting mechanism
should be based on design, without regarding performance. Do
what's right; the probability that it will cause a performance
problem is so low that it's not worth worrying about.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34