6

Marking an online function when compiling with -O3?

 3 years ago
source link: https://www.codesd.com/item/marking-an-online-function-when-compiling-with-o3.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Marking an online function when compiling with -O3?

advertisements

C++ allows you to annotate functions with the inline keyword. From what I understand, this provides a hint (but no obligation) to the compiler to inline the function, thereby avoiding the small function calling overhead.

I have some methods which are called so often that they really should be inlined. But inline-annotated functions need to be implemented in the header, so this makes the code less well-arranged. Also, I think that inlining is a compiler optimization that should happen transparently to the programmer where it makes sense.

So, do I have to annotate my functions with inline for inlining to happen, or does GCC figure this out without the annotation when I compile with -O3 or other appropriate optimization flags?


inline being just a suggestion to compiler is not true & is misleading.There are two possible effects of marking a function inline:

  1. Substitution of function definition inline to where the function call was made &
  2. Certain relaxations w.r.t One definition rule, allowing you to define functions in header files.

An compiler may or may not perform #1 but it has to abide to #2. So inline is not just a suggestion.There are some rules which will be applied once function is marked inline.

As a general guideline, do not mark your functions inline just for sake of optimizations. Most modern compilers will perform these optimizations on their own without your help. Mark your functions inline if you wish to include them in header files because it is the only correct way to include a function definition in header file without breaking the ODR.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK