3

!! Specific on frequently used methods like getter & setter. !!

I have no idea when the keyword inline should be used. Ofc I know what it does, but I still have no idea.

According to an interview with Bjarne Stroustrup he said:

My own rule of thumb is to use inlining (explicitly or implicitly) only for simple one- or two-line functions that I know to be frequently used and unlikely to change much over the years. Things like the size() function for a vector. The best uses of inlining is for function where the body is less code than the function call and return mechanism, so that the inlined function is not only faster than a non-inlined version, but also more compact in the object core: smaller and faster.

But I often read that the compiler automatically inline short functions like getter, setter methods (in this case getting the size() of a vector).

Can anyone help?

Edit:

Coming back to this after years and more experience the high performance C+++ programming, inline can indeed help. Working in the games industry even forceinline sometimes makes a difference, since not all compilers work the same. Some might inline automatically some don't. My advice is if you work on frameworks, libraries or any heavily used code consider the use of inline, but this is just general advice anyway since you want such code to be fully optimized for any compiler. Always using inline might not be the best, because you'll also need the class definition for this part of the code. Sometimes this can increase compilation times if you can't use forward declarations anymore.

another hint: you can use C++14 auto return type deduction even with seperating the function definition:

MyClass.h

class MyClass
{
    int myint;
public:
    auto GetInt() const;
}  

inline auto MyClass::GetInt() const { return myint; }

all in one .h file.

jeromintus
  • 367
  • 1
  • 5
  • 14
  • 1
    This may be a duplicate of http://stackoverflow.com/questions/145838 – David Segonds Aug 13 '14 at 11:28
  • still have no idea if simple functions like Stroustrup said, should be used with inline in header(like getter/setter other online functions) – jeromintus Aug 13 '14 at 13:01
  • had no difference using inline on getter/setter, so it seems like, it doesn't matter if I use inline or not. Stroustrup's inline rule of thumb seems to have no impact ._. – jeromintus Aug 13 '14 at 13:26
  • possible duplicate of [Benefits of inline functions in C++?](http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c) – David Segonds Aug 19 '14 at 12:40
  • actually no, my question was more specific on frequently used methods like getter and setters and if it's recommended to inline them, or at least give the compiler a hint. – jeromintus Aug 19 '14 at 13:38

6 Answers6

6

Actually, inline keyword is not for the compiler anymore, but for the linker.

That is, while inline in function declaration still serves for most compilers as a hint, on high optimization setting they will inline things without inline and won't inline things with inline, if they deem it better for the resulting code.

Where it is still necessary is to mark function symbols as weak and thus circumvent One Definition Rule, which says that in given set of object files you want to make into a binary, each symbol (such as function) shall be present only once.

Xarn
  • 3,460
  • 1
  • 21
  • 43
  • so it doesn't matter or improve my performance a lot, If use/don't use inline? – jeromintus Aug 13 '14 at 11:48
  • 1
    @Sleicreider IIRC, if you use -O2 on GCC, sprinkling `inline` in the right place will help a lot. (On -O2, GCC inlines only functions with `inline` hint.) On -O3, it mostly doesn't matter, except for correctness. – Xarn Aug 13 '14 at 11:53
  • I've actually never change the optimization level, especially because i'm using a Engine for game development right now. why would anyone want something lower than -O3 then? – jeromintus Aug 13 '14 at 11:57
  • @Sleicreider Because -O3 is very aggressive in its optimization, which leads to higher chance of either miscompile or exploiting Undefined Behavior to transform the code in way the programmer didn't expect. (Both of which lead to given program behaving differently from expectation.) – Xarn Aug 13 '14 at 12:09
  • Whats the default optimization level in Visual Studio – jeromintus Aug 13 '14 at 12:12
3

Bjarne's quote is old. Modern compilers are pretty smart at it.

That said, if you don't use Link Time Code Generation, the compiler must see the code to inline it. For functions used in multiple .cpp files, that means you need to define them in a header. And to circumvent the One Definition Rule in that case, you must define those functions as inline.

Class members defined inside the class are inline by default, though.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

The below speaks specifically to C++:

The inline keyword has nothing to do with inlining.

The inline keyword allows the same function to be defined multiple times in the same program:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required.

§3.2 [basic.def.odr]

Attaching meaning beyond this to the inline keyword is erroneous. The compiler is free to inline (or not) anything according to the "as-if rule":

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input.

