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

Flávio Etrusco flavio.etrusco at gmail.com
Fri Nov 18 20:44:26 CET 2022

Em seg., 14 de nov. de 2022 15:26, Vojtěch Čihák via fpc-pascal <
fpc-pascal at lists.freepascal.org> escreveu:

> 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%.


What do you mean by "be inlined"? The 'inline' directive instructs the
compiler (or is it the linker? 😬) to try copying the whole function
inline, also avoiding the call (stack) setup; you can't do this for this
for a general purpose method.
I'm curious how you got that 6% figure, it seems rather large. Is this
comparing the parameter vs virtual method approach or comparing a fully
inline (no indirect call of any kind) sort function to some other option?
Are you passing the variables by reference?

Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20221118/c24a9183/attachment.htm>

More information about the fpc-pascal mailing list