Looking for details of how interface method tables are implemented in D

FeepingCreature feepingcreature at gmail.com
Fri Jun 23 12:32:54 UTC 2023


On Friday, 23 June 2023 at 09:53:25 UTC, Dibyendu Majumdar wrote:
> Follow up question.
>
> If a class implements 5 interfaces.
>
> I create an object of this class.
>
> Is it correct that the resulting object has 6 vtable pointers 
> embedded, one for the class, and 5 for the interfaces?
>
> And every object that is allocated will have this overhead?
>
> In Java land, no vtables are embedded in the object.

Yep, that's correct. Though note this only applies to interfaces 
that are direct parents of the class; interfaces that are parents 
of other interfaces don't count. However conversely you do still 
need space for the interface pointers of your superclass.

Java is a dynamically compiled language. This allows it to take 
liberties that are simply unfeasible for a statically compiled 
language. D cannot specialize a method call for a particular 
class at runtime; it has to be fast on its first execution, and 
every successive one. So trading off space for speed makes sense 
here.

That said, if you're creating an object with six interfaces, 
which you are instantiating so often that its size matters, 
you're almost certainly writing unidiomatic D. Keep in mind that 
if you write D as if it was Java, you will be setting yourself up 
for GC and performance pain down the road.


More information about the Digitalmars-d mailing list