[fpc-pascal] How to inline CompareFunc to Sort method in generic abstract class

Hairy Pixels genericptr at gmail.com
Tue Nov 15 01:33:53 CET 2022


There’s no way since FPC can’t inline function pointers (I asked already a while ago).

It depends on what’s in your list but you can often override comparison operators (like =, < and >) or restructure your classes so there is a base last and then 2+ subclasses that have the top level sorting methods.

Another idea is to use something like traits and pass in a sorter to the list class on construction which does the sorting and has inlined comparison functions.

> On Nov 15, 2022, at 1:26 AM, Vojtěch Čihák via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> Hi,
>  
> I wrote a generic abstract class - a list based on dynamic array (i.e. array of T;) and this class can be specialized elsewhere with any type (records or classes).
> Part of the class is sorting. There are more ways how to deliver *compare function* to sorting method. I can pass it as a parameter or I can define it as: function Compare(A, B: T): Integer; virtual; abstract;. But this way the function cannot be inlined.
>  
> Question: Is there a way how to *inline* compare function to sorting method in this general purpose generic abstract class?
>  
> Thanks.
>  
> PS: The gain is 6-7%.  
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Regards,
Ryan Joseph



More information about the fpc-pascal mailing list