Discussion:
[fpc-pascal] How is interface variable implemented?
Dennis
2018-08-24 11:10:49 UTC
Permalink
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

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pasca
Sven Barth via fpc-pascal
2018-08-25 11:08:21 UTC
Permalink
Post by Dennis
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.
Post by Dennis
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

Loading...