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?

7
  • 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. Commented Aug 24, 2022 at 19:23
  • 5
    Here is a small example: 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). Commented Aug 24, 2022 at 19:34
  • 2
    And function call can have side effects. It prevents many optimizations. Inlined function will not. godbolt.org/z/WoMKhnGcd Commented Aug 24, 2022 at 19:46
  • 1
    @EugeneSh. you comments could be turned into a regular answer. Commented Aug 24, 2022 at 20:21
  • 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. Commented Aug 25, 2022 at 5:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.