14

There is a nice explanation of using inline instruction on another question

Could anyone explain me if there is any difference using inline and __always_inline on a header file?

And, when I would prefer __always_inline over inline or vice-versa?

2
  • 1
    inline is standard and __always_inline looks like a compiler extension only for certain compilers. Btw inline is a suggestion to the compiler. There is no guarantee that it will be inlined. Commented Jan 8, 2014 at 12:13
  • It would be helpful to always specify compiler when asking about compiler specific keywords. Commented Jan 8, 2014 at 12:29

3 Answers 3

6

None of the answers here really answer the question of when you should use inline instead of always-inline, but the answer is fairly simple. You should pretty much always use inline unless you know exactly what you are doing and understand the performance implications of inlining the function you are annotating, because a bad inlining decision can in the worst case make a program much slower, and that includes both inlining when you shouldn't inline, and not inlining when you should inline. Generally speaking the compiler is quite intelligent at deciding this in the majority of cases, but it's not perfect; if it were, there would be no need for the inline keyword. It's true that the C language is quite esoteric about what precisely the inline keyword means, so you should read up before you use it, but it's better to leave ultimate say on whether to inline a function to the compiler as much as possible.

That being said, when you are optimizing critical parts of the code (and if you don't care about performance, why are you inlining things?), sometimes the compiler will make the wrong decision. In this case, you can correct the behavior with noinline or alwaysinline. But you should always familiarize yourself with the assembly of the functions in question before making this decision; too much inlining can increase code size and register pressure, and decrease cache locality. Too little can make code orders of magnitude slower.

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

1 Comment

Are there cases where it always makes sense to inline? For example: a macro that puts a float into a {float/unsigned} union and retrieves the unsigned from the union. Couldn't that be an inline function? Better to check the assembly anyway? On x86 it just does this: movd eax, xmm0
3

Always inline function attribute indicates that a function must be inlined. The compiler attempts to inline the function, regardless of the characteristics of the function.

However with inline attributes the compiler does not inline a function if doing so causes problems. For example, a recursive function is inlined into itself only once.

__forceinline is equivalent to __always_inline.

Comments

1

This article gives some useful information: https://www.kernel.org/doc/local/inline.html

"In Linux, the keyword "__always_inline" forces a function to be inlined, and "noinline" prevents a function from being inlined. We don't use the "inline" keyword because it's broken."

1 Comment

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.

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.