Source
Case - 1:
When you declare a const in your program,
int const x = 2;
Compiler can optimize away this const by not providing storage to this variable rather add it in symbol table. So, subsequent read just need indirection into the symbol table rather than instructions to fetch value from memory.
Note: If you do something like:
const int x = 1;
const int* y = &x;
Then this would force compiler to allocate space for x
. So, that degree of optimization is not possible for this case.
In terms of function parameters const
means that parameter is not modified in the function. As far as I know, there's no substantial performance gain for using const
rather it's a means to ensure correctness.
Case - 2:
"Does declaring the parameter and/or the return value as const help the compiler to generate more optimal code?"
const Y& f( const X& x )
{
// ... do something with x and find a Y object ...
return someY;
}
What could the compiler do better? Could it avoid a copy of the parameter or the return value?
No, as argument is already passed by reference.
Could it put a copy of x or someY into read-only memory?
No, as both x
and someY
live outside its scope and come from and/or are given to the outside world. Even if someY
is dynamically allocated on the fly within f()
itself, it and its ownership are given up to the caller.
What about possible optimizations of code that appears inside the body of f()? Because of the const, could the compiler somehow improve the code it generates for the body of f()?
Even when you call a const member function, the compiler can't assume that the bits of object x
or object someY
won't be changed. Further, there are additional problems (unless the compiler performs global optimization): The compiler also may not know for sure that no other code might have a non-const reference that aliases the same object as x
and/or someY
, and whether any such non-const references to the same object might get used incidentally during the execution of f();
and the compiler may not even know whether the real objects, to which x
and someY
are merely references, were actually declared const in the first place.
Case - 3:
void f( const Z z )
{
// ...
}
Will there be any optimization in this?
Yes because the compiler knows that z
truly is a const object, it could perform some useful optimizations even without global analysis. For example, if the body of f()
contains a call like g( &z )
, the compiler can be sure that the non-mutable parts of z
do not change during the call to g()
.