[fpc-pascal] How to inline CompareFunc to Sort method in generic abstract class
Flávio Etrusco
flavio.etrusco at gmail.com
Tue Nov 22 02:07:37 CET 2022
Hi,
Thanks for the clarifications (although in some aspects I'm more confused
now LOL).
If you really want the performance gains (and if you're convinced of these
gains, of course) maybe you could implement inlined functions for each key
native datatype taking an offset for the field.
And if you have computed or complex keys, creating an indirection list
would probably have speed gains.
--
Best regards,
Flavio
Em sáb., 19 de nov. de 2022 00:26, Vojtěch Čihák via fpc-pascal <
fpc-pascal at lists.freepascal.org> escreveu:
> Hi,
>
>
>
> I specialized my generic abstract class in a different unit with this type:
>
>
>
> TRec = record
>
> A: Integer;
>
> B: Integer;
>
> end;
>
>
>
> (A is for sorting, B is for testing of stability, i.e. for MergeSort,
> TimSort)
>
>
>
> and compare function, declared with inline; directive:
>
>
>
> function MyComptFunc(const ARec, BRec: TRec): Integer;
>
> var i, aCnt: Integer;
>
> begin
>
> aCnt:=CFComplex;
>
> for i:=0 to aCnt do
>
> Result:=(BRec.A-ARec.A);
>
> inc(CFCnt);
>
> //InterlockedIncrement(CFCnt);
>
> end;
>
>
>
> The reason for this complex compare function is that it also measure
> number of comparisons and it can simulate more expensive comparisons (like
> sorting strings).
>
>
>
> For a while, I added this unit to the second "uses" section
> (implementation) of the other unit, which is impractical, but I had
>
> temporary access to the compare function. I tested again with ShellSort,
> sorting 2'000'000 values takes:
>
> 1380ms inlined
>
> 1515ms not inlined, ~9% slower
>
> 1430ms when compare func. is a parameter ~4% slower
>
>
>
> I pass variables by value. But you are right, when I shave the function
> like this:
>
>
>
> function MyComptFunc(const ARec, BRec: TRec): Integer;
>
> begin
>
> Result:=(BRec.A-ARec.A);
>
> end;
>
>
>
> the results are:
>
> 750ms inlined
>
> 950ms not inlined, ~21% slower
>
> 835ms when compare func. is a parameter ~10% slower
>
>
>
> so the gain of inlining is higher for sorting primitive types.
> V.
>
> ______________________________________________________________
> > Od: "Flávio Etrusco via fpc-pascal" <fpc-pascal at lists.freepascal.org>
> > Komu: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
> > Datum: 18.11.2022 20:45
> > Předmět: Re: [fpc-pascal] How to inline CompareFunc to Sort method in
> generic abstract class
> >
>
>
> Em seg., 14 de nov. de 2022 15:26, Vojtěch Čihák via fpc-pascal <
> fpc-pascal at lists.freepascal.org> escreveu:,
> 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,
> Flávio
>
>
> ----------
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20221121/04070e3d/attachment.htm>
More information about the fpc-pascal
mailing list