What exactly happens to empty inline functions? - c++

I'm writing code (using GCC 4.7.2), where I'm excessively logging stuff during the testing phase in countless positions of the code. These loggings should disappear on the release binary.
I'm doing the logging via a function just like void log(std::string msg);. As these function calls are many and distributed via the whole code in many files, I had the idea to make it an inline function and just give it an empty body for the release binary.
No my question is: what does the compiler do with it? Does the binary just contain the rest of the code without the function, does it contain a nop, or anything else? Could I eliminate the logging code from the binary completely by emptying the inline logging function?
I'm not only interested in an answer to solve the problem but I'm also curious about the compiler's behaviour itself.

If you want different code between debug and release, then that's an ideal use case for the preprocessor:
#ifdef NDEBUG
#define log(ignored)
#endif
Then you're not leaving anything up to the compiler. You're guaranteed that only the debug version will have the extra calls. This is how assert works, too.
Note that this will also drop the parameter computation. For example, if you have log(get_msg()) then the macro method will drop the call to get_msg() as well. This is probably desirable, but you need to be aware of it.
As for inline, that's entirely up to the compiler. The inline keyword itself is only a hint, it does not obligate the compiler to do anything. The compiler performs its own optimization calculations on whether or not to inline a particular function (that includes inlining functions not marked inline). That typically means a sufficiently high optimization level (i.e. -O3), and that the body of the inline function is visible in that particular compilation unit. For example, if the compiler only sees a declaration but the (maybe empty) function body is in a different .cpp file, then it cannot inline. But yes, if the compiler determines that there are no side effects, it is free to make the whole function disappear.
But again, there's no reason to depend on that when the preprocessor offers such a clean and widely used solution.

