[fpc-pascal] Are there any drawbacks to "reference to"?
Hairy Pixels
genericptr at gmail.com
Sun Jun 19 15:44:04 CEST 2022
> On Jun 19, 2022, at 7:01 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>
> As I can see neither Anthony's nor Ryan's mail, but I can see them in the archive, I'll use Jonas mail for some general replies (please CC me when replying):
Firstly unrelated, I posted a question about generics which you probably have the answer to and may have missed (https://www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg55415.html). I just joined the Lazarus forum also and mentioned this in a related topic where another user seems to have stumbled on something similar.
> Also keep in mind that the expensive creation only happens once. Afterwards it's "only" the price of an indirect call.
>
The performance problem will surfice if you use a reference inside a function which is called often. Lets say you decided to use a reference as a general callback type because it’s convenient not to consider “is nested” or “of object” and then call this from within a function which is called in a tight loop.
Depending on what you’re doing this extra allocation of the interface could totally blow your program out of the water but this isn’t apparent to the programer who merely sees what they’re doing a simple callback. For this reason you can’t rely on references as an all purpose callback type and if performance is important you need to make duplicate functions which take different types of functions pointers, hence our desire for a generalized function pointer that that doesn’t do the interface allocation if there’s no passing of the reference outside the calling scope.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list