2

Im reading What Every Programmer Should Know About Memory https://people.freebsd.org/~lstewart/articles/cpumemory.pdf and it says that inline functions make your code more optimizable

for example:
Inlining of functions, in particular, allows the compiler to optimize larger chunks of code at a time which, in turn, enables the generation of machine code which better exploits the processor’s pipeline architecture

and:
The handling of both code and data (through dead code elimination or value range propagation, and others) works better when larger parts of the program can be considered as a single unit.

and this also:
If a function is only called once it might as well be inlined. This gives the compiler the opportunity to perform more optimizations (like value range propagation, which might significantly improve the code).

After reading these, to me atleast it seems like inline functions are easier to optimize, but why? Why is it easier to optimize something is inline?

  • 11
    Because they are becoming inlined in the caller code, and the compiler can see the context it is being called in more clearly. This way such a function can be optimized in different ways in different contexts, unlike a "regular" function that has only one instance and needs to perform as is in any context it is being called from. – Eugene Sh. Aug 24 '22 at 19:23
  • I am not a professional C programmer, but I'm pretty sure that it's related to making code specific optimizations - For example, if you need to pass an address to a function which is then dereferenced and the address is discarded, it would force it to use memory instead of a register for the variable. But with inlining it can just use a register. I'm unsure to if this optimization actually exists but I think that it's an example. – Random Aug 24 '22 at 19:23
  • 5
    Here is a small example: https://godbolt.org/z/zv8bars1j - with inlined function the compiler can figure out that the decrement of the value incremented in a function is remaining unchanged. With not inlined it cannot (well, maybe it can, but can't do nothing with this conclusion, as the same function might be called elsewhere, where this observation does not hold). – Eugene Sh. Aug 24 '22 at 19:34
  • 2
    And function call can have side effects. It prevents many optimizations. Inlined function will not. https://godbolt.org/z/WoMKhnGcd – 0___________ Aug 24 '22 at 19:46
  • 1
    @EugeneSh. you comments could be turned into a regular answer. – yugr Aug 24 '22 at 20:21
  • Near duplicate of [Benefits of inline functions in C++?](https://stackoverflow.com/q/145838) but most of the answers there are not very good, like not specific or contain wrong parts. Or are about the `inline` keyword and its effect on the One Definition Rule. – Peter Cordes Aug 24 '22 at 21:40
  • 2
    I wouldn't say "easier". When inlining, the compiler has more data, and actually considering that data in some sense makes the process harder. I would say instead that inlining makes optimization *more effective*. – Nate Eldredge Aug 25 '22 at 05:22

2 Answers2

1

The reason that it is easier to make a better job when optimizing inlined functions than with outlined is that you know the complete context in which the function is called and can use this information to tune the generated code to match this exact context. This often allows more efficient code for the inlined copy of the function but also for the calling function. The caller and the callee can be optimized to fit each other in a way that is not possible for outlined functions.

Johan
  • 3,667
  • 6
  • 20
  • 25
0

There is no difference!

All functions are subject to being inlined by gcc in -O3 optimization mode, whether declared inline, static, neither or both.

see: https://stackoverflow.com/a/40783656/9925764

or here is the modifying the example of @Eugene Sh. without noinline option. https://godbolt.org/z/arPEf7rd4

  • 1
    That's not what the question was asking. It's asking why it helps when the compiler actually does inline, regardless of what keywords you used to encourage the compiler to do that or not. The question title can be misleading, but it's asking about the compiler's job, not the human's job in optimizing the source to compile more easily. Have a look at the comments under the question, and the other answer; they're all answering what the question body asked. – Peter Cordes Nov 10 '22 at 18:18
  • What you're pointing out is a separate point that's also useful to understand, that compilers can inline even if you don't use the `inline` keyword. Especially with link-time optimization to allow cross-file inlining; the `inline` keyword makes it easier to write functions that can be inlined into callers in different files without LTO. – Peter Cordes Nov 10 '22 at 18:22