0

There are already many good explains about "inline" in c++, such as this and this...

But generally most of them are talking about "even though a func with inline prefix, may or may not be expanded", or about "it's up to the compiler...".

When I am refactoring my old codes recently, I need to decide whether keep or move some inline functions/methods into definite file(instead of header file). So I need to know that "If a func/method(without inline prefix) be implemented in *.cpp file instead of header file, may it NEVER be inline/expanded?". If we keep them stay in headers, at least there be a chance/hope them would be inlined?

Pls forgive my ugly English, it's not my primary language.

Leon
  • 1,489
  • 1
  • 12
  • 31
  • When I think about inclining my first thought is will this code execute thousands of times such that the optimization can make a measurable time difference. – drescherjm May 19 '20 at 15:29
  • My codes flagged as inline, are executing about 2000~3000 times per second, all day and all night, it's the reason I make them inline. but the compiling process becomes longer and longer. – Leon May 19 '20 at 15:36
  • That is a trade off for moving the code to the header. – drescherjm May 19 '20 at 15:38
  • 3
    The `inline` keyword is mostly for ODR purposes, not actually inclining of code. Most compilers mostly ignore it for that. And functions in .cpp files can certainly be inlined when using link time optimization (LTO), regardless of the presence of `inline` or not. – Jesper Juhl May 19 '20 at 15:44
  • Reminder: inline functions may be a cause of Code Bloat. Wherever the call to the inline code is, the compiler pastes the code. You may want to limit the inlining to functions where the overhead of calling the function is greater than the execution of the content. – Thomas Matthews May 19 '20 at 16:20
  • Another reminder: any changes made to the inline code will cause all translation units dependent on the header file to be recompiled. Placing code into translation units avoids this issue. – Thomas Matthews May 19 '20 at 16:22
  • 1
    @JesperJuhl thanks a lot! Can we have a method to inspect whether or not a function be inlined actually? – Leon May 19 '20 at 17:00
  • 1
    @Leon You can always use tools like `objdump`, `readelf`, `nm` and similar to inspect the generated object files, libraries, executables etc. Knowledge of assembly language highly recommended (read: required) & remember that making sense of optimized code can be *Hard*. – Jesper Juhl May 19 '20 at 18:12

2 Answers2

2

Remember that in C++ (and in C), what the compiler works with is a translation unit, in which - after preprocessing - there is no distinction between lines from a .cpp file and lines from a header (.h/.hpp/.hxx) file. So, in the translation unit containing the definition of the function - nothing changes. inlining is just as likely.

What about the other translation units, which used to see the function's definition, and now see only its declaration? Here, the main question is: "Does my compiler and linker support link-time optimization?" If your compiler doesn't support LTO, or if you haven't enabled LTO support - then, generally, your function won't be inlined. If LTO is supported and enabled, the linker has the "deeper" information which allows it to rearrange an already-compiled function so as to plug in the code of another such function from a different compiled object. So, with LTO, your .cpp-defined function might still get inlined. But it is perhaps less likely than during compilation proper.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Sorry, I missed your answer. It's very professional and useful for me! Thank you very much!!! – Leon May 21 '20 at 15:21
1

Will c++ functions/methods implemented in *.cpp file be NEVER inline expanded?

This is an implementation issue, specific to your particular C++ compiler.

If you use a recent GCC as your C++ compiler, you could be interested by compiler flags such as -Wall -O0 -fno-inline (or, on the opposite, optimizing with -flto -fwhole-program -Wall -O3 both at compile and link time). Configure wisely your build automation tool (e.g. your Makefile).

But the resulting executable might run slower.

I am using in May 2020 (e.g. in RefPerSys) g++ -fno-inline -O0 -g3 with GCC 9 or GCC 10 for ease of debugging with GDB (on Linux/Debian/Sid/x86-64). The same C++ source code can be compiled twice in two different ELF executables, one with full DWARF information and and no inlining, and another with DWARF information and optimizing flags.

When I am refactoring my old codes recently, I need to decide whether keep or move some inline functions/methods into definite file (instead of header file).

The only rational reason to do that is to reduce compilation time. Is it such a real concern? How many hours do you need to compile your C++ code? You might also ask your manager or client to buy you a more powerful development computer. Ask on Workplace for guidance and provide legal details.

A possibility might be to compile with g++ -Wall -Wextra -O0 -fno-inline -g during the debugging phase, and to deliver the same C++ code compiled and linked with g++ -Wall -Wextra -O3 -flto for production purposes. If executable size is a concern, consider replacing -O3 with -Os. YMMV.

Think also of legal considerations, see this.

Of course read more about C++.

The n3337 C++11 standard comes to mind.

Regarding inlining, you could compile with g++ -S -fverbose-asm -O3 and look into the generated code if inlining happened. You could even write your own GCC plugin to drive inlining decisions (see the GCC MILEPOST project or the old GCC MELT one or Bismon). This would require a few months of fulltime work.

PS. I happen to professionally work on Bismon and did contribute to GCC. Feel free to contact me by email to basile.starynkevitch@cea.fr about it. Maybe my boss could be delighted by a contract (only above 50k€) about it. The decision won't be mine, but that of my employer CEA LIST (and then political considerations matter regarding policies between France -near Paris- where I work and live and China -near Singapore- where you work).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you! your answer is very professional and useful! How did you fingure out that I'm a Chinese? Hah! – Leon May 21 '20 at 14:09
  • Please upvote my answer if it fits. Your profile mentions a corporation; I used Google to find out its geographical location – Basile Starynkevitch May 21 '20 at 14:09
  • By the way, probably Singapore is not enough near from China, if you think Paris is near France. The air line distance btw Singapore and China is equivalent to the distance btw Paris and Stockholm, and Singapore is not same country with China, even though most people there speak Chinese. – Leon May 21 '20 at 14:22
  • Starynkevith What was you meaning in your answer bottom i.g. "Maybe my boss could be delighted by a contract..."? You are looking for some soft order or helping us for developing? – Leon May 21 '20 at 14:29
  • @BasileStarynkevitch: I think you should have concluded your answer with the suggestion to contact you and not gone into details about contracts and sums-of-money etc. - that's not appropriate, IMHO, for an SO answer. – einpoklum May 21 '20 at 15:27
  • On another note - it's not true that the only reason to move an implementation out of a header file is compilation speed. If the header file is a public API, you very often want to avoid committing to the specific implementation; and you sometimes prefer for you, rather than your users, to provide the compiled code. – einpoklum May 21 '20 at 15:31