8

Is it worth it to use the inline keyword or the compiler is smart enough to know when he needs to inline a function ?

Suugaku
  • 2,667
  • 5
  • 31
  • 33
  • 4
    A remark not worth an answer: if you do not intend to use the function from outside the compilation unit it is in, the keyword "static" does more to help the compiler than "inline". The compiler can then for instance inline regardless of size if there is a single call site. – Pascal Cuoq Sep 06 '10 at 09:54

4 Answers4

15

Yes, it is smart enough. But what has made no progress at all in the past 40 years is the way C programs are built. It is still one source code file at a time.

So to get a function inlined in more then one .c file you put the function definition in the .h file. And if you don't mark them inline, the linker will complain about the multiple definitions.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2
    +1, `inline` nowadays means "allow multiple definitions". The fact that it's also a hint to the compiler to "make calling this function fast" is an unrelated and irrelevant side-effect :-) I think it's not that "the way programs are built" hasn't advanced - some compilers/linkers have. But the lowest common denominator, that the standard allows, hasn't advanced. – Steve Jessop Sep 06 '10 at 14:06
  • 11
    Saying `inline` means "allow multiple definitions" is simply wrong. This question is about the C language, not C++. C does not have `extern inline` functions, the `inline` keyword is invalid without `static`, and multiple definitions of a function are never allowed. – R.. GitHub STOP HELPING ICE Sep 06 '10 at 23:30
10

The compiler is pretty smart, and has multiple metrics to figure out if something is worth inlining. But sometimes however the developer will have knowledge about how the application is going to be run and will know to inline something that the compiler doesn't do automatically. However, I would never inline stuff manually unless I had a done some benchmarks and found that inline would improve my performance.

You can read more about how GCC uses inline.

bramp
  • 9,581
  • 5
  • 40
  • 46
3

Its always worth being explicit about intention.

Its also worth noting that the compiler doesn't even need to inline if it thinks its better not too.

Goz
  • 61,365
  • 24
  • 124
  • 204
0

The Standard says

7.1.2 Function specifiers

2. A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. 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; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

So I'd liken inline to maybe <br /> in HTML -- it is good practice to use the self-closing <br /> all the time. But one might argue that almost all browser implementations treat <br> and <br /> exactly the same, which IMO misses the point.

As others have noted, this is probably not very relevant for performance value in these days. But I think it still serves nicely as a semantic, intention conveying keyword.

kizzx2
  • 18,775
  • 14
  • 76
  • 83