This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
virtual inheritance: optimization for empty objects
- To: egcs at egcs dot cygnus dot com
- Subject: virtual inheritance: optimization for empty objects
- From: Alexander Schiemann <aschiem at count dot math dot uni-sb dot de>
- Date: Thu, 4 Mar 1999 22:05:05 +0100 (MET)
Hi,
deriving a class B from class A without adding a data-member
does not increase the size.
But with virtual inheritance egcs-1.1.1 does not take advantage
of such a situation. Of course, some virtual pointer had to
be added. But we need only the locations of non-empty blocks
of data assigned to some virtual bases.
The easiest example is the following multiple derivation:
struct A{
int a; // some data
};
struct B1:virtual A{}; // no additional data
struct B2:virtual A{}; // no additional data
struct C:public B1,public B2{};
We get the sizes:
sizeof A=4
sizeof B1=8
sizeof B2=8
sizeof C=12
So we have two redundant virtual pointers in C.
We could let B1 and B2 overlap in C (both consist of the
same virtual pointer).
With additional levels of virtual bases the possible gain
increases quickly. Even if only some of the derived classes do not
add data it seems possible (though not entirely trivial) to
get rid of many of the virtual pointers.
I think that quite frequently abstract classes consist only of
virtual functions. I encountered this situation modeling hierarchies
of mathematical objects (that have a partial order of generalization).
In this context every base-class occurs once in an object and only the
lowest base-class and the most derived classes have some data.
Now I would like to know
- does this suggestion conform with the standard (two base subobjects
having the same address)? I checked it, but perhaps
I missed some requirement.
- are there compilers that implement this kind of layout?
Thanks
Alexander Schiemann