[fpc-pascal] How is interface variable implemented?

Dennis dec12 at avidsoft.com.hk
Fri Aug 24 13:10:49 CEST 2018


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




More information about the fpc-pascal mailing list