Live data from Hacker News

When static makes your C code 10 times faster

mazzo.li

21–30 of 113 posts

Re: When static makes your C code 10 times faster

#21

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

That's bitwise AND.

Re: When static makes your C code 10 times faster

#23
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

Const is to state that this module is not allowed to modify. Think on what'const volatile int foo' means.

Mostly seen in embedded space.

Re: When static makes your C code 10 times faster

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

The only real case I use switch fallthrough for is when you have two cases with the same code, e.g.:

  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

#26
Back in 2012 I observed exactly the same thing. I actually managed to get a warning for this added to Clang, called -Wmissing-variable-declarations. When set, warnings are generated in case a non-static global variable is defined without an external declaration that precedes it.

I 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

#27
post #14

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

clang and gcc have a -Wimplicit-fallthrough warning flag that will warn about switch cases that fall through without either C++17's [[fallthrough]] attribute or a /* fallthrough */ comment.

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

#28
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…

Author here -- I agree that const is better. The perf difference I encountered was due to the static keyword though, which is why the blog post talks about that specific issue.

Re: When static makes your C code 10 times faster

#29
Great write up: a precise problem that digs into the internals pointedly to teach a simple concept. This is exactly how I tell the junior devs where I work to do lunch talks that give people some concrete, memorable piece of learning that makes them better in their practical work.
Post reply on HN