joe hinkle

Mark Butcher - Your Opinion on Variable Usage

Discussion created by joe hinkle on Oct 30, 2016
Latest reply on Nov 2, 2016 by joe hinkle

I'm looking at ways to minimize my ram usage and increase performance.


Based on your past experience - please comment on an opinion I have.  I'm looking for confirmation or - "Your Head is up your Ass on that one" reply.


I'm using an RTOS, so each task associated with the RTOS has a while loop - so you never exit from the function (standard RTOS implementation).  Local stack variables are always consumed and never reused.


I'm using Kinetis ARM micros - so they are all 32 bit cpus.


Local variables used within the RTOS task function always utilize the space of a 32 bit word (I'll call it a DWORD), whether ii's a BYTE (8 bits), or a WORD (16 bits).  


So a local BYTE consumes 4 bytes from the stack.  The compiler also has to add additional code to make sure the outcome of operations stay within the BYTE range.


SO .. local BYTEs and WORDs - consume more space than required and adds a small performance hit due to the additional processing required to make sure the operations are within range.


My opinion -- NOW ....


To save memory (actually stack space which could then be reduced) - move local BYTE and WORD variable out into global space.  


When a BYTE is declare global -- you only consume 1 byte of memory instead of 4 on the stack and the additional range operations are not required.


I looked at a task where I had 6 byte variables defined - that's 24 bytes of stack consumed.  By making them global -- I only consume 6.


Your thoughts based on your experience.