Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-08

How is interface variable implemented?

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis

if I have this test program below:

program testi;
{$INTERFACES CORBA}
uses sysutils;
type
   IInterfaceA = interface
      procedure Increment ;
   end;

  { TObjectA }

   TObjectA = class(IInterfaceA)
     Value : Int64;
     procedure Increment;
   end;
var
   c : int32;
   i : IInterfaceA;
   obj : TObjectA;
   aTime : TDateTime;

{ TObjectA }

procedure TObjectA.Increment;
begin
   Inc(Value);
end;
begin
   obj := TObjectA.Create;
   try
     aTime := now;
     for c := 1 to 100000000 do
         obj.Increment;
     aTime := now - aTime;
     Writeln('Object call Time Taken Seconds:', aTime*24*60*60);
   finally
     FreeAndNil(obj);
   end;
   obj := TObjectA.Create;
   try
     aTime := now;
     i := obj;
     for c := 1 to 100000000 do
         i.Increment;
     aTime := now - aTime;
     Writeln('Interface call Time Taken Seconds:', aTime*24*60*60);
   finally
     FreeAndNil(obj);
   end;
   Readln;
end.

The run results:
Object call Time Taken Seconds: 2.1899964194744825E-001 Interface call Time Taken Seconds: 2.7999999001622200E-001

so, the time difference is about 27%

My question is, where is this extra time spent?

I am assuming an interface variable has a table with entries storing ( pointer to the object instance AND an offset of method address in the Virtual Method Table of that object.)

Am I correct?

What is the memory occupied by the interface variable? I know the sizeof(i) = 8 (in 64 bit windows), but what is the size of the instance?

I cannot find any of this info on the web.

Thanks for your answer in advance.

Dennis

Reply by email

Sven Barth
to Dennis

Dennis schrieb am Fr., 24. Aug. 2018, 13:11:

Show quoted text 15 lines
The run results: Object call Time Taken Seconds: 2.1899964194744825E-001 Interface call Time Taken Seconds: 2.7999999001622200E-001 so, the time difference is about 27% My question is, where is this extra time spent? I am assuming an interface variable has a table with entries storing ( pointer to the object instance AND an offset of method address in the Virtual Method Table of that object.) Am I correct?

Using an interface is always calling virtual methods.

It's however a table that contains pointers to compiler generated wrappers that adjust the Self Pointer and call the correct method.

What is the memory occupied by the interface variable? I know the sizeof(i) = 8 (in 64 bit windows), but what is the size of the instance?

The instance is essentially only the VMT, cause for interfaces implemented by FPC classes the interface variable points to a shifted Self pointer that is "fixed" c the wrappers mentioned above.

Regards,
Sven

Reply by email