[fpc-pascal] Traits Proposal

Sven Barth pascaldragon at googlemail.com
Sun Feb 14 17:55:29 CET 2021


Am 14.02.2021 um 13:30 schrieb Michael Van Canneyt via fpc-pascal:
>
>
> On Sun, 14 Feb 2021, Sven Barth via fpc-pascal wrote:
>
>>> 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.
>
> I think the "performance implications" is more referring to memory 
> management.
>
> Interfaces means an extra object per interface, so more objects on the 
> heap with all the extra memory management that implies.

No, interfaces by *themselves* don't mean that. An interface instance is 
merely a shifted Self pointer that points to the static VMT of which the 
entries correct the Self pointer again upon the method call. This can be 
seen in GetInterfaceByEntry in rtl/inc/objpas.inc (the core function 
dealing with the casting of class to interface).

> That is why I think that a 'trait' using an object/advanced record has 
> it's advantage: There is only 1 continuous memory block for a class 
> implementing
> traits.

For delegation it is true that memory allocation is involved, but let's 
be fair here: this instance is normally allocated together with its 
parent instance and thus the performance impact only adds to the already 
costly instantiation of the parent instance. And if you really need to 
worry about heap fragmentation then you have other problems in my opinion...

That said, as I wrote, one could in principle allow the use of records 
and objects for the delegation as well, after all delegation classes 
don't need to implement the interface either and the compiler needs to 
create appropriate thunks anyway.

Regards,
Sven


More information about the fpc-pascal mailing list