Live data from Hacker News

Why Const Doesn't Make C Code Faster

theartofmachinery.com

131–140 of 197 posts

Re: Why Const Doesn't Make C Code Faster

#131
Overly broad statement. const for global or static variables do make code faster, as they are placed in the .rodata segment, which allows swapping them out for free. This can be a huge win.

He only talks about const args, where the compiler does not try yet to check for casts which violate the constness guarantees. (DFA, Escape Analysis). And thus misses all important optimizations. You cannot rely on that never being implemented in the future. With LTO they already do I think.

Re: Why Const Doesn't Make C Code Faster

#132

The most succinct way I've heard this explained is: "Const means ' I won't mutate this'. It doesn't mean ' nobody will mutate this'." To do optimizations, compilers really need to know that nobody will mutate a value; just knowing that a particular function won't mutate a value isn't that helpful.

True to form, there's multiple forms of const in C++. const on a methond says "I won't mutate the state of this class (except for members explicitly marked as mutable) A const reference says I won't mutate this (unless I const_cast away the constness first) But a const variable actually is a promise to C++, saying "This object will never mutate after initialization ever forever I promise for real this time " ( except…

You are allowed to cast away const-ness of an actual const object, just as long as you don't modify it.

Re: Why Const Doesn't Make C Code Faster

#133
I just see too many programmers that right from the start try to micro-optimize their code with things like this. You'll usually see very well written code but with all sorts of hacks, taken from blogs and StackOverflow answers and whatnot all across the web, but in the end they fail to optimize their actual algorithm and end up with some ultra-high memory footprints or O(n^3) stuff instead of some nice O(log(n)) for example. And they'll argue that they already "optimized the sh*t out of it", because they saved a couple of hundred assembly calls in a binary file around the size of a few megabytes ...

Re: Why Const Doesn't Make C Code Faster

#135
post #11
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

const doesn't actually stop data from changing, just reassignment. const user = {id: 1, status: 'active'}; user.status = 'disabled'; is valid javascript

That is how object oriented languages work. It is a constant reference to a mutable object.

  final int[] arr = {1, 2, 3};
  arr[1] = 10;
is valid Java.

Re: Why Const Doesn't Make C Code Faster

#136
post #115

Earlier quoted context omitted.

>The author didn't ask me about it, he just assumed the compiler was buggy and gave us a bad review. Sigh. Did you write a letter complaining? Any response? Do you have a link to the review or copy of it? (Sorry, I just find this history fascinating).

I did complain to the author, but the damage was done. Can't undo a magazine article. I didn't save a copy of the magazine I was so annoyed. BTW, it goes on. A couple years ago a kind soul gently suggested I implement DFA since clang had invented DFA. (No, I didn't invent DFA either, but I do believe that Datalight Optimum C was the first DFA compiler for DOS.) http://www.program-transformation.org/Transform/CCompile…

(Bottom link:) does that mean the dmd backend code dates back to the mid 80s?

Re: Why Const Doesn't Make C Code Faster

#137

Earlier quoted context omitted.

Does it enforce these attributes or are they just a promise by the code author?

D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions pure, you know it's solid. One pain point with pure functions is you can't insert debug logging statements. D has a special case for that - purity for a statement isn't checked if it is prefixed with the `debug` keyword: pure int square(int x) { debug printf("c…

The same "trick" is especially handy in a pure-by-design language like Haskell. There you have the `trace` function which prints to stdout while pretending not to. It's similarly useful, even if it may get called zero or many times. Is the same true of D's debug?

Re: Why Const Doesn't Make C Code Faster

#138
post #96
post #90

Earlier quoted context omitted.

I'm not sure what you mean? Allocators are often pure in D, they probably shouldn't be but that would mean allocating outside the function which would be ugly and probably buggy too

Obviously a pure function can allocate memory on the stack. I would think it could also allocate memory on the heap, iff it was guaranteed that the memory was freed before the function exits. Was thinking that would be something the compiler would do. Forgive me I'm just a small brained firmware programmer.

So it seems that you are sort of talking about a function that is memoizable but not pure. Pure has no mutation of any global state. So hypothetically touching the heap means not pure - if somehow others can see it.

Re: Why Const Doesn't Make C Code Faster

#139

Earlier quoted context omitted.

Does it enforce these attributes or are they just a promise by the code author?

D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions pure, you know it's solid. One pain point with pure functions is you can't insert debug logging statements. D has a special case for that - purity for a statement isn't checked if it is prefixed with the `debug` keyword: pure int square(int x) { debug printf("c…

Isn't that erroneous, in the sense that printf() has side-effects, breaking the "pure" promise?

Re: Why Const Doesn't Make C Code Faster

#140

Earlier quoted context omitted.

Does it enforce these attributes or are they just a promise by the code author?

They are a promise and that is precisely what makes them powerful. It gives GCC info it might not be able to derive, but you know to be true. You can even promise a func is pure without even implementing it in the same translation unit! That produces lots of improvement. See these two godbolt links: https://godbolt.org/z/v2zqJI https://godbolt.org/z/kk3Q3T

> You can even promise a func is pure without even implementing it in the same translation unit!

Actually that's the main point, as if it's in the same TU, the compiler can probably figure out a function is pure.

Post reply on HN