I have read numerous times that enforcing const-correctness in your C or C++ code is not only a good practice with regards to maintainability, but also it may allow your compiler to perform optimizations. However, I have read the complete opposite, too — that it does not affect performance at all.
Therefore, do you have examples where const correctness may aid your compiler with improving your program's performance?
The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.
No, const does not help the compiler make faster code. Const is for const-correctness, not optimizations.
Concerning objects (especially strings), call by reference is faster than call-by-value because the function call does not need to create a copy of the original object. Using const, one can also ensure that the reference is not abused.
In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.
const
correctness can't improve performance because const_cast
and mutable
are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const
data may e.g. be a pointer to a std::atomic
, meaning the compiler has to respect changes made by other threads.
That said, it is trivial for the compiler to look at the code it generates and determine if it actually writes to a given variable, and apply optimizations accordingly.
That all said, const
correctness is a good thing with respect to maintainability. Otherwise, clients of your class could break that class's internal members. For instance, consider the standard std::string::c_str()
-- if it couldn't return a const value, you'd be able to screw around with the internal buffer of the string!
Don't use const
for performance reasons. Use it for maintainability reasons.
Yes it can.
Most const
s are purely for the benefit of the programmer and do not help the compiler optimize because it's legal to cast them away and so they don't tell the compiler anything useful for optimization. However, some const
s cannot be (legally) cast away and these do provide the compiler with useful information for optimization.
As an example, access to a global variable defined with a const
type can be inlined while one without a const
type cannot be inlined because it might change at runtime.
https://godbolt.org/g/UEX4NB
C++:
int foo1 = 1;
const int foo2 = 2;
int get_foo1() {
return foo1;
}
int get_foo2() {
return foo2;
}
asm:
foo1:
.long 1
foo2:
.long 2
get_foo1():
push rbp
mov rbp, rsp
mov eax, DWORD PTR foo1[rip] ; foo1 must be accessed by address
pop rbp
ret
get_foo2():
push rbp
mov rbp, rsp
mov eax, 2 ; foo2 has been replaced with an immediate 2
pop rbp
ret
In practical terms, keep in mind that while const
can improve performance, in most cases it won't or it will but the change will not be noticeable. The primary usefulness of const
is not optimization.
Steve Jessop gives another example in his comment on the original question which brings up something worth mentioning. In a block scope, it's possible for a compiler to deduce if a variable will be mutated and optimize accordingly, regardless of const
, because the compiler can see all uses of the variable. In contrast, in the example above, it's impossible to predict if foo1
will be mutated since it could be modified in other translation units. I suppose a hypothetical sentient ultra-compiler could analyze an entire program and determine if it's valid to inline access to foo1
... but real compilers can't.
A bit old, but still applies: http://www.gotw.ca/gotw/081.htm And some more: http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With