[fpc-pascal] Where is the best place to declare an array?

Werner Van Belle werner at yellowcouch.org
Thu May 6 23:28:04 CEST 2010


Jonas Maebe wrote:
> I don't have the faintest idea why it would be true. 
I'll explain below
> Local variables (including arrays) are allocated on the stack and are accessed without any kind of indirection (except for dynamic arrays, but the same goes for globally defined dynamic arrays). 
This is not true. You must take your current stackframe into account,
which adds an indirection.

> I don't know how to explain this briefly. I suggest you to simply try it out by compiling with -al, then you can see what kind of code the compiler generates for the different kind of variables.
>   
Good I did so, thanks for the tip. Below you will see why I believe one
is faster than the other.

# Var local located at ebp-20
# local[2]:=2;
        movw    $2,-18(%ebp)
# global[2]:=2;
        movw    $2,U_P$PROGRAM_GLOBAL+2

In this case the global accessors address U_P$PROGRAM_GLOBAL+2 (which is
a constant for the assembler), while the local variable must work in a
register indirect fashion (ebp-20). This is why I believe one would
indeed be faster than the other. In 'human' (haha) terms:

- global variable access: write data into 'ds+constant'
- local variables: write data into 'ss+bp+constant'.

Since the latter involves a term more I assume it is bound to take more
time. Sadly enough I could not verify this and finding clockccounts on
this type of instructions is far from simple.

Of course, this indirection matters little since the biggest bottleneck
has always been the memory access itself.

Wkr,

-- 
http://werner.yellowcouch.org/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20100506/31fe23ac/attachment.sig>


More information about the fpc-pascal mailing list