Boost logo

Boost :

From: Anthony Liguori (anthony_at_[hidden])
Date: 2003-09-07 15:14:06


Iain K. Hanson wrote:

>Which leads me to the realisation that I have been testing the wrong thing.
>I should have been testing virtual versus non-virtual SI and not SI versus
>MI.
>
>
I just ran some tests using the following program:

struct exception {
  const char *what() {
    return "exception";
  }
};
 
struct child_exception {
  const char *what() {
    return "child_exception";
  }
};
 
int main(int, char **)
{
  const char *reason;
 
  try {
    throw exception();
  } catch (exception &e) {
    reason = e.what();
  }
 
  try {
    throw child_exception();
  } catch (exception &e) {
    reason = e.what();
  }
}

And then with the following patch (to test for virtual single inherentence):

--- si.cpp 2003-09-07 14:55:57.000000000 -0500
+++ vsi.cpp 2003-09-07 14:56:23.000000000 -0500
@@ -1,5 +1,5 @@
 struct exception {
- const char *what() {
+ virtual const char *what() {
     return "exception";
   }
 };

I ran gcc 3.2.3 once with no optimization (gcc -S si.cpp) and once with
-O4 (gcc -O4 si.cpp) and examined the difference in assembly output for
the virtual and non-virtual examples. I won't include the assembly here
(it's easy enough to recreate on your own) but here's what I found:

The only differences between the non-virtual and virtual examples were
in initially populating the data fields (I believe this was just to take
care of the vtable) which only added one additional movl instruction,
calling the actual functions (it was a function pointer call verses a
direct call), and then in handling the catch (which was only an
additional two movl instructions--again, presumably to take care of the
vtable).

While the difference between the optimized and unoptimized code
generation was significant, the differences between the optimized
non-virtual and virtual exception handling were identical to the
differences between unoptimized non-virtual and virtual exception handling.

This leads me to conclude that the only cost in using virtual SI with
exceptions verses non-virtual SI with exceptions is simply the normal
virtual inherentence overhead of vtable management and a function
pointer call.

Presumably, the great difference in code generation you saw had more to
do with the difference between the combination of virtual MI verse
non-virtual SI. Of course, my tests may be overly simplistic.

Hope this helps,
Anthony Liguori


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk