Local variables in Audio Callback

Local variables are stored on the stack right? So, should I avoid locals in the audio callback and go for globals instead?

I’ve read that the compiler optimizes locals into registers, but I guess there is some limit on the number of registers in the processor.

You could use godbolt or objdump and compare the assembler generated.

My guess is that they both will look the same if you use the globale like a local.

I spend way to much time getting rid of globals (I am all about unit testable code). It would be „annoying“ if the global version is faster. :sweat_smile:

1 Like

When using the stack, the only thing you should worry is the overflow, if you’re storing huge objects as local variables. Performance is not an issue here. The stack is a safe place and using global vars instead of local vars hurts code maintainability.


Thank you @Konrad and @Firesledge for your input!

No worries then! :slight_smile: