On Thu Aug 17 15:51:16 2023 +0000, Alexandre Julliard wrote:
Static constants do have another advantage besides optimization: They
can be placed in read-only memory, so if someone editing this code did take a pointer to the constant and accidentally modify it, it would be obvious because the code would segfault. In my opinion that is a "nice to have" even though, like the optimization benefit, it doesn't matter very much in this particular case. Please let's avoid that kind of "optimization", it doesn't make any sense. I don't think there's anything to change it, but if you absolutely want to get rid of the magic number, just replace it by strlen().
In terms of optimization, as long as the address isn't taken, a const local variable will literally get replaced by the direct constant, as if using a #define macro or an enum, how is static better?
The compiler actually has to do *more* work to optimize out a static constant like this, since it has to actually remove the variable (because it's not addressed from anything via its address and not visible outside the compilation unit, so it can be removed, but still that's an extra effort it has to do).
const local variables aren't even allocated as variables when optimizing. Even if they did, it's because they're caching some expensive calculation, which is actually good.
static const makes sense when you want to have one pre-allocated instance of the variable, when you need to actually read it via its address. This is not the case here.