This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

virtual inheritance: optimization for empty objects


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]