§1.9 [intro.execution]

  • No, it doesn't. The language construct's only normative purpose is allowing a function to be defined multiple times in the same program. Connotations relating to inlining the function are not normative, and since the compiler is free to inline **any** function, should be ignored. – Robert Allan Hennigan Leahy Aug 13 '14 at 12:52
1

Considering what compiler optimizations can do, the only use of inline I have today is for non-template function whose body is defined inside headers files outside class bodies.

Everything is defined (note: defined != declared) inside class bodies is inline by default, just as templates are.

The meaning of inline in fact is: "Defined in header, potentially imported in multiple sources, just keep just one copy of it" told to the linker.

May be in c++35 someone will finally decide to replace that keyword with another one more meaningful.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
  • Misplaced downvote. This repeats what's already said, doesn't improve on that, but it's not wrong. – MSalters Aug 13 '14 at 12:34
  • It is incorrect to say that `inline `means "[d]efined in header". – Robert Allan Hennigan Leahy Aug 13 '14 at 12:37
  • @RobertAllayHenniganLeahy: if "correct" means "as spec. say" you're right. If "correct" means "understandable from the op" may be you're not. – Emilio Garavaglia Aug 13 '14 at 12:41
  • It's misleading and incorrect on every level. You're more than welcome to define an `inline` function in not-a-header, and the compiler won't care, because the difference between "header" and "source" is an abstraction that's flattened away by the preprocessor. – Robert Allan Hennigan Leahy Aug 13 '14 at 12:49
  • @RobertAllanHenniganLeahy: Saying "misleading and incorrect on every level" it's very dangerous: one single example and ypo're doomed. BE POLITE AND DON'T OFFEND. The OP himself spoke about "his rule of thiumbs". And there nothing incorrct in giving him "my own". You can define inline in source but - as you said- it will have no effect so it has no clue in doinf it. Using inline is required to produce visible effect only when defining functions in headers. Every other use is either implicit or inifluent. Feel free to have you opnion, but before to say someone "incorrect at every level"... – Emilio Garavaglia Aug 13 '14 at 13:04
0

C++ standard Section 7.1.2 Point 2:

(...) The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call (...)

In other words instead of havin a single code for your function, that is called several times, the compiler may just duplicate your code in the various places the function is called. This avoids the little overhead related to the function call, at the cost of bigger executables.

Be aware that inline keyword may be used also with namespaces, but with a very different meaning. Members of an inline namespace can be used in most respects as though they were members of the enclosing namespace. (see Standard, section 7.3.1 point 8).

Edit: The google style guide recommends to inline only when a function is ten lines or less.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • The compiler may perform the mentioned transformation with any code. Attaching that meaning to `inline` is meaningless. – Robert Allan Hennigan Leahy Aug 13 '14 at 12:39
  • @RobertAllanHenniganLeahy Yes, the optimizer may inline other code. However, it's not fair to put a downvote because the standard still considers inlining, just because your personal opinion is that it's useless... :-/ – Christophe Aug 13 '14 at 12:41
  • 1
    This answer does not address the question. The question is about when to use `inline`. You use it if a function needs to be declared multiple times in the same program, as when a function is defined in a header file. Sprinkling it around because you (who are unqualified to make such a judgment when compared to the compiler) think a function ought to be `inline`'d is just noise, and accomplishes next-to-nothing. I.e. it is not when `inline` ought to be used. – Robert Allan Hennigan Leahy Aug 13 '14 at 12:43
  • I don't agree: I've pointed out the time vs. space aspect of the question, which makes clear when it's interesting to use. Again, you are free to to have your personal opinion about what's noise and what isn't – Christophe Aug 13 '14 at 12:48
  • The time vs. space aspect is irrelevant, since `inline` does not compel the compiler to perform any transformation. Such consideration belongs in a discussion about inlining, but not `inline`. – Robert Allan Hennigan Leahy Aug 13 '14 at 12:54
0

I'm answering the question my self!: Solution: After a few performance tests, the rule of thumb from Stroustrup is right! inlining Short functions like the .size() from vector can improve the performance (.size() calls are used frequently). But the impact is only noticeable for FREQUENTLY used functions. If a getter/setter method is used a lot, inlining it might increase the performance.

Stroustrup:

Don’t make statements about “efficiency” of code without first doing time measurements. Guesses about performance are most unreliable.

jeromintus
  • 367
  • 1
  • 5
  • 14