You might or might not be left with a trivially empty function (if for example, the function's address is used to make a pointer, then the function needs to exist).
But all inlined call sites will turn into nothing. (And the compiler should choose to always inline direct calls to a function it can see is empty -- Adam's answer is correct about calls into other translation units making this difficult, but Whole Program Optimization can help even there)
Do note, however, that parameters to an inline function will still be evaluated. They might also get inlined and mostly eliminated, but side effects in parameters will occur. This is rather different from using a #define macro to eliminate the entire log(...) string from the source code. The macro gets rid of the parameter computations, too.

Related

Is there any point in using __attribute__((noinline)) in a source file (not header file)?

Some programmers at my company have started sprinkling __attribute__((noinline)) in methods all over our code, in both header and source files. We do not use whole-program optimization. And in the vast majority of the cases I'm seeing, these methods are not calling other methods in the same source file ("translation unit"); the only callers of these "noinline" methods are (in most cases) methods in other source files.
I want to make a point that these __attribute__((noinline)) attribute markers are not doing anything and only clutter our code, but before I do that I just want to verify that I'm right:
Is there any benefit to marking a method "noinline" if its definition (code body) is...
in a source (.c or .cpp) file
not called by any other methods in that source file
and whole program optimization is not being used
...?
I.e., in the absence of whole-program optimization, could a compiler+linker even inline a method defined as such (above bullet points) anyway?
Is there any benefit to marking a method "noinline" if its definition
(code body) is...
in a source (.c or .cpp) file
not called by any other methods in that source file
and whole program optimization is not being used
...?
The position of the attribute on the function / method definition instead of on a non-definition declaration is not necessarily relevant in the particular case of a noinline attribute, because in order for inlining to be performed, some form of the function's definition (which bears the attribute) must be available.
Certainly the function won't be inlined into any other functions in the same translation unit if no other functions in that unit call it in the first place, directly or indirectly.
The "whole program optimization is not being used" seems to be an attempt to stipulate that the function in question cannot be inlined into any other functions, either. In that case, the noinline attribute is, by assumption, unnecessary to prevent inlining of that function.
I.e., in the absence of whole-program optimization, could a
compiler+linker even inline a method defined as such (above bullet
points) anyway?
This seems to be a question of semantics. I would categorize any optimization that crosses translation-unit boundaries as a "whole-program optimization". In that sense, then, no, a build process that does not perform any whole-program optimization will not perform the specific whole-program optimization of inlining a function from one TU into a function from a different TU.
Since you have not specified any details of your compilation procedure, we cannot speak to whether it is really true in your particular case that whole program optimzation is not being used, in the sense described above.
However, whether the attribute is necessary to prevent inlining of functions in your current code base is not the only consideration relevant to the "Is there any benefit?" question. Consider:
Whether the attribute is necessary to prevent inlining or not, it is effective at communicating the intent that the function should not be inlined.
In the event that your analysis of the code is wrong, or the code changes so that it no longer applies, the noinline attributes will provide for the desired inlining-suppression behavior.
Both of these are reminiscent of the nature and (proper) usage of assertions. If an assertion in my program ever triggers then that means a situation has arisen that I believed could not happen and assumed would not happen. Are assertions then just wasteful code? No, certainly not. They serve a documentary purpose (often supplemented with explanatory code comments), an error detection purpose, and often a failsafe purpose. Similar can be said of use of noinline under the circumstances you describe.
Inlining can still happen within a source file. If a function is called after its definition, the compiler could inline it. So the attribute would prevent that.
Therefore, this attribute can still have useful effects in source files, to the extent that you need to turn off inlining.
Regardless of the merits (of using noinline or not), I would use macros rather than peppering __attribute__((noinline)) everywhere.
And, the compiler can disable inlining via the command line (see below).
In my code, I sometimes want a function [usually defined in a .h file] that will always be inlined:
static inline __attribute__((always_inline))
That's relatively verbose [and error prone], so I define:
#define inline_always static inline __attribute__((always_inline))
I'm not a fan of the compiler deciding the inlining of a function for me, so I disable it from the command line:
-fno-inline-small-functions -fno-inline-functions-called-once -fno-inline-functions
That might be a safer/better alternative than changing function prototypes.
There is some merit to not inlining functions that have not been explicitly marked as inline.
For debugging and breakpoints.
The compiler's heuristic fails to produce the best code:
It inlines something that provides little benefit from the inlining vs. the function call overhead.
The inlining seems good to the compiler but the calling function's code is sub-optimal because it causes additional register pressure requiring that the registers be spilled [to the stack] more often.
For debug, we can compile with -O0 to disable compiler inlining of functions.
But, sometimes, we need to be able to put a [gdb] breakpoint on a function in code that has been compiled with optimization.
If the function has been [auto] inlined, this won't work [too well].
So, either use -fno-inline et. al. or apply the macro to key functions as in:
#ifdef DEBUG_NOINLINE
#define noinline_debug __attribute__((noinline))
#else
#define noinline_debug /**/
#endif
Note that the Linux kernel uses such macros:
#define noinline __attribute__((noinline))
#define __always_inline inline __attribute__((__always_inline__))
This is useful in case a given compiler does not support a particular attribute:
#if __has_attribute(__nonstring__)
# define __nonstring __attribute__((__nonstring__))
#else
# define __nonstring
#endif

how to check an inline function was automatically converted to a regular function by the compiler?

I know in some cases, the explicitly defined inline function will be converted to the regular function with calling stacks by the compiler. By how do I know this is the case? (for my C++ code)
BTW, in what circumstances will the compiler transform an inline function to a regular function?
You run nm or otool on the executable, and if you see the name of the function, then it has been defined. That doesn't mean that it hasn't indeed been inlined at all (it's possible that the compiler inlines a function but also generates an independent function body because for example one assigns a function pointer to it). For that, you need to examine the actual generated assembly code.
The keyword inline does not mean that the function will be inlined (or not). It means allow for multiple definitions in different translation units of the same program. The compiler will not transform an inline into a non-inline, it will follow the rules set in the standard and (almost) orthogonally determine whether to inline or not your functions.
As of whether to determine if a function was inlined or not, the guaranteed way to do so is checking the generated object code (or assembly). Checking whether there is an out-of-line definition of the function won't help, as many compilers will generate that out-of-line definition even if the code was actually inlined.
The compiler will DEFINITELY produce a regular function if the inline function has it's address taken (that is, you are making a function pointer to it, or something similar). And of course, if the function is virtual, it also has to exist as a standalone function since a virtual function call is not always possible to inline.
Other than that, it's entirely up to the compiler - it uses all manner of heuristics, such as "How big is the function?", "how many times is it called?", "how much 'gain' is there from inlining it?". If a function is called many times, and is quite large, it is probably left as a standalone function. If the function is only called a few times, or it's tiny, then it's inlined [assuming the compiler actually CAN inline it, of course - as described above, it may not be able to]. Also, nearly all compilers need to "see" the source code of the function to inline the function, but both GCC/G++ and MSVC do have otions for "whole program optimisation" that is intended to overcome this issue.
In other words, you can only "know" by reading the resulting machine code - in some cases, there are also extended "warning" or "notes" messages that you can enable to tell the compiler to give you information about "I did not inline function Func1 because ...insert some reason here...".

Inline function [duplicate]

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?
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
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.
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.
Advantages :-
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.
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 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.
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.
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.
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.
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.
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.
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

When should I use inline functions? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When should I write the keyword 'inline' for a function/method?
I have a question about inline functions in c++.
I know inline functions are used to replace each apperance with the inline function body.
But I do not know when to use it. It is said that inline functions can improve performance, but when should I consider using an inline function?
Inline functions are best for small stubs of code that are performance-critical and reused everywhere, but mundane to write.
However, you could also use them to split up your code, so rather than having a 1000-line long function, you may end up splitting this up into a couple of 100-line long methods. You could inline these so it would have the same effect as a 1000-line long method.
Modern compilers will automatically inline some of your smaller methods, however you can always manually inline ones that are bigger.
There are many resources about this, for example:
http://msdn.microsoft.com/en-us/library/1w2887zk(v=vs.80).aspx
http://www.cplusplus.com/forum/articles/20600/
http://www.glenmccl.com/bett_007.htm
http://www.exforsys.com/tutorials/c-plus-plus/inline-functions.html
Usually modern compilers are intelligent enough to inline certain small functions to a limit (in increment of space). You can provide the hint. Of course, you'll inline small, repetitive functions that save the overhead of the call.
Note also, that sometimes, even when you provide the inline keyword, the compiler can decide not to inline that function, so its use (for optimization purposes, at least) is somewhat informative.
Most of the modern compilers are smart enough to apply optimization of inlining functions, Probably best to let the compiler decide it.
inline is just a suggestion and the compiler is free to reject it or apply it.
You might consider making a function inline if:
A function is small
You can use inline function instead of a preprocssor directive #define
The purpose of the inline keyword is to allow you to define the same function in multiple compilation units (usually by defining it in a header file included from multiple source files). On some compilers, this is essential if you want the compiler to be able to consider it for inlining in more than one compilation unit.
So, if you define a function in a header file, then you should always declare it inline, otherwise you'll get link errors if the file is included from more than one source file. (If it's a class member function, you could define it inside the class instead; that implicitly declares it inline). If you define it in a source file, then you can declare it inline if you think it's a good candidate for inlining (e.g. if it's small, or only called from one place) if you like.
However, compilers generally think they have a better idea which functions than you do; some may take an inline declaration as a hint, and others may completely ignore it. Some may provide non-standard extensions to force functions to be (or not to be) inlined; only use those if you're sure (by measurement) that it will give an improvement over the compiler's decision, as unwise inlining can hurt performance.
You should use an inline function when you think that repeated calls to the function would take more time than simply placing the body of the function within the main code (in other words, when the body of the function is small and the function gets called repeatedly). However, compilers generally optimize code and may ignore functions that have been defined as inline. There are many other threads on SO that deal with this in greater detail.
Everything said before looks correct, I just want to warn you about a common mistake since you speak about performance.
Some programmers incorrectly think that "inline" = faster because the overhead of the function call is saved. Yes, the overhead is saved, but if your inline function compile into a bigger code than a function call (which happen very quickly), the whole code will get bigger. It then increase the probability that your code will not fit in the processor cache. And cache is everything today... So your "inlined" code will actually run more slowly...
I would say the use of "inline" is only OK for trivial getter/setter functions written directly in the .h.
For everything else, I would advise not to use "inline" and let the compiler decide itself.
And a general advice : apart from the general conception, you should not think about optimisation until everything run and you can make measurements of which operations takes the process time. It usually is less than 20% of the code, so you don't waste your time blindly optimizing everything else. And with measurements, you can immediately see if an optimisation (for example adding some inline here and there) actually works or not.

Why should I ever use inline code?

I'm a C/C++ developer, and here are a couple of questions that always baffled me.
Is there a big difference between "regular" code and inline code?
Which is the main difference?
Is inline code simply a "form" of macros?
What kind of tradeoff must be done when choosing to inline your code?
Thanks
Performance
As has been suggested in previous answers, use of the inline keyword can make code faster by inlining function calls, often at the expense of increased executables. “Inlining function calls” just means substituting the call to the target function with the actual code of the function, after filling in the arguments accordingly.
However, modern compilers are very good at inlining function calls automatically without any prompt from the user when set to high optimisation. Actually, compilers are usually better at determining what calls to inline for speed gain than humans are.
Declaring functions inline explicitly for the sake of performance gain is (almost?) always unnecessary!
Additionally, compilers can and will ignore the inline request if it suits them. Compilers will do this if a call to the function is impossible to inline (i.e. using nontrivial recursion or function pointers) but also if the function is simply too large for a meaningful performance gain.
One Definition Rule
However, declaring an inline function using the inline keyword has other effects, and may actually be necessary to satisfy the One Definition Rule (ODR): This rule in the C++ standard states that a given symbol may be declared multiple times but may only be defined once. If the link editor (= linker) encounters several identical symbol definitions, it will generate an error.
One solution to this problem is to make sure that a compilation unit doesn't export a given symbol by giving it internal linkage by declaring it static.
However, it's often better to mark a function inline instead. This tells the linker to merge all definitions of this function across compilation units into one definition, with one address, and shared function-static variables.
As an example, consider the following program:
// header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
#include <cmath>
#include <numeric>
#include <vector>
using vec = std::vector<double>;
/*inline*/ double mean(vec const& sample) {
return std::accumulate(begin(sample), end(sample), 0.0) / sample.size();
}
#endif // !defined(HEADER_HPP)
// test.cpp
#include "header.hpp"
#include <iostream>
#include <iomanip>
void print_mean(vec const& sample) {
std::cout << "Sample with x̂ = " << mean(sample) << '\n';
}
// main.cpp
#include "header.hpp"
void print_mean(vec const&); // Forward declaration.
int main() {
vec x{4, 3, 5, 4, 5, 5, 6, 3, 8, 6, 8, 3, 1, 7};
print_mean(x);
}
Note that both .cpp files include the header file and thus the function definition of mean. Although the file is saved with include guards against double inclusion, this will result in two definitions of the same function, albeit in different compilation units.
Now, if you try to link those two compilation units — for example using the following command:
⟩⟩⟩ g++ -std=c++11 -pedantic main.cpp test.cpp
you'll get an error saying “duplicate symbol __Z4meanRKNSt3__16vectorIdNS_9allocatorIdEEEE” (which is the mangled name of our function mean).
If, however, you uncomment the inline modifier in front of the function definition, the code compiles and links correctly.
Function templates are a special case: they are always inline, regardless of whether they were declared that way. This doesn’t mean that the compiler will inline calls to them, but they won’t violate ODR. The same is true for member functions that are defined inside a class or struct.
Is there a big difference between "regular" code and inline code?
Yes and no. No, because an inline function or method has exactly the same characteristics as a regular one, most important one being that they are both type safe. And yes, because the assembly code generated by the compiler will be different; with a regular function, each call will be translated into several steps: pushing parameters on the stack, making the jump to the function, popping the parameters, etc, whereas a call to an inline function will be replaced by its actual code, like a macro.
Is inline code simply a "form" of macros?
No! A macro is simple text replacement, which can lead to severe errors. Consider the following code:
#define unsafe(i) ( (i) >= 0 ? (i) : -(i) )
[...]
unsafe(x++); // x is incremented twice!
unsafe(f()); // f() is called twice!
[...]
Using an inline function, you're sure that parameters will be evaluated before the function is actually performed. They will also be type checked, and eventually converted to match the formal parameters types.
What kind of tradeoff must be done when choosing to inline your code?
Normally, program execution should be faster when using inline functions, but with a bigger binary code. For more information, you should read GoTW#33.
Inline code works like macros in essence but it is actual real code, which can be optimized. Very small functions are often good for inlining because the work needed to set up the function call (load the parameters into the proper registers) is costly compared to the small amount of actual work the method does. With inlining, there is no need to set up the function call, because the code is directly "pasted into" any method that uses it.
Inlining increases code size, which is its primary drawback. If the code is so big that it cannot fit into the CPU cache, you can get major slowdowns. You only need to worry about this in rare cases, since it is not likely you are using a method in so many places the increased code would cause issues.
In summary, inlining is ideal for speeding up small methods that are called many times but not in too many places (100 places is still fine, though - you need to go into quite extreme examples to get any significant code bloat).
Edit: as others have pointed out, inlining is only a suggestion to the compiler. It can freely ignore you if it thinks you are making stupid requests like inlining a huge 25-line method.
Is there a big difference between "regular" code and inline code?
Yes - inline code does not involve a function call, and saving register variables to the stack. It uses program space each time it is 'called'. So overall it takes less time to execute because there's no branching in the processor and saving of state, clearing of caches, etc.
Is inline code simply a "form" of macros?
Macros and inline code share similarities. the big difference is that the inline code is specifically formatted as a function so the compiler, and future maintainers, have more options. Specifically it can easily be turned into a function if you tell the compiler to optimize for code space, or a future maintainer ends up expanding it and using it in many places in their code.
What kind of tradeoff must be done when choosing to inline your code?
Macro: high code space usage, fast execution, hard to maintain if the 'function' is long
Function: low code space usage, slower to execute, easy to maintain
Inline function: high code space usage, fast execution, easy to maintain
It should be noted that the register saving and jumping to the function does take up code space, so for very small functions an inline can take up less space than a function.
-Adam
It depends on the compiler...
Say you have a dumb compiler. By indicating a function must be inlined, it will put a copy of the content of the function on each occurrence were it is called.
Advantage: no function call overhead (putting parameters, pushing the current PC, jumping to the function, etc.). Can be important in the central part of a big loop, for example.
Inconvenience: inflates the generated binary.
Is it a macro? Not really, because the compiler still checks the type of parameters, etc.
What about smart compilers? They can ignore the inline directive, if they "feel" the function is too complex/too big. And perhaps they can automatically inline some trivial functions, like simple getters/setters.
Inline differs from macros in that it's a hint to the compiler (compiler may decide not to inline the code!) and macros are source code text generation before the compilation and as such are "forced" to be inlined.
Marking a function inline means that the compiler has the option to include in "in-line" where it is called, if the compiler chooses to do so; by contrast, a macro will always be expanded in-place. An inlined function will have appropriate debug symbols set up to allow a symbolic debugger to track the source where it came from, while debugging macros is confusing. Inline functions need to be valid functions, while macros... well, don't.
Deciding to declare a function inline is largely a space tradeoff -- your program will be larger if the compiler decides to inline it (particularly if it isn't also static, in which case at least one non-inlined copy is required for use by any external objects); indeed, if the function is large, this could result in a drop in performance as less of your code fits in cache. The general performance boost, however, is just that you're getting rid of the overhead of the function call itself; for a small function called as part of an inner loop, that's a tradeoff that makes sense.
If you trust your compiler, mark small functions used in inner loops inline liberally; the compiler will be responsible for Doing The Right Thing in deciding whether or not to inline.
If you are marking your code as inline in f.e. C++ you are also telling your compiler that the code should be executed inline, ie. that code block will "more or less" be inserted where it is called (thus removing the pushing, popping and jumping on the stack). So, yes... it is recommended if the functions are suitable for that kind of behavior.
"inline" is like the 2000's equivalent of "register". Don't bother, the compiler can do a better job of deciding what to optimize than you can.
By inlining, the compiler inserts the implementation of the function, at the calling point.
What you are doing with this is removing the function call overhead.
However, there is no guarantee that your all candidates for inlining will actually be inlined by the compiler. However, for smaller functions, compilers always inline.
So if you have a function that is called many times but only has a limited amount of code - a couple of lines - you could benefit from inlining, because the function call overhead might take longer than the execution of the function itself.
A classic example of a good candidate for inlining are getters for simple concrete classes.
CPoint
{
public:
inline int x() const { return m_x ; }
inline int y() const { return m_y ; }
private:
int m_x ;
int m_y ;
};
Some compilers ( e.g. VC2005 ) have an option for aggressive inlining, and you wouldn't need to specify the 'inline' keyword when using that option.
I won't reiterate the above, but it's worth noting that virtual functions will not be inlined as the function called is resolved at runtime.
Inlining usually is enabled at level 3 of optimization (-O3 in case of GCC). It can be a significant speed improvement in some cases (when it is possible).
Explicit inlining in your programs can add some speed improvement with the cost of an incresed code size.
You should see which is suitable: code size or speed and decide wether you should include it in your programs.
You can just turn on level 3 of optimization and forget about it, letting the compiler do his job.
The answer of should you inline comes down to speed.
If you're in a tight loop calling a function, and it's not a super huge function, but one where a lot of the time is wasted in CALLING the function, then make that function inline and you'll get a lot of bang for your buck.
First of all inline is a request to compiler to inline the function .so it is upto compiler to make it inline or not.
When to use?When ever a function is
of very few lines(for all accessors
and mutator) but not for recursive
functions
Advantage?Time taken for invoking the function call is not involved
Is compiler inline any function of its own?yes when ever a function is defined in header file inside a class
inlining is a technique to increase speed. But use a profiler to test this in your situation. I have found (MSVC) that inlining does not always deliver and certainly not in any spectacular way. Runtimes sometimes decreased by a few percent but in slightly different circumstances increased by a few percent.
If the code is running slowly, get out your profiler to find troublespots and work on those.
I have stopped adding inline functions to header files, it increases coupling but gives little in return.
Inline code is faster. There is no need to perform a function call (every function call costs some time). Disadvantage is you cannot pass a pointer to an inline function around, as the function does not really exist as function and thus has no pointer. Also the function cannot be exported to public (e.g. an inline function in a library is not available within binaries linking against the library). Another one is that the code section in your binary will grow, if you call the function from various places (as each time a copy of the function is generated instead of having just one copy and always jumping there)
Usually you don't have to manually decide if a function shall be inlined or not. E.g. GCC will decide that automatically depending on optimizing level (-Ox) and depending on other parameters. It will take things into consideration like "How big is the function?" (number of instructions), how often is it called within the code, how much the binary will get bigger by inlining it, and some other metrics. E.g. if a function is static (thus not exported anyway) and only called once within your code and you never use a pointer to the function, chances are good that GCC will decide to inline it automatically, as it will have no negative impact (the binary won't get bigger by inlining it only once).