[fpc-pascal] Traits Proposal

Benito van der Zander benito at benibela.de
Tue Feb 16 14:51:55 CET 2021


>
> There are no significant performance implications of interfaces. 
> They're essentially a virtual method call, something that one is doing 
> all day long with Object Pascal classes. 


Interfaces are extremely slow. Virtual method calls are also slow. I 
have been using interfaces for reference counting, and have been 
thinking to replace it all with managed records because they are so slow 
(unfortunately records can also be slow because fpc does not always keep 
them in registers even if they are pointer sized)

There we have:

* slower creation of the class, because each implemented interface adds 
another VMT reference to the class which needs to be initialized.

* slower method calling because of the wrapper function

* extremely slow casting an interface to the class. Never cast an 
interface, better add a method to the interface returning a class reference

* slow reference counting. Especially if it is thread safe and exception 
safe with the implicit exception block


On 14/02/2021 11:31, Sven Barth via fpc-pascal wrote:
> Am 14.02.2021 um 01:09 schrieb Ben Grasset via fpc-pascal:
>> This seems possibly a *little* too similar to the existing Interface 
>> type in Object Pascal, however, I *would* really like to see some 
>> kind of functionality that basically amounts to "has the same 
>> capabilities as Interfaces and works on records and objects too, but 
>> does NOT require any kind of heap allocation".
>
> My personal idea for this would be to allow for duck typing regarding 
> interfaces: as long as the methods of the class, object or record 
> satisfy those of the interface then it's possible to assign it. In 
> case of objects and records one would mainly use raw interfaces (aka 
> corba ones) instead of reference counted one as otherwise one would 
> need to provide the methods of IInterface as well though one might not 
> be able to implement them in a usable way as stack objects simply 
> don't work that way.
>
>> So whether it be this, or just an improvement on the Interfaces we 
>> already have, I'd definitely personally be in favor of something that 
>> "works like Interfaces except minus the negative performance 
>> implications."
>
> There are no significant performance implications of interfaces. 
> They're essentially a virtual method call, something that one is doing 
> all day long with Object Pascal classes.
>
> Regards,
> Sven
> _______________________________________________
> fpc-pascal maillist  - fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


More information about the fpc-pascal mailing list