[fpc-pascal] Are there any drawbacks to "reference to"?

Hairy Pixels genericptr at gmail.com
Mon Jun 20 03:37:32 CEST 2022

> On Jun 19, 2022, at 10:04 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> As you can see the allocation only happens once and not all the time. 
> What might be worse however is the optimization behavior as in this example the compiler wouldn't optimize the counter variable into a regvar with enabled optimizations (however in case of a nested function the compiler wouldn't do this either if the function isn't inlined). 

I’m saying if the OUTER method is in a tight loop (Foo in this example) you’re in trouble because it now contains an allocation which is unexpected. In fact if this happened within a class wouldn’t it capture and initialize all the classes fields also or does it know to only use the fields which are encountered in the function body?

Either way the reference never leaves the calling scope and thus is wasted and should be optimized away to something which exists on the stack, like “is nested”.

	Ryan Joseph

More information about the fpc-pascal mailing list