UnTraDe UnTraDe - 29 days ago 7
C Question

What kind of optimization does const offer in C/C++? (if any)

I know that where possible you should use the const keyword when passing parameters around by reference or by pointer for readability reasons. Is there any optimizations that the compiler can do if I specify that an argument is constant?

There could be a few cases:

Function parameters:

Constant reference:

void foo(const SomeClass& obj)


Constant SomeClass object:

void foo(const SomeClass* pObj)


And constant pointer to SomeClass:

void foo(SomeClass* const pObj)


Variable declarations:

const int i = 1234


Function declarations:

const char* foo()


What kind of compiler optimizations each one offers (if any)?

Answer

[Readers please note that the majority of this post is lifted from an article by Herb Sutter - http://www.gotw.ca/gotw/081.htm - without attribution by the OP.]

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 below:-

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;
  }

Ques=> 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.

Ques=> 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 )
  {
    // ...
  }

Ques=> 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()