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.
I've not seen this as the basis for the default usage of const over var (or let) in javascript. The primary reason that I'm aware of is that if you use const by default, the compiler will tell you if you accidentally re-define the value of a variable. Possibly because people feel that immutability by default reduces error frequency, though I don't have any data to back up that assertion.
Why Const Doesn't Make C Code Faster
31–40 of 197 posts
Re: Why Const Doesn't Make C Code Faster
#32> So most of the time the compiler sees const, it has to assume that someone, somewhere could cast it away, which means the compiler can’t use it for optimisation. This is true in practice because enough real-world C code has “I know what I’m doing” casting away of const. Some years ago i'd agree, but experience has shown that C/C++ compiler writers would rather win benchmark games than keep working code working. So…
Re: Why Const Doesn't Make C Code Faster
#33Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…
The fact that std::launder ( https://en.cppreference.com/w/cpp/utility/launder ) exists blows my mind. Like, why is this a thing that the standard allows?
In short, placement new (or some scenarios involving unions) technically cause undefined behavior if you try to use the object after the call to placement new, since the pre-existing object there has had its lifetime expire. std::launder lets you use a pre-existing pointer to the memory at the same location to access the data there.
Re: Why Const Doesn't Make C Code Faster
#34I'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
const user = Object.freeze({id: 1, status: 'active'});
But const is immaterial to this topic, as it only prevents you from being able to reassign user to some other value, i.e., user = 7;
While... let user = Object.freeze({id: 1, status: 'active'});
user.status = 'disabled'; // throws an error.
Provides the behavior that you're seeking to guard against, regardless of let/var/const usage.Re: Why Const Doesn't Make C Code Faster
#35For even more aggressiveness, try __attribute__((const)) which tells it that the func accesses NOTHING but the params, not even global vars. Of course this is quite limiting. You can also use this as a trick when YOU know that the only accessed global state is itself constant but gcc does not know that. This can produce substantial savings in number of calls of funcs that, for example, do lookups in large constant data tables, like table-based versions of sin() and cos().
In the example the article gives, adding __attribute__((pure)) to constFunc() does produce the behaviour author wanted and then some
LWN has more: https://lwn.net/Articles/285332/
Re: Why Const Doesn't Make C Code Faster
#36Earlier quoted context omitted.
const doesn't actually stop data from changing, just reassignment. const user = {id: 1, status: 'active'}; user.status = 'disabled'; is valid javascript
You're talking about two different things. In this case, you'd use... const user = Object.freeze({id: 1, status: 'active'}); But const is immaterial to this topic, as it only prevents you from being able to reassign user to some other value, i.e., user = 7; While... let user = Object.freeze({id: 1, status: 'active'}); user.status = 'disabled'; // throws an error. Provides the behavior that you're seeking to guard aga…
No, I'm talking about the behavior of const.
Re: Why Const Doesn't Make C Code Faster
#37It seems that interprocedure optimisations in modern compilers make a lot of const-by-reference optimisations apply even when the code is mutable-by-reference in the parameter list but the function body doesn't modify it in practice. This would only work if it could deterministically work out which function is called.
Local constant stack values surely can be completely deterministically verified as such by the optimiser even without the `const` modifier. They could be overwritten without a C assignment to it, via the stack from a buffer overflow of an array next to it, but that's undefined behaviour so a compiler is presumably free to assume it is not modified, eliminate unnecessary register/memory loading code, and let the developers deal with the consequences.
As trailing `const` on member functions outlaws modifications via `this`, it would follow that the same optimisation-even-without-modifier process would apply as to `const` local stack values.
As constancy is a constraint that aids human comprehension, there's a good reason for choosing a keyword just as short as the mutable equivalent, such as Swift's `let` vs `var`; if the more constrained equivalent is equally or more convenient, more constrained and thus easy-to-reason-about code becomes more common.
Re: Why Const Doesn't Make C Code Faster
#38The real reason to use "const" is to show intent.
See "Const and Rigid Parameters" in this wonderful article about the Doom 3 source code: https://kotaku.com/the-exceptional-beauty-of-doom-3s-source-...
Re: Why Const Doesn't Make C Code Faster
#39gcc has some attributes to help with that, for example try __attribute__((pure)) on funcs that will always return the same thing for the same inputs (given the same global state) and do not modify said input or state (and thus can be called more or fewer times than programmer wrote). Usually gcc will be quite aggressive with optimizations if you use that. For even more aggressiveness, try __attribute__((const)) which…
Re: Why Const Doesn't Make C Code Faster
#40Consider that `x` may point to a global variable which `something` may modify. That is why the reload is necessary.
edit: I just realized that the reload would be necessary even without an intervening function call. https://godbolt.org/z/QhVzlV
Consider that `x` may point to errno; then printf would modify it!