Can const-correctness improve performance?
Solution 1:
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.
Solution 2:
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.
Solution 3:
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/
Solution 4:
in my experience, no
For scalar variables, compiler is able to determine whenever the value is changed and perform necessary optimization itself.
For array pointers, const correctness is no guarantee that values are really constant in presence of potential aliasing problems. Hence compiler can not use const modifier alone to perform optimizations
if you are looking optimization, you should consider __restrict__
or special function modifiers/attributes: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html