When static makes your C code 10 times faster
1–10 of 113 posts
Re: When static makes your C code 10 times faster
#2mod’ing by a power of two -- is equal to bitwise and of that number minus one!
All you need to do is keep the bits lower than that power of two, which is what the and will do."
Re: When static makes your C code 10 times faster
#3Re: When static makes your C code 10 times faster
#4This is a common issue with C code (including lots of code I've written). It's really easy to forget to const something, which forces the compiler to do global reasoning or to generate worse code. I've gotten into the habit of making things const unless I know I plan on mutating them, but I wish there was tooling that encouraged it. (BTW, this is something Rust does well by making things constant by default and requiring "mut" if it's mutable.)
Re: When static makes your C code 10 times faster
#5I think a simple “const” would have also done the trick. Sometimes -O3 is clever enough to figure out that the value is never written, so it makes it an asm constant.
Having it local or const makes the compiler able to inline it and do a simple bitwise and with a constant.
So yes, make your variables static const by default (if you really need global).
Re: When static makes your C code 10 times faster
#6Honestly if I as the programmer knew that I was really trying to select bits from a number I’d just use a binary and directly. In that specific situation I think the intent is more clear that way. Like:
//select the bottom 8 bits
unsigned bottom8 = val & 0xff;
Re: When static makes your C code 10 times faster
#7Re: When static makes your C code 10 times faster
#8This can be accomplished more simply and reliably by marking modulus as const. The compiler currently has to reason about the whole compilation unit to determine that modulus is not modified, which works. However, if future code modifies modulus (either on purpose or accidentally) or something changes that prevents the compiler from performing global reasoning, the optimization will be lost. By marking the actual int…
Re: When static makes your C code 10 times faster
#9This can be accomplished more simply and reliably by marking modulus as const. The compiler currently has to reason about the whole compilation unit to determine that modulus is not modified, which works. However, if future code modifies modulus (either on purpose or accidentally) or something changes that prevents the compiler from performing global reasoning, the optimization will be lost. By marking the actual int…
The core optimization is modulus % constant (and a power-of-2 as well). The static just enabled the optimizer to do better heavy lifting to get there. A const would've made the intent clear to the human reader and the compiler.
static const would've been best.
Re: When static makes your C code 10 times faster
#10This can be accomplished more simply and reliably by marking modulus as const. The compiler currently has to reason about the whole compilation unit to determine that modulus is not modified, which works. However, if future code modifies modulus (either on purpose or accidentally) or something changes that prevents the compiler from performing global reasoning, the optimization will be lost. By marking the actual int…
If there were any expressions that took the address of the variable, then even both `static const` qualifiers wouldn't work for a sufficiently paranoid compiler.
EDIT: gcc seems to agree with me: you can see the optimized version here[1] and the unoptimzed version if you remove "const".