[fpc-pascal] Traits Proposal

Ryan Joseph genericptr at gmail.com
Tue Feb 16 19:21:05 CET 2021



> On Feb 16, 2021, at 6:51 AM, Benito van der Zander via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> 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)

It's a real concern indeed. What we're proposing with a default property doesn't need to have these impacts but the class instantiation is going to be affected I think. 

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

How bad is this? We need to make a test case we can profile. For example if you have a game that creates 1000 classes 60 frames per second. If adding interfaces to the classes hurts this process we may have a deal breaker.

> 
> * slower method calling because of the wrapper function

You mean for fields? We can inline those I believe but it's not fun boiler plate for sure.

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

We can avoid casting using the default property but it will still be possible to cast the class if you want to. 


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

It's a whole other topic but FPC needs opt-in ARC at the language level for classes. Interfaces are not only slow but then you need to pass around a reference to the interface instead of the class you actually care about. It makes no sense to me whatsoever.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list