[fpc-pascal] interfaces and smartpointers [was Traits Proposal]

Benito van der Zander benito at benibela.de
Wed Feb 17 19:11:06 CET 2021


Hi,

> 1) Implicit cast to ITest which does a runtime lookup (but there's interface name so no string comparison like Supports?)

just open the disassembler window in Lazarus and single step through all 
the instructions. Then you see everything

create alone calls a bunch of methods:

TInterfacedObject.NewInstance
TObject.NewInstance
getmem
TObject.InitInstance
fillchar
InitInterfacePointers
fpc_pushexceptaddr (on linux only?)
fpc_setjmp
TInterfacedObject.AfterConstruction


(interfaces with TInterfacedObject are even worse than implementing the 
interface yourself )

The implicit casting is very fast, a single assembly instruction:

add    $0x20,%rsi

But then it also calls fpc_intf_assign for reference counting


> 2) calling "println" there is a call to a wrapper function (called "thunks")?

Yes. Basically the wrapper function casts the interface back to the 
class (sub    $0x20,%rdi) before calling the actual method. Because the 
class method assumes self is the class and not an interface




Bye,
Benito
On 17.02.21 17:37, Ryan Joseph via fpc-pascal wrote:
>
>> On Feb 17, 2021, at 8:27 AM, Benito van der Zander via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>
>> var c: ITest;
>> begin
>>    c := TTest.Create(123);
>>    c.println;
> So this is where you're getting your performance penalties? Correct me if I'm wrong but two things happen here:
>
> 1) Implicit cast to ITest which does a runtime lookup (but there's interface name so no string comparison like Supports?)
> 2) calling "println" there is a call to a wrapper function (called "thunks")?
>
> Regards,
> 	Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210217/5cee9740/attachment.htm>


More information about the fpc-pascal mailing list