Referencing Globals - rosco-pc/propeller-wiki GitHub Wiki

Some calculations of the spin code SIZE to reference 'global' variables in various ways. Speed was not tested.

For reference, when variables are the same object. Referencing a stack variable and object VAR 1 byte. Referencing a DAT variable 2 bytes. Referencing a memory location from a Constant pointer was 4 bytes.

For variables shared from another object there is normally a 2 step process, retrieve the pointer and then retrieve the variable using the pointer. Because of the large difference in how a variable is accessed seen above, the storage of the pointer becomes key. If used in only one location, the best method is to retrieve the address on initialisation, and store with either in a object word VAR. If used more than once in the same method, store the pointer in a stack variable. These 2 techniques use 3 and 2 bytes to dereference respectively, and the code setting up the pointer is similarly sized. Once the extra 2 byte to store a stack pointer (long) vs a VAR pointer (word) is taken into account they are the same size. However the local var method is prefered as is expected to be faster as less code is loaded into the cog in the loop.

If you are using a pointer to iterate an array, there is significant difference depending how the array offsets are handled. The most effective is to use the offset array syntax, as the increment code is far smaller, except for BYTE arrays. So use blah := WORD/LONG/BYTE[stackPtr][stackOffset++], where both the ptr and offset are stored in stack variables.

⚠️ **GitHub.com Fallback** ⚠️