Sign up ×

The const and volatile chapter on the 'Surviving the Release Version' Article gave me the idea that the compiler can use the const keyword as hint for its optimization job.

Do you know some other optimization-hints for the compiler or design principles for functions so that the compiler can make them inline?

By the way, do you declare primitive-type function parameters as const or const reference (like void foo(const int i) or void foo(const int& i))?


share|improve this question

7 Answers 7

up vote 2 down vote accepted

It is rare that const qualification can help the compiler to optimize your code. You can read more about why this is the case in Herb Sutter's "Constant Optimization?"

Concerning your last question: in general, you should prefer to pass by value things that are cheap to copy (like fundamental type objects--ints and floats and such--and small class type objects) and pass other types by const reference. This is a very general rule and there are lots of caveats and exceptions.

share|improve this answer

I don't think the compiler can use const keyword for optimization since at any point the constness can be casted away.

It is more for correctness than optimization.

share|improve this answer
Wrong assumption. Casting const away is not always allowed, i.e. in const int i = 5, the const-ness of i may not be cast away. The optimizer may (and will!) rely on this, by substituting 5 for a variable access. When you do try to cast away the const-ness of a const object, you get undefined behavior, so the optimizer may emit code that's incorrect for those cases. –  MSalters Nov 15 '10 at 10:52
I see. However, does the const in that case make any difference? Won't the compiler know that "i" is unchanged and inline it anyway? –  ronag Nov 15 '10 at 11:26

As soon as you enable some optimization the compiler will notice that the parameter i is never modified, so whether you declare it as int or as const int doesn't matter for the generated code.

The point of passing parameters by const & is to avoid needless copying. In case of small parameters (one machine word or less) this doesn't lead to better performance, so you shouldn't do that. foo(int) is more efficient than foo(const int&).

share|improve this answer

There's no practical benefit to either form. If the type is less than a single machine word, take it by value. The other thing is that a modern compiler's semantic analysis is way above what const can and can't do, you could only apply optimizations if it was pre-compiled or your code was VERY complex. The article you linked to is several years old and the compiler has done nothing but improve massively since then.

share|improve this answer

A few "general compiler" things off the top of my head.

  • const as a hint that a variable will never change
  • volatile as a hint that a variable can change at any point
  • restrict keyword
  • memory barriers (to hint to the compiler a specific ordering) - probably not so much an "optimisation" mind you.
  • inline keyword (use very carefully)

All that however should only come from an extensive profiling routine so you know what actually needs to be optimised. Compilers in general are pretty good at optimising without much in the way of hints from the programmer.

share|improve this answer
Of those the only thing that does anything for optimization on modern compilers, would be the restrict keyword... which is not standard C++. –  ronag Nov 15 '10 at 10:35

If you look into the sources of Linux kernel or some such similar projects, you will find all the optimisation clues that are passed on to gcc (or whichever compiler is used). Linux kernel uses every feature that gcc offers even if it is not in the standard.

This page sums up gcc's extensions to the C language. I referred C here because const and volatile are used in C as well. More than C or C++, compiler optimization appears the focus of the question here.

share|improve this answer

I don't think the real purpose of const has much to do with optimization, though it helps. Isn't the real value in compile-time checking, to prevent you from modifying things you shouldn't modify, i.e. preventing bugs?

For small arguments that you are not going to modify, use call-by-value.

For large arguments that you are not going to modify, use either call-by-reference or passing the address (which are basically the same thing), along with const.

For large or small arguments that you are going to modify, drop the const.

BTW: In case it's news, for real performance, you need to know how to find the problems you actually have, by profiling. No compiler can do that for you.

share|improve this answer

Your Answer


