What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today's optimized compilers, fast CPUs, huge memory etc. (not like in the 1980< where memory was scarce and everything had to fit in 100KB of memory) what advantages do they really have today?
Inline functions are faster because you don't need to push and pop things on/off the stack like parameters and the return address; however, it does make your binary slightly larger.
Does it make a significant difference? Not noticeably enough on modern hardware for most. But it can make a difference, which is enough for some people.
Marking something inline does not give you a guarantee that it will be inline. It's just a suggestion to the compiler. Sometimes it's not possible such as when you have a virtual function, or when there is recursion involved. And sometimes the compiler just chooses not to use it.
I could see a situation like this making a detectable difference:
inline int aplusb_pow2(int a, int b) {
return (a + b)*(a + b) ;
}
for(int a = 0; a < 900000; ++a)
for(int b = 0; b < 900000; ++b)
aplusb_pow2(a, b);
Advantages
- By inlining your code where it is needed, your program will spend less time in the function call and return parts. It is supposed to make your code go faster, even as it goes larger (see below). Inlining trivial accessors could be an example of effective inlining.
- By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)
Disadvantages
- It can make your code larger (i.e. if you use inline for non-trivial functions). As such, it could provoke paging and defeat optimizations from the compiler.
- It slightly breaks your encapsulation because it exposes the internal of your object processing (but then, every "private" member would, too). This means you must not use inlining in a PImpl pattern.
- It slightly breaks your encapsulation 2: C++ inlining is resolved at compile time. Which means that should you change the code of the inlined function, you would need to recompile all the code using it to be sure it will be updated (for the same reason, I avoid default values for function parameters)
- When used in a header, it makes your header file larger, and thus, will dilute interesting informations (like the list of a class methods) with code the user don't care about (this is the reason that I declare inlined functions inside a class, but will define it in an header after the class body, and never inside the class body).
Inlining Magic
- The compiler may or may not inline the functions you marked as inline; it may also decide to inline functions not marked as inline at compilation or linking time.
- Inline works like a copy/paste controlled by the compiler, which is quite different from a pre-processor macro: The macro will be forcibly inlined, will pollute all the namespaces and code, won't be easily debuggable, and will be done even if the compiler would have ruled it as inefficient.
- Every method of a class defined inside the body of the class itself is considered as "inlined" (even if the compiler can still decide to not inline it
- Virtual methods are not supposed to be inlinable. Still, sometimes, when the compiler can know for sure the type of the object (i.e. the object was declared and constructed inside the same function body), even a virtual function will be inlined because the compiler knows exactly the type of the object.
- Template methods/functions are not always inlined (their presence in an header will not make them automatically inline).
- The next step after "inline" is template metaprograming . I.e. By "inlining" your code at compile time, sometimes, the compiler can deduce the final result of a function... So a complex algorithm can sometimes be reduced to a kind of
return 42 ;
statement. This is for me extreme inlining. It happens rarely in real life, it makes compilation time longer, will not bloat your code, and will make your code faster. But like the grail, don't try to apply it everywhere because most processing cannot be resolved this way... Still, this is cool anyway...
:-p
3.2/6: There can be more than one definition of [..] inline function with external linkage [..] non-static function template
. At 5.1.5/6 For a generic lambda, the closure type has a public inline function call operator member template
. And at 7.1.2/2: the use of inline keyword is to declare an inline function
where it is a suggestion to inline the function body at point of call. So, I conclude that even if they can behave the same, inline functions and function templates are still separate, orthogonal notions that can be mixed (ie inline function template) –
Altruistic Inline functions are faster because you don't need to push and pop things on/off the stack like parameters and the return address; however, it does make your binary slightly larger.
Does it make a significant difference? Not noticeably enough on modern hardware for most. But it can make a difference, which is enough for some people.
Marking something inline does not give you a guarantee that it will be inline. It's just a suggestion to the compiler. Sometimes it's not possible such as when you have a virtual function, or when there is recursion involved. And sometimes the compiler just chooses not to use it.
I could see a situation like this making a detectable difference:
inline int aplusb_pow2(int a, int b) {
return (a + b)*(a + b) ;
}
for(int a = 0; a < 900000; ++a)
for(int b = 0; b < 900000; ++b)
aplusb_pow2(a, b);
In archaic C and C++, inline
is like register
: a suggestion (nothing more than a suggestion) to the compiler about a possible optimization.
In modern C++, inline
tells the linker that, if multiple definitions (not declarations) are found in different translation units, they are all the same, and the linker can freely keep one and discard all the other ones.
inline
is mandatory if a function (no matter how complex or "linear") is defined in a header file, to allow multiple sources to include it without getting a "multiple definition" error by the linker.
Member functions defined inside a class are "inline" by default, as are template functions (in contrast to global functions).
//fileA.h
inline void afunc()
{ std::cout << "this is afunc" << std::endl; }
//file1.cpp
#include "fileA.h"
void acall()
{ afunc(); }
//main.cpp
#include "fileA.h"
void acall();
int main()
{
afunc();
acall();
}
//output
this is afunc
this is afunc
Note the inclusion of fileA.h into two .cpp files, resulting in two instances of afunc()
.
The linker will discard one of them.
If no inline
is specified, the linker will complain.
Inlining is a suggestion to the compiler which it is free to ignore. It's ideal for small bits of code.
If your function is inlined, it's basically inserted in the code where the function call is made to it, rather than actually calling a separate function. This can assist with speed as you don't have to do the actual call.
It also assists CPUs with pipelining as they don't have to reload the pipeline with new instructions caused by a call.
The only disadvantage is possible increased binary size but, as long as the functions are small, this won't matter too much.
I tend to leave these sorts of decisions to the compilers nowadays (well, the smart ones anyway). The people who wrote them tend to have far more detailed knowledge of the underlying architectures.
gcc -flto -O3
), to allow inlining across C / C++ source files, without having to put functions in .h
headers. Otherwise it can only work within one compilation unit. –
Oceania Inline function is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.
It does not require function calling overhead.
It also save overhead of variables push/pop on the stack, while function calling.
It also save overhead of return call from a function.
It increases locality of reference by utilizing instruction cache.
After in-lining compiler can also apply intra-procedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..
To check more about it one can follow this link http://tajendrasengar.blogspot.com/2010/03/what-is-inline-function-in-cc.html
I'd like to add that inline functions are crucial when you are building shared library. Without marking function inline, it will be exported into the library in the binary form. It will be also present in the symbols table, if exported. On the other side, inlined functions are not exported, neither to the library binaries nor to the symbols table.
It may be critical when library is intended to be loaded at runtime. It may also hit binary-compatible-aware libraries. In such cases don't use inline.
inline
when building a shared library! –
Sommer static
functions, or ELF __attribute__((visibility("hidden")))
, are also not exported in the shared library. –
Oceania inline
allows you to place a function definition in a header file and #include
that header file in multiple source files without violating the one definition rule.
static
, and static
predates inline
and is more directly defined to mean "this is not visible outside of this translation unit". –
Averett static
makes a separate copy of the machine code for each compilation unit if the compiler chooses not to inline it at every call site in that compilation unit. With inline
, those duplicate definitions gets merged by the linker. So inline
is much better for functions that might not get inlined. (Smaller code footprint). –
Oceania inline
at source level, just a little more annotation+logic would let you do it for static
functions too). –
Averett static
the same as inline
in this regard, and what inline
really adds semantically, is that a static
function with the same name is allowed to have a different definition between translation units. So if you have two linked objects with inline
(but not static
) function named foo
, then as I understand it, the linker is allowed to just assume that they have the same definition... whereas with static
(and no inline
) the linker can't assume they're the same and must also compare the machine code or some additional metadata. –
Averett static
always had great semantics to enable all these optimizations. But yes, inline
's semantics do help make some of these optimizations simpler to implement. –
Averett inline
without static
, it is still extern
and the compiler is forced to generate a non-inlined copy of it for possible linking (unless it can somehow prove or be told that it won't be linked). Combining static
with inline
fixes that, but then I'm pretty sure the function is allowed to have a different definition between translation units, so the linker can't simply assume they're the same anymore, and so we're back to the same exact optimization properties as just static
without inline
. –
Averett inline
with neither static
nor extern
is not extern
, but it does have "external linkage" - and that inline definition of the function does not need to be generated non-inlined, but some external definition of that function must occur in another translation unit, and that external definition must be generated (and I said earlier that lets the linker assume they're the same, but actually the language doesn't guarantee the inline definition will be used instead of the external one, so the linker is free to delete any non-inlined copies besides the external one.) –
Averett static
, C++ inline
forces generation of a stand-alone definition (unlike C where you need to instantiate the function in exactly one .c
). Discarding those duplicate definitions can be done just by matching the name with C++ inline
, as you say, and hopefully dropping an un-referenced definition entirely, but that's still just matching symbol names. With static inline
, you need identical-code-folding optimization to realize that two separate functions (of the same name in separate compilation units) have the same machine code. ... –
Oceania gcc -flto
, and unfortunately not all projects build that way. (When I've looked at Linux distro packages in the past, they often don't.) And you need a toolchain willing to give those potentially-distinct functions the same address. I guess it's possible that linkers would support duplicate function merging like they do for strings placed in a special section, but they'd need special support for that. A relative call or jmp tailcall to a stand-alone definition in some .o
might not have a relocation entry, so removing it would break things. –
Oceania .text
section is less bad than multiple non-inline copies actually getting called, for I-cache footprint (although not iTLB if other code in the same page is hot), and BPU (same branch at diff addrs) –
Oceania .c
)" - yeah, that words it better! That's what I was trying to get at with "some external definition of that function must occur in another translation unit" (also in retrospect I would say "in a translation unit", not "in another"). Although I probably would say "externalize" or "make external" rather than "instantiate" - "instantiate" feels like a C++ concept, like with templates, while the relevant semantics/concepts in C are more about external visibility than whether or not an instance is created. –
Averett inline
also guarantees that a full definition will be visible in every compilation unit that uses it, IIRC. So that actually works better than C, with common uses not resulting in any stand-alone definitions to be discarded at link time. (Functions that are actually small and do get inlined, and without their addresses being taken, compiled with optimization enabled.) godbolt.org/z/Th8sbqrMM is -O0 –
Oceania inline
with static
in both of those godbolt examples produces the same result, which I'm more interested in since that's the main point I wanted to make with these comments - that in practice static
almost always ends up being just as good as inline
. –
Averett inline
(without either extern
nor static
) functions, are fully representative of what the compiler normally has to do when generating a whole program. I know that even in C, those examples you gave don't generate an external definition, but we would be close to or hitting undefined behavior unless something else did, because we have only an inline definition and no external definition - but I don't yet know C++ standard enough to either agree or disagree with your conclusion.) –
Averett static inline
in C++ gets GCC -O0
to reserve less stack space, not aligning the stack by 16 before a call because it still does IPA (inter-procedural analysis) to know that the callee doesn't care about alignment. –
Oceania During optimization many compilers will inline functions even if you didn't mark them. You generally only need to mark functions as inline if you know something the compiler doesn't, as it can usually make the correct decision itself.
inline
in the source to be a big deal. And if a basic "optimize this well" option doesn't tell the compiler to inline functions where best, it's just not a good compiler (by modern standards anyway). –
Averett It is not all about performance. Both C++ and C are used for embedded programming, sitting on top of hardware. If you would, for example, write an interrupt handler, you need to make sure that the code can be executed at once, without additional registers and/or memory pages being being swapped. That is when inline comes in handy. Good compilers do some "inlining" themselves when speed is needed, but "inline" compels them.
Generally speaking, these days with any modern compiler worrying about inlining anything is pretty much a waste of time. The compiler should actually optimize all of these considerations for you through its own analysis of the code and your specification of the optimization flags passed to the compiler. If you care about speed, tell the compiler to optimize for speed. If you care about space, tell the compiler to optimize for space. As another answer alluded to, a decent compiler will even inline automatically if it really makes sense.
Also, as others have stated, using inline does not guarantee inline of anything. If you want to guarantee it, you will have to define a macro instead of an inline function to do it.
When to inline and/or define a macro to force inclusion? - Only when you have a demonstrated and necessary proven increase in speed for a critical section of code that is known to have an affect on the overall performance of the application.
gcc -O3 -flto
) if you want the compiler to be able to inline small functions across .cpp
files, not just within one compilation unit. Otherwise you do need to put small hot functions in .h
files (like trivial accessors), and therefore use inline
. –
Oceania Why not make all functions inline by default? Because it's an engineering trade off. There are at least two types of "optimization": speeding up the program and reducing the size (memory footprint) of the program. Inlining generally speeds things up. It gets rid of the function call overhead, avoiding pushing then pulling parameters from the stack. However, it also makes the memory footprint of the program bigger, because every function call must now be replaced with the full code of the function. To make things even more complicated, remember that the CPU stores frequently used chunks of memory in a cache on the CPU for ultra-rapid access. If you make the program's memory image big enough, your program won't be able to use the cache efficiently, and in the worst case inlining could actually slow your program down. To some extent the compiler can calculate what the trade offs are, and may be able to make better decisions than you can, just looking at the source code.
Fell into the same trouble with inlining functions into so libraries. It seems that inlined functions are not compiled into the library. as a result the linker puts out a "undefined reference" error, if a executable wants to use the inlined function of the library. (happened to me compiling Qt source with gcc 4.5.
Our computer science professor urged us to never use inline in a c++ program. When asked why, he kindly explained to us that modern compilers should detect when to use inline automatically.
So yes, the inline can be an optimization technique to be used wherever possible, but apparently this is something that is already done for you whenever it's possible to inline a function anyways.
inline
in C++ has two very distinct meanings — only one of those is related to optimisation, and your professor is correct with regards to that. However, the second meaning of inline
is often necessary to satisfy the One Definition Rule. –
Pinnatifid Conclusion from another discussion here:
Are there any drawbacks with inline functions?
Apparently, There is nothing wrong with using inline functions.
But it is worth noting the following points!
Overuse of inlining can actually make programs slower. Depending on a function's size, inlining it can cause the code size to increase or decrease. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache. - Google Guidelines
The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost - Source
There are few situations where an inline function may not work:
- For a function returning values; if a return statement exists.
- For a function not returning any values; if a loop, switch or goto statement exists.
- If a function is recursive. -Source
The
__inline
keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not__inline
is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run. If you specify optimize , you must also specify the noinline option if you want the__inline
keyword to be ignored. -Source
inline
keyword for determining whether or not to inline code all the points made above are wrong. They would be true if the compiler used the keyword for inlining (it is only used for determining marking multiple definitions for linking purposes). –
Polity return
statement is only an obstacle to manually copy/pasting the function body into another function! Not for the compiler inlining. The link supporting to weird claims is dead. A function returning a value must have a return
statement in C++; if execution reaches the end of a function without a return
, that's undefined behaviour. So the only way to call a non-void function without a return statement would be if the function actually doesn't return, e.g. calling a noreturn function. Some of the other sections on this answer are generally true. –
Oceania © 2022 - 2024 — McMap. All rights reserved.
inline
is a c++ keyword and that inlining is a compiler optimization technique. See this question "when should I write the keywordinline
for a function/method" for the correct answer. – Aether