4 ms·
The point about const is not wrong. What you said about const is true if, and only if, the object was originally const - i.e. you can't do this: const int n
by int_19h 10y ago
The point about const is not wrong. What you said about const is true if, and only if, the object was originally const - i.e. you can't do this:
const int n = 1;
const_cast<int&>(n) = 2;
However, when the function receives a pointer or reference to const, it doesn't know whether the referenced object is originally const or not. It only knows that it cannot write through that pointer; but someone else still might be able to do so. Example:
int n = 1;
void foo() { n = 2; }
int bar(const int& x) { int y = x; foo(); return y + x; }
bar(n);
Even though x is reference to const here, the compiler has to do two reads from x here, because foo can - indeed, does - change the value referenced by x.
Worse yet, the function can often change it itself via aliasing:
int n = 1;
int bar(const int& x, int& y) { y = x; return x; }
bar(n, n);
Again, this is perfectly legal.
The problem is that both C and C++ lack a way to say "this is a pointer or reference to something that is immutable", as opposed to "... something that you can't mutate". The other problem is that C++ (but not C, thanks to "restrict") lacks a way to say "the object referenced by this pointer or reference is not aliased in any other respect that matters to this function".
Rust, OTOH, does let you say that something that's referenced is immutable. In fact, it is the case by default. So the compiler can aggressively optimize around the fact.
In C++, they can only optimize as well when the compiler can do a full program analysis, and determine that the referenced value is immutable in practice. Which can be hard to do for non-trivial programs, and outright impossible to do across dynamic library boundaries.
- gpderetta 10y agotemplate<class T> struct const_ { const T value; }; const_<T>& freeze(T&); T& thaw(const_<T>&); do_something(const_<T>& x) { /* the language guarantees that x can't be mutated from anywhere */ }
- pjmlp 10y agoYou can cast away const.
- gpderetta 10y ago'value' is a const qualified object, not a const qualified reference. If you cast away the const and write to the object you are into UB, ergo the compiler can assume that isn't done. Note that the trick still doesn't help much in practice: GCC conservatively still won't optimize based on const qualification of values.
- int_19h 10y agoNice. Of course, now you have to actually remember to write `const_<int> x = ...` instead of `const int x = ...` everywhere. And also get all the libraries to do the same...
- DannyBee 10y ago"The point about const is not wrong. What you said about const is true if, and only if, the object was originally const - i.e. you can't do this:" That's what i said, actually: " Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue both result in undefined behavior. So yeah." "he other problem is that C++ (but not C, thanks to "restrict") lacks a way to say "the object referenced by this pointer or reference is not aliased in any other respect that matters to this function"." "In C++, they can only optimize as well when the compiler can do a full program analysis" Which is why i said anyone who cares hands it the whole program anyway. Because even in rust, not having the whole program but claiming to pretend about performance is just silly. "I care tremendously about performance, but i refuse to give you enough of the program for you to do anything with it :P" That said, you can actually do it without fully program analysis. It suffices to have summaries. " and determine that the referenced value is immutable in practice. Which can be hard to do for non-trivial programs, " With no offense, this isn't the early 2000's anymore. Whole program LTO is not some esoteric feature, it's the default for anyone who actually cares about performance. Same with the type of mod/ref analysis you are talking about. It's done already. We whole program optimize binaries with hundreds of millions of lines of code in them. So do plenty of people. It also doesn't actually require full program,only the ability to see the allocation site and the call. It doesn't have to prove it's always immutable, if it can prove it at least once it can just specialize that function (and do so in a way that it would be reused with anywhere else that specializes). But yes, you could also try to prove it is always immutable (and gcc does that too). " and outright impossible to do across dynamic library boundaries." This is actually not right, though admittedly not common, fwiw. First, you can summarize it, and include it in a section of the .so, assuming you are just doing dynamic linking. If you are doing dynamic loading, yes, you'd need to JIT it. But still doable. (i won't point out that dynamic loading is not actually legal in any of these languages :P)