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

FeepingCreature feepingcreature at gmail.com
Sun Jun 18 02:33:27 UTC 2023


On Saturday, 17 June 2023 at 20:29:35 UTC, Dibyendu Majumdar 
wrote:
> On Saturday, 17 June 2023 at 17:16:16 UTC, FeepingCreature 
> wrote:
>>> I am looking for details of how interface method dispatch 
>>> works in D.
>
>> Note: While writing this answer, I found the actual specified 
>> answer in the D ABI docs: 
>> https://dlang.org/spec/abi.html#classes
>>
>> Anyway, how does all that compare to Java? No clue! Sorry. I 
>> can only assume it's pretty similar, because this is really is 
>> the "unambiguously sensible" way to do it.
>
> It seems closer to C++ design, as Java does not adjust an 
> object pointer when casting to an interface or subtype.

Ah, GPT-4 (and then Google) points me at 
https://wiki.openjdk.org/display/HotSpot/InterfaceCalls (It is 
impossible to directly google 'java interface implementation' 
because the results are far, far too flooded with beginner 
tutorials.)

To summarize, it looks like Java can do interface calls on the 
same reference as the object mostly by paying a lot of runtime 
cost to find the actual method in question. In comparison, the D 
approach can just call the interface method at a fixed vtable 
offset, as it does with class methods. Once the runtime has the 
interface vtable, calls proceed as in D.

I think comparatively, Java can more readily commit to overhead 
costs for interface resolution because in inner loops it expects 
to find a deterministic object type and create a direct call 
anyways. D has to compile in one shot and be done, so it has to 
be fast from the start.


More information about the Digitalmars-d mailing list