Andrei Alexandrescu wrote:
I seem to recall that interface dispach in D does a linear search in the interfaces list, so you may want to repeat your tests with a variable number of interfaces, and a variable position of the interface being used.

Such numbers are not interesting to me. On average, each class I write implements one interface. I rarely use inheritance and interfaces in the same class.

But your information is incorrect. Here's what happens:

object of class A
| vtable
|   | classinfo pointer
|   | methods...
| fields...
| interface vtable
|   | struct Interface*
|   | methods

struct Interface
{
   ptrdiff_t this_offset;
   ClassInfo interfaceInfo;
}

There are two ways to implement interface calls with this paradigm. The compiler way:

interface I
{
   void doStuff(int arg);
}
class A
{
   void doStuff(int arg) { writefln("do stuff! %s", arg); }

   // this method actually goes into the interface vtable
   ReturnType!doStuff __I_doStuff(ParameterTypeTuple!doStuff args)
   {
      auto iface = cast(Interface*)this.vtable[0];
      this = this + iface.this_offset;
      return doStuff(args);
   }
}


You can also do it with the runtime, but that's a lot harder. It would be effectively the same code.

Reply via email to