On Thu Aug 17 15:54:00 2023 +0000, Gabriel Ivăncescu wrote:
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.
All good points, thanks for the explanation. I've pushed a new patch that uses strlen. The function call is optimized out on GCC and Clang, so the final machine code is identical. Still waiting to hear Jacek's opinion.