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).
When static makes your C code 10 times faster
21–30 of 113 posts
Re: When static makes your C code 10 times faster
#22Multiplication is also very fast, usually one or two cycles on larger chips.
Re: When static makes your C code 10 times faster
#23Earlier 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
Mostly seen in embedded space.
Re: When static makes your C code 10 times faster
#24Re: When static makes your C code 10 times faster
#25If 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.
switch (foo) {
case 1:
case 2:
/* common body */
break;
case 3:
/* body 3 */
break;
}
It's not cognitively hard to make an empty case body fallthrough to the next run, but include an implicit break at the end of every nontrivial body.Supporting Duff's Device is not a compelling feature to support--irreducible loops are basically going to destroy any hope of optimization you might accrue.
Re: When static makes your C code 10 times faster
#26I worked on this as part of FreeBSD, which is why their base system is nowadays built with that flag enabled.
Re: When static makes your C code 10 times faster
#27Earlier quoted context omitted.
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.
I made Firefox's code base able to compile with -Wimplicit-fallthrough. About one hundred fall through cases needed to be annotated and about 2-3 were actual bugs (though minor).
Re: When static makes your C code 10 times faster
#28This 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…