Question

void Animation::playAnimation() const
{
    static const int index = 0;
    const std::string& animationFileName = 
    m_animationContainer.getAnimationName(index);
    static const int zOrder = -1;
    static bool isLooping = false;

    AnimationBank::play(animationFileName,
                        zOrder,
                        isLooping);
}

Is there any benefit to to define constant local variables as static? Or it is unnecessary and even bad practice.

Was it helpful?

Solution

Beyond @Christophe's very good answer, the code generated for the static is most likely worse than the one for the local variable, so if you're interested the under-the-hood benefit, statics are worse on modern processors.

The reason is that the statics must be located somewhere in memory that can be found by all the other threads and by all the other invocations. This basically means putting them in global memory.

Over the years, processors & compilers together have significantly optimized access to local variables due to the popularity of their usage, as compared with other variables, such as globals, statics, and fields. The compiler may choose to store a local variable in a CPU register, and even if it doesn't (so it uses the invocation stack instead) all of the stack is almost certainly in the cache. Accessing the stack is usually a short displacement addressing mode (off the stack pointer register). However, accessing globals or statics usually requires and extended offset or absolute address, so the resulting instructions doing so are longer than their equivalent for stack memory access.

All that being said, due to the combination of static and const the compiler may detect that it can substitute the constant value at the point of usage, so perhaps use of const mitigates the above. Still, your snippet shows at least one non-const statics, so perhaps the discussion is topical.

OTHER TIPS

It is not a question of benefits, but a question of semantics:

  • A static variable in a function (even a member function), means that the variable is shared between all the calls of that function. So one call of that function has a side effect on the subsequent calls.

  • A non static variable is unique to each execution of the function.

So, unless it is required for some specific and well justified reason, keep local variables non static.

Additional remark: The static variable is also shared accross different threads. So calling this function from more than one thread at a time might result in race conditions and UB (even if called for different objects).

A local variable is initialized or constructed every time the function is called. Local variables are stored on the stack and therefore are generally thread safe.

A static local variable is initialized or constructed only once; the first time the function is called. Local static variables are not stored on the stack and therefore are generally not thread safe.

A const local variable is a variable that does not change and is initialized or constructed every time the function is called. Local const variables are stored on the stack and therefore are generally thread safe.

A static const local variable is a variable that does not change and is initialized or constructed only once; the first time the function is called. Local static const variables are not stored on the stack and therefore are generally not thread safe.

Compilers may be able to optimize const variables into a compile time constant.

Compilers may be able to optimize non-static variables by by keeping them in registers rather than on the stack.

Licensed under: CC-BY-SA with attribution
scroll top