Difference between __always_inline and inline
Asked Answered
U

3

13

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?

Understructure answered 8/1, 2014 at 12:11 Comment(2)
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.Nonpayment
It would be helpful to always specify compiler when asking about compiler specific keywords.Raynold
L
5

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.

Leralerch answered 30/4, 2020 at 22:52 Comment(0)
H
2

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.

Hershelhershell answered 13/1, 2019 at 16:50 Comment(0)
W
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."

Willams answered 17/9, 2019 at 12:8 Comment(1)
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.Dhyana

© 2022 - 2024 — McMap. All rights reserved.