Live data from Hacker News

When static makes your C code 10 times faster

mazzo.li

31–40 of 113 posts

Re: When static makes your C code 10 times faster

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

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

> The only real case

which is probably the case in 90% of all my switch'es. Worst use of a time machine ever.

Re: When static makes your C code 10 times faster

#32
post #22

Division is slow, which is something most programmers don't know. If you can binary AND instead of MOD this can be a huge win. Multiplication is also very fast, usually one or two cycles on larger chips.

If your compiler doesn't do this for divisors known at compile time, get your money back.

Re: When static makes your C code 10 times faster

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

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

From Algol's linage point of view, a very good outcome.

Re: When static makes your C code 10 times faster

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

Because, admittedly, non-default switch breaks are on par with Hitler

Re: When static makes your C code 10 times faster

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

No, even if you could kill Hitler, then WW2 doesn't happen, computers aren't invented yet, so time travel isn't invented yet so you can't head back to kill him. Read your briefing notes.

Also https://tvtropes.org/pmwiki/pmwiki.php/Main/HitlersTimeTrave...

Re: When static makes your C code 10 times faster

#37
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_value());
    }

    $ make main.o
    gcc    -c -o main.o main.c

    $ cc value.o main.o

    $ ./a.out
    123456789
Compiler when generating an object file has to assume the value of exported non-const symbol can change. It's necessary to tell the compiler that the value cannot change, either by not exporting the symbol by using `static` or making the value of it `const`. In example provided in your article `static` makes sense (or even `static const`) as I don't think there is a reason to export this global.

Re: When static makes your C code 10 times faster

#38

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

Yes, see last few paragraphs of the post, in which I also speculate why GCC doesn't infer that there is only one compilation unit.

Re: When static makes your C code 10 times faster

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

Re: When static makes your C code 10 times faster

#40

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.

Thanks, that's good to know!
Post reply on HN