Search code examples
classdoverhead

Overhead of D classes


I thought I heard that D classes have two words (2 void*) of overhead instead of the one word required by C++. I also heard that the vtable layout is incompatible with C++. Do I remember correctly? If so what is the rationale for these decisions?


Solution

  • A few things:

    1. In C++, a class that has no virtual functions will have zero overhead.
    2. In D, a class always inherits virtual functions from Object, so it always has that __vptr overhead, but also has a __monitor, which C++ class objects don't have.
    3. In both C++ and D, there will be additional vptr for each interface that the class implements.

    The vtable layout is incompatible with C++ because D includes a pointer to a TypeInfo instance, which has runtime type information about the class. C++ obviously doesn't have that, so it is incompatible.