r/cpp • • Nov 28 '18

Google C++ Style Guide is No Good

https://eyakubovich.github.io/2018-11-27-google-cpp-style-guide-is-no-good/
34 Upvotes

61 comments sorted by

View all comments

107

u/tehjimmeh Nov 28 '18

Marking the function “inline” lets the compiler make the decision. Not marking it inline is a sure way to prevent inlining, unless Link Time Optimizations are turned on.

This is so wrong it hurts.

22

u/Supadoplex Nov 28 '18 edited Nov 28 '18

It's just a slight oversimplification. What prevents expanding a function invocation inline is defining the function in a separate translation unit from where the function is invoked (except, as the quote points out, LTO may be able to lift this restriction).

What inline declaration technically does, is relax the one-definition-rule such that a function can be defined in multiple translation units which is otherwise ill-formed according to ODR (and replaces the restriction with a new one: that the function must be defined in all TU's which invoke it).

In effect, this allows a function to be expanded inline in more than one translation unit. The emphasized qualification being the part that the quote left out for simplicity.

0

u/[deleted] Nov 28 '18

[deleted]

5

u/Supadoplex Nov 29 '18 edited Nov 29 '18

... and serves as a hint to most compilers.

I've read other programmers claim that, but I'm not convinced. The article below is quite convincing.

Can you provide me with a minimal example where the compiler takes the hint i.e. expands a function inline only when the function is declared inline even when the function is defined in the same TU regardless.

If all compilers ignore the hint, then it's not much of a hint.

The article seems to imply that the keyword WILL inline the function ...

The article seems to imply the opposite: That the compiler gets to decide whether whether to expand inline or not - which is correct.

The article seems to imply that ... the absence of [inline] would somehow stop it.

That the article does imply and it is indeed false, unless, as I pointed out, the statement is qualified: The absence of inline would stop inline expansion into more than one translation unit (which requires additional qualification that was not left out in the article that LTO may relax this).

Perhaps the article should have been more specific.

20

u/Veedrac Nov 29 '18

11

u/Supadoplex Nov 29 '18

Thanks, nice article.

TL;DR: The author did find pieces of code in both GCC and Clang that do take inline declaration as a hint.

6

u/VinnieFalco wg21.org | corosio.org Nov 29 '18

UGH I wish I saw that blog post 2 years ago...

5

u/[deleted] Nov 29 '18

Also a C++ Weekly on this.

0

u/tehjimmeh Nov 29 '18 edited Nov 29 '18

But like, your code wouldn't link if you omitted 'inline' (or 'static', I guess).

In 2018, when there's very little justification for not compiling your release build with LTO, where translation unit boundaries don't matter for inlining. I really don't get why LTO gets sometimes talked about like some esoteric, experimental option rather than the normal, expected mode of compilation.

'inline' since C++11 has meant these days just means relaxing ODR like you describe. It's the reuse of an essentially redundant keyword which sort of makes sense because it sort of can relate to inlining in the traditional compilation model. However, it does not directly relate to inlining (other than sometimes being a hint for back compat reasons) and should not really be discussed as if it does.

3

u/Supadoplex Nov 29 '18 edited Nov 29 '18

But like, your code wouldn't link if you omitted 'inline' (or 'static', I guess).

You have two options: Either you declare the function inline and define it in all translation units that invoke it, or you define the function in exactly one TU.

If you do define a function in multiple TU's, then you must first change that before you can omit inline. Once you conform to one of the options listed in previous paragraph, the code should link successfully (assuming it conforms to the standard in other respects as well).

In case of template functions with unbounded template arguments, they must be inline, you don't have a choice. If you want to provide a "header only library", then inline is the only choice as well.

In other cases, you can make the choice: Inline (and define in all TU where the function is invoked), or non-inline (and define exactly once). As far as I can tell, the only reason to choose inline is to allow inline expansion into more than one TU (sans LTO) - and also apparently to hint the optimiser as discussed in another branch of this thread.

In 2018, when there's very little justification for not compiling your release build with LTO

Perhaps. But if you distribute source rather than binary, you often cannot assume that your clients can use LTO. Furthermore, are you certain that LTO is able to perform all optimisations (across TU's) that regular optimiser does (within a TU)?

'inline' since C++11 has meant relaxing ODR ...

No. As far as I can tell, the meaning of inline did not change in C++11. The meaning of inline function has been the same since standardisation at least. I don't know much about pre-standard C++, the meaning may have been different in some of the old dialects.

1

u/Gotebe Dec 01 '18

He who provides sources really should also provide the build sources.

I am not building e.g. OpenSSL by making my own makefile for it. I do change it for... reasons, but if LTO was in, it stayed in.

0

u/tehjimmeh Nov 29 '18

As far as I can tell, the only reason to choose inline is to allow inline expansion into more than one TU

I mean, you just stated the other reason in your previous paragraph - header only libraries. (Or just stylistic reasons - sometimes people just like to have short functions defined in header files.)

apparently to hint the optimiser as discussed in another branch of this thread.

Yeah, I think the 3 major compilers do use it as a hint under some circumstances, but then again, they use lots of things as hints/heuristics. If you listen to e.g. Chandler Carruth talking about it, this is mostly kept for back compat reasons - i.e. if this were removed, it might end up not inlining something in some big codebase which could regress performance, and then the inliner would need to be tuned to get those cases etc. Inlining decisions are tricky.

Furthermore, are you certain that LTO is able to perform all optimisations (across TU's) that regular optimiser does (within a TU)?

Yeah, I mean, the difference between LTO and non-LTO is that the optimizer sees all functions at once, not just all the functions in a single TU at a time, so intuitively this should be the case (along with many other whole program optimizations).

I'll concede that sometimes libraries are distributed as compiled static or dynamic libraries and headers, and in those cases, inline functions in the headers for the purpose of enabling inlining can sometimes be desirable.

No. As far as I can tell, the meaning of inline did not change in C++11. The meaning of inline function has been the same since standardisation at least. I don't know much about pre-standard C++, the meaning may have been different in some of the old dialects.

Yeah, I wrote that paragraph too fast. I do think something to do with 'inline' changed or was formalized in C++11, but I'm not sure. What I meant to say is that these days it just doesn't directly relate to inlining.