Live data from Hacker News

When static makes your C code 10 times faster

mazzo.li

61–70 of 113 posts

Re: When static makes your C code 10 times faster

#61

As an embedded programmer working on small micros I make every single function static (including third party code, which I modify and bring into the source tree and curate myself), gives you global/link time optimizations and dead code elimination for free, leads to better code even at -O0, -Og and -O1, static const configuration structs/values gets optimized away, and so on. Really wish it was the default.

[deleted]

Re: When static makes your C code 10 times faster

#62
I think this is beyond simply making the variable static/constant. It is the specific value of the constant that is allowing the division to be substituted with bitwise AND, which then makes it so much faster. I wonder how much the speedup would be if some other near-random value is there for the constant (which is likely beyond the purpose at hand).

Re: When static makes your C code 10 times faster

#63
post #4

This 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…

I started to use const whenever possible after being familiar with some compiler optimizations and the Haskell pl. The point is knowing how to give the compiler an easier job.

Re: When static makes your C code 10 times faster

#64
post #8
post #4

This 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.

i think this is most compilers.

Re: When static makes your C code 10 times faster

#67

As an embedded programmer working on small micros I make every single function static (including third party code, which I modify and bring into the source tree and curate myself), gives you global/link time optimizations and dead code elimination for free, leads to better code even at -O0, -Og and -O1, static const configuration structs/values gets optimized away, and so on. Really wish it was the default.

It also gives you a nice private namespace for the translation unit so you can confidently modify any of the static objects without concern for impacting external code.

I once had a gray beard chew me out over changing a function signature when revising things. I had to point out politely that it was static and that anyone who managed to link to the function had to be breaking a lot of rules to do so.

Re: When static makes your C code 10 times faster

#68
post #4

This 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…

> It's really easy to forget to const something, which forces the compiler to do global reasoning or to generate worse code.

Global mutable state is pure evil. Don’t write globals.

It shouldn’t be easy to forget const on a global because a mutable global should produce immediate revulsion and nausea.

(I don’t really consider a const global to be “a global”. So ordinarily I’d just say globals are evil don’t write globals. But I’m trying to be explicit here.)

Re: When static makes your C code 10 times faster

#69
post #19
post #17

Earlier quoted context omitted.

This is why language standards specify what the compiler can assume and call out some behavior as undefined, exactly so compilers don't have to be paranoid and produce code that sucks. If an underlying object is const, the compiler is allowed to assume that it does not change (it is valid to cast away const on a pointer or reference, but not if the object itself was declared const).

> (it is valid to cast away const on a pointer or reference, but not if the object itself was declared const). Isn't it valid to cast to non-const for a const, but only invalid to modify the const through the casted pointer?

[deleted]

Re: When static makes your C code 10 times faster

#70
post #56
post #15

I agree with many of the sibling comments, static vs. non-static is almost a complete red-herring. Static only means that the variable is local to the translation unit (the C file). The relevant difference in the example is actually the const-ness of the variable, which you may put explicitly, but which a powerful compiler can also infer here in the static case. Other than this optimization possibility, const and sta…

this is totally wrong in my experience, although some of the understanding is right. by using static to limit to the compilation unit the compiler can workout the value is constant. const doesn't do this thanks to const_cast etc. maybe things have changed, but const on a file level variable doesn't do this reliably, or at least hasn't for considerable lengths of time. of course 'static const' is the better answer. :)

I hope your comment wasn't intended to be as hostile as it reads to me, but I do wonder what your experience was exactly, because there might be a misinterpretation.

From a technical standpoint, using const_cast to modify a constant is Undefined Behavior. This was explicitly specified that way to make constants inlineable by the compiler. Every compiler that I've ever used does this, it's a trivial but very effective optimization.

Proof by Godbolt: https://godbolt.org/z/djEdvee4s

Observe how GCC completely eliminates the contents of undefined_mutate_modulus (which it's allowed to -- UB means it can do anything with that function, and it chooses the simplest possible thing) rather than de-optimizing mod4_const like you suggested. Compilers are smart.

Post reply on HN