开发者

Hints for the compiler to help it with the optimization task

开发者 https://www.devze.com 2023-01-25 15:55 出处:网络
The const a开发者_开发知识库nd 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.

The const a开发者_开发知识库nd 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))?

Thanks!


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.


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&).


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号