Live data from Hacker News

When static makes your C code 10 times faster

mazzo.li

41–50 of 113 posts

Re: When static makes your C code 10 times faster

#41
post #39

Without `static`, compiler exports a symbol. $ cat value.c int value = 42; int get_value() { return value; } $ make value.o gcc -c -o value.o value.c $ nm value.o 0000000000000000 T get_value U _GLOBAL_OFFSET_TABLE_ 0000000000000000 D value This symbol, not being `const` can be modified by any other compilation unit. $ cat main.c #include int value; int get_value(); int main() { value = 123456789; printf("%d\n", get_…

I quite like C syntax, and I sometimes long for a language that would change a few elements (default to const and static, different/safer standard api for strings). Some might call it Go but a simple slightly updated C would be nice.

That is pretty much D (assuming you only want to use the C-like bits) - it's not const by default but const is much easier to use and much more violent

And arrays are bounds checked by default, no more issues with them (and strings are arrays)

Re: When static makes your C code 10 times faster

#44
post #39

Without `static`, compiler exports a symbol. $ cat value.c int value = 42; int get_value() { return value; } $ make value.o gcc -c -o value.o value.c $ nm value.o 0000000000000000 T get_value U _GLOBAL_OFFSET_TABLE_ 0000000000000000 D value This symbol, not being `const` can be modified by any other compilation unit. $ cat main.c #include int value; int get_value(); int main() { value = 123456789; printf("%d\n", get_…

I quite like C syntax, and I sometimes long for a language that would change a few elements (default to const and static, different/safer standard api for strings). Some might call it Go but a simple slightly updated C would be nice.

Have you seen zig? It's a modern language that tries to keep the simplicity and explicitness of C (it does change the syntax a bit though).

Re: When static makes your C code 10 times faster

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

Yes, but you would need way fewer of such phrases than you need now. Making the exceptional case take more code is the way to go, IMO.

Many languages also support something like

  case 1,2,4:
or even

  case 1-10, 20-22, 34:
That further decreases the need for falling through.

Re: When static makes your C code 10 times faster

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

The perf difference was due to the compiler being able to infer 'const' by way of 'static'.

Advocating 'static' when your actual intent is 'const' does less experienced readers a disservice; they will assume that 'static' is meant to make things faster, and be disappointed when it doesn't work for non-constant values.

Re: When static makes your C code 10 times faster

#47

Earlier quoted context omitted.

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.

The perf difference was due to the compiler being able to infer 'const' by way of 'static'. Advocating 'static' when your actual intent is 'const' does less experienced readers a disservice; they will assume that 'static' is meant to make things faster, and be disappointed when it doesn't work for non-constant values.

I am not advocating static. I'm advocating for looking at what the compiler outputs when surprising behavior is encountered. The example is extracted from a larger piece of code, and I reported the minimal case as-is.

If anything, the title is meant to be read as "isn't it amusing that something apparently unrelated such as `static` causes a performance improvement".

That said, I have added a note clarifying this at top of the post now.

Re: When static makes your C code 10 times faster

#48
Const / static allows for "immediate" ASM instruction generation: which means the value is known at compile time so it can compare it directly inline as opposed to the overhead of comparing it to a labeled memory address. It's generally good practice whenever possible.

Re: When static makes your C code 10 times faster

#50

Const / static allows for "immediate" ASM instruction generation: which means the value is known at compile time so it can compare it directly inline as opposed to the overhead of comparing it to a labeled memory address. It's generally good practice whenever possible.

Isn't that constexpr?
Post reply on HN