Variable allocation question

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 22,614
edited 2006-03-29 - 01:21:57 in Propeller 1
Chip,

1. Could you explain what happens when a Spin subroutine is called?
2. Are the return variable, parameters, and local variables pushed onto the cog's stack?
3. Is there a relative performance hit for accessing local vs. global variables?

The reason I ask is that I'm beginning to wonder if it's better just to use global variables in a non-recursive environment, assuming that locals cause a stack- and/or performance-hit. This especially concerns me since all locals are longs.

Thanks,
Phil

Comments

  • cgraceycgracey Posts: 12,793
    edited 2006-03-29 - 01:03:13
    Phil,

    There are single-byte Spin codes for accessing the first 8 global longs and the first 8 locals. The first eight locals are, in sequence:

    0) The RESULT variable (or whatever else you've named it)

    1..N)·Any passed-parameter variables

    N+1..) Any local variables

    Usually, there are no more than eight of these local variables, so your code is optimally small. However, you may have many global variables, so it is worthwhile to declare your most popular first 8 global longs in the beginning of the first VAR block. This will insure that they can be read and written via single-byte Spin codes. Throughout your object.

    The 'stack' that is used for an interpreter's workspace·serves as a linear buffer that tracks·return addresses and pointers, holds passed parameters, holds local variables, and provides math workspace for RPN-style computations that must be resolved at run-time. It builds from the stack base upwards, and recedes as routines are returned from. A stack size of 7 longs·is about the bare minimum that you can get away with for very simple programs. If you're calling a few levels deep, passing some parameters, and performing some math operations, you might need 30+ longs.
    Phil Pilgrim (PhiPi) said...
    Chip,

    1. Could you explain what happens when a Spin subroutine is called?
    2. Are the return variable, parameters, and local variables pushed onto the cog's stack?
    YES
    3. Is there a relative performance hit for accessing local vs. global variables?
    NOT FOR THE FIRST 8 OF EACH.

    The reason I ask is that I'm beginning to wonder if it's better just to use global variables in a non-recursive environment, assuming that locals cause a stack- and/or performance-hit. This especially concerns me since all locals are longs.

    Thanks,
    Phil
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 22,614
    edited 2006-03-29 - 01:21:57
    Chip, that helps a lot. Thanks!

    -Phil
Sign In or Register to comment.