Live data from Hacker News

When static makes your C code 10 times faster

mazzo.li

11–20 of 113 posts

Re: When static makes your C code 10 times faster

#12
If I could travel back in time I'd tell Dennis to make "static" the implicit default, and have a special keyword like "public" or "export" for items that are meant to be accessible from outside the compilation unit.

I'd also ask him to make "switch" break by default.

Then I'd go kill Hitler or something.

Re: When static makes your C code 10 times faster

#13

I think every programmer should know, that logical AND is many times faster than Modulus (which is at least as hard as a division), and use & instead of % right in his code for powers of two (and not expect it to be done by a compiler).

This is like the easiest thing for a compiler to detect and optimize.

Re: When static makes your C code 10 times faster

#14
post #12

If I could travel back in time I'd tell Dennis to make "static" the implicit default, and have a special keyword like "public" or "export" for items that are meant to be accessible from outside the compilation unit. I'd also ask him to make "switch" break by default. Then I'd go kill Hitler or something.

But then you would need 'unbreak' or just 'goto' to the next case. No duffs device no cigar.

Probably a module/namespace system would be the biggest improvement.

Re: When static makes your C code 10 times faster

#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 static are orthogonal concepts. I'm not sure to what extent the article author is aware of this.

So the lesson should be: use const (or #define) if you mean to have a constant. It's still a good idea to also make things static, but the real reason for that is to avoid name collisions with variables in other C files.

Re: When static makes your C code 10 times faster

#16
post #14
post #12

If I could travel back in time I'd tell Dennis to make "static" the implicit default, and have a special keyword like "public" or "export" for items that are meant to be accessible from outside the compilation unit. I'd also ask him to make "switch" break by default. Then I'd go kill Hitler or something.

But then you would need 'unbreak' or just 'goto' to the next case. No duffs device no cigar. Probably a module/namespace system would be the biggest improvement.

FWIW: Go's switch statements are break by default, with an explicit 'fallthrough' keyword if one wishes to override that behaviour.

Re: When static makes your C code 10 times faster

#17
post #8

Earlier quoted context omitted.

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.

Are you sure? Isn't it UB to modify a const object? It will probably end up in a non-writable memory page. EDIT: gcc seems to agree with me: you can see the optimized version here[1] and the unoptimzed version if you remove "const". [1] https://godbolt.org/z/KWrW45rK8

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

Re: When static makes your C code 10 times faster

#18
post #13

I think every programmer should know, that logical AND is many times faster than Modulus (which is at least as hard as a division), and use & instead of % right in his code for powers of two (and not expect it to be done by a compiler).

This is like the easiest thing for a compiler to detect and optimize.

True, but something to be aware of: If the compiler uses bitwise operations to implement %, it emits special handling of negative values (it's really more of a remainder operator than a modulus). You can avoid that either by using unsigned numbers, or & as suggested:

https://godbolt.org/z/vdz59q994

Re: When static makes your C code 10 times faster

#19
post #17

Earlier quoted context omitted.

Are you sure? Isn't it UB to modify a const object? It will probably end up in a non-writable memory page. EDIT: gcc seems to agree with me: you can see the optimized version here[1] and the unoptimzed version if you remove "const". [1] https://godbolt.org/z/KWrW45rK8

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?

Re: When static makes your C code 10 times faster

#20
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.

[deleted]
Post reply on HN