75

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)?

11
  • 13
    const on the target of a pointer or reference does not mean constant. It means that this is a read-only view of the object. Access to the same object by other means may yet change it. Only const on an object definition actually makes it immutable.
    – Ben Voigt
    Dec 14, 2014 at 5:31
  • 34
    The main point of using const is not to assist the compiler in optimizations but to protect yourself from mistakes. Actually, I doubt that any compilers rely on const-qualification to optimize the code.
    – ach
    Dec 14, 2014 at 5:31
  • 1
    @BenVoigt I know that it does not make the object constant, but only make it appear constant for the scope of this function. Maybe I didn't use the correct term to express what I mean.
    – UnTraDe
    Dec 14, 2014 at 5:33
  • 5
    The short answer is that const makes no difference to optimization; it's to help catch bugs at compile-time.
    – M.M
    Dec 14, 2014 at 7:13
  • 6
    C and C++ are two different languages with a common heritage. While comments about the non-existent hybrid "C/C++" might provide some insight, focusing on one or the other will be much more meaningful. Dec 14, 2014 at 14:35

5 Answers 5

43

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 for this variable; instead it can be added to the symbol table. So a subsequent read just needs 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().

10
  • 2
    The optimization is still possible. The language rules prohibit changing x, so its value of 1 can still be substituted anywhere the value is needed. You're right that there needs to be memory to take an address, but accesses to that memory through the name x can be skipped.
    – Ben Voigt
    Dec 14, 2014 at 5:43
  • @BenVoigt: The accesses to that memory address can only be skipped if the compiler can prove that it is indeed an access to the memory address each time the pointer is referenced. For example, consider foo(&y); bar(*y); where foo is defined in another translation unit. Since y itself is not const, the compiler cannot know whether foo changes y, therefore it cannot optimize away *y since at that place it cannot tell where y points to. But since y might still point to x, the memory address for x must exist and contain the value 1.
    – celtschk
    Dec 14, 2014 at 13:20
  • 11
    Downvoted for plagiarising Sutter without attribution: gotw.ca/gotw/081.htm Apr 14, 2016 at 8:11
  • 5
    @ravi you don't really need policies in order not to plagiarise. Just don't take credit for other people's work, not only on stackoverflow but everywhere. It's not nice.
    – Varaquilex
    Nov 23, 2016 at 1:44
  • 2
    @all_blaming_plagiarism I believe this article is completely in Herb's Sutter book. So, it's meant to transfer his knowledge to world. And I think I am doing the same over here except not including the source of information. Anyway will take care of attribution next time.
    – ravi
    Jan 2, 2017 at 10:02
16

Before giving any answer, I want to emphasize that the reason to use or not use const really ought to be for program correctness and for clarity for other developers more so than for compiler optimizations; that is, making a parameter const documents that the method will not modify that parameter, and making a member function const documents that that member will not modify the object of which it is a member (at least not in a way that logically changes the output from any other const member function). Doing this, for example, allows developers to avoid making unnecessary copies of objects (because they don't have to worry that the original will be destroyed or modified) or to avoid unnecessary thread synchronization (e.g. by knowing that all threads merely read and do not mutate the object in question).

In terms of optimizations a compiler could make, at least in theory, albeit in an optimization mode that allows it to make certain non-standard assumptions that could break standard C++ code, consider:

for (int i = 0; i < obj.length(); ++i) {
   f(obj);
}

Suppose the length function is marked as const but is actually an expensive operation (let's say it actually operates in O(n) time instead of O(1) time). If the function f takes its parameter by const reference, then the compiler could potentially optimize this loop to:

int cached_length = obj.length();
for (int i = 0; i < cached_length; ++i) {
   f(obj);
}

... because the fact that the function f does not modify the parameter guarantees that the length function should return the same values each time given that the object has not changed. However, if f is declared to take the parameter by a mutable reference, then length would need to be recomputed on each iteration of the loop, as f could have modified the object in a way to produce a change in the value.

As pointed out in the comments, this is assuming a number of additional caveats and would only be possible when invoking the compiler in a non-standard mode that allows it to make additional assumptions (such as that const methods are strictly a function of their inputs and that optimizations can assume that code will never use const_cast to convert a const reference parameter to a mutable reference).

19
  • 3
    This depends on whether the dynamic type of obj is known to be const, whether f takes its parameter by copy or reference, and whether the body of f is visible here. It does NOT depend on whether f taking a reference parameter is const-qualified or not.
    – Ben Voigt
    Dec 14, 2014 at 5:41
  • 4
    Your new edit (saying const reference instead of "const parameter") is much clearer. It's clearly wrong now. The transformation you mentioned is possible only if obj is created const, or the compiler can see inside the length() member function.
    – Ben Voigt
    Dec 14, 2014 at 5:44
  • 7
    @MichaelAaronSafyan: A const function can still return a different result on each call. This optimization actually requires that the function is marked idempotent or the compiler can deduce that by inspection of the function body. The parameter type doesn't help.
    – Ben Voigt
    Dec 14, 2014 at 5:52
  • 5
    @MichaelAaronSafyan "though, really, it shouldn't be marked const in such a case" -- Why not? The only meaning that const conveys on a method is that it will not alter the object on which it is invoked when it is called. It does not mean that the return value will be the same each time it is called even if the object isn't changed. const on a method is a promise not to change the object; it doesn't imply idempotence.
    – cdhowie
    Dec 14, 2014 at 8:52
  • 3
    @MichaelAaronSafyan Ultimately it has nothing to do with what we think const implies on a member function nor when we think it should be used. The standard is the authority, and the as-if rule governs all optimization. If the compiler can prove that the const method is idempotent then it can elide calls in a block of code where the object isn't modified. But if it can't prove this and it does the optimization anyway, the as-if rule is broken and it's not really a C++ compiler, just a compiler that can parse C++ code but that interprets it incorrectly.
    – cdhowie
    Dec 14, 2014 at 9:03
7

Function parameters:

const is not significant for referenced memory. It's like tying a hand behind the optimizer's back.

Suppose you call another function (e.g. void bar()) in foo which has no visible definition. The optimizer will have a restriction because it has no way of knowing whether or not bar has modified the function parameter passed to foo (e.g. via access to global memory). Potential to modify memory externally and aliasing introduce significant restrictions for optimizers in this area.

Although you did not ask, const values for function parameters does allow optimizations because the optimizer is guaranteed a const object. Of course, the cost to copy that parameter may be much higher than the optimizer's benefits.

See: http://www.gotw.ca/gotw/081.htm


Variable declarations: const int i = 1234

This depends on where it is declared, when it is created, and the type. This category is largely where const optimizations exist. It is undefined to modify a const object or known constant, so the compiler is allowed to make some optimizations; it assumes you do not invoke undefined behavior and that introduces some guarantees.

const int A(10);
foo(A);
// compiler can assume A's not been modified by foo

Obviously, an optimizer can also identify variables which do not change:

for (int i(0), n(10); i < n; ++i) { // << n is not const
 std::cout << i << ' ';
}

Function declarations: const char* foo()

Not significant. The referenced memory may be modified externally. If the referenced variable returned by foo is visible, then an optimizer could make an optimization, but that has nothing to do with the presence/absence of const on the function's return type.

Again, a const value or object is different:

extern const char foo[];

5

The exact effects of const differ for each context where it is used. If const is used while declaring an variable, it is physically const and potently resides in read-only memory.

const int x = 123;

Trying to cast the const-ness away is undefined behavour:

Even though const_cast may remove constness or volatility from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const or to access an object that was declared volatile invokes undefined behavior. cppreference/const_cast

So in this case, the compiler may assume that the value of x is always 123. This opens some optimization potential (constants propagation)

For functions it's a different matter. Suppose:

void doFancyStuff(const MyObject& o);

our function doFancyStuff may do any of the following things with o.

  1. not modify the object.
  2. cast the constness away, then modify the object
  3. modify an mutable data member of MyObject

Note that if you call our function with an instance of MyObject that was declared as const, you'll invoke undefined behavior with #2.

Guru question: will the following invoke undefined behavior?

const int x = 1;
auto lam = [x]() mutable {const_cast<int&>(x) = 2;};
lam();
3
  • You're probably right that modifying the mutable data member of an const object is defines behavior. I'll edit that part. But i think you got my guru question wrong. An by-value lambda capture preserves the const-ness of the captures. So internally inside the lambda we have an variable x which is declared as const int. Modifying that one is undefined behavior.
    – Stefan
    Dec 14, 2014 at 15:55
  • @Deduplicator I wish it was that way, but it isn't. See youtube.com/watch?v=48kP_Ssg2eY from 16:40.
    – Stefan
    Dec 14, 2014 at 16:23
  • Careful, the const semantics of lambdas not explicitly marked as mutable have changed.
    – Ben Voigt
    Dec 14, 2014 at 17:04
4

SomeClass* const pObj creates a constant object of pointer type. There exists no safe method of changing such an object, so the compiler can, for example, cache it into a register with only one memory read, even if its address is taken.

The others don't enable any optimizations specifically, although the const qualifier on the type will affect overload resolution and possibly result in different and faster functions being selected.

4
  • Can you elaborate more on overload part? Maybe an example?
    – UnTraDe
    Dec 14, 2014 at 5:38
  • @UnTraDe: I'm trying to think of a case in the Standard library where the const version of a function does something radically different, and I can't. User code and other libraries might do such a thing, however.
    – Ben Voigt
    Dec 14, 2014 at 5:47
  • @BenVoigt, I remember the old days when implementors of the C++ standard library experimented with reference-counted std::strings. Calling, e. g., begin() on a non-const string caused its detachment; that is, if the std::string object shared the string with another std::string object, it was copied at this moment and marked as non-reference-countable. Calling begin() on a const string did not change its internal state.
    – ach
    Dec 14, 2014 at 6:12
  • @Andrey: Yes, that the type of improvement via overload selection I'm talking about. But it isn't standard-compliant for std::string to do that, and I'm not aware of any other class in the Standard which has such a behavior.
    – Ben Voigt
    Dec 14, 2014 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.