Why should i do something like this:
inline double square (double x) { return x*x;}
instead of
double square (double x) { return x*x;}
Is there a difference?
The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.
Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.
On a modern compiler there is likely not much difference. It may be inlined without the inline and it may not be inlined with the inline.
Yes there is a difference. https://isocpp.org/wiki/faq/inline-functions.
When you specify that a function is inline you are causing the compiler to put the code of the method in where ever it is being called.
void myfunc() {
square(2);
}
is identical to
void myfunc() {
2 * 2;
}
Calling a function is good for code clarity, but when that function is called the local state has to be pushed to the stack, a new local state is setup for the method, and when it is done the previous state needs to be popped. That is a lot of overhead.
Now if you up your optimization level, the compiler will make decisions like unrolling loops or inlining functions. The compiler is still free to ignore the inline statement.
From Wikipedia: Inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.
http://en.wikipedia.org/wiki/Inline_function
inline works well with the concept of procedural abstraction:
inline double square (double x) { return x*x;}
int squareTwice(double x) {
double first = square(x);
double second = square(x);
return first * second;
}
The above is fundamentally similar to the following:
int squareTwice(double x) {
double first = x*x;
double second = x*x;
return first * second;
}
This happens because when the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream; therefore, it may be easier to procedurally abstract the second example to the first example.
Procedural abstraction makes it possible to break up a routine into smaller subroutines that are much easier to read (although this can be a style choice).
The inline function, if the compiler complies, will include the inline function in the code in which it was called as if no function was called (as though you had put the logic in the calling function) and avoid the function call overhead.
Related
This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 8 years ago.
I was searching the internet a few topics related to inline functions, but none of them took my doubts.
So far I know the inline function works the same way that a method or a block, but recently saw the following response:
By declaring a function inline you tell the compiler to replace the
complete code of that function directly into the place from where it
was called. This is a rather advanced feature that requires
understanding of lower-level programming.
So that means that in normal functions: I was at a point A and wanted to go to point B for that I get out of A and go to B, right?
Have inline functions works in this way: I was at point A and would like to go to point B for that point B comes up to point A?
What I said above is correct?
Ideally, with the inline keyword, the compiler pastes the contents of the inlined function at the point where the function is called.
Given:
void Print(void)
{
cout << "Hello World!\n";
}
int main(void)
{
Print();
return 0;
}
The compiler would emit an assembly instruction to call the Print function inside the main function.
When declaring the Print function as inline, the compiler would generate the conceptual main() function below:
int main(void)
{
// Substitute content of Print function because it's declared as inline
cout << "Hello World!\n";
return 0;
}
Remember that the inline keyword is a suggestion to the compiler and the compiler can ignore it. The compiler may already inline small functions without using the inline keyword.
Long ago, the inline keyword was used to force compilers to paste code where the function was invoked. This technique was used to eliminate function call overhead. This was in the times when compilers were less intelligent about optimizing.
The quote should read "replace the call" ... "place the complete code". The ideal behind an inline function is similar to a macro; in every place in the source code that calls the inline function, instead of calling the inline function, the call is replaced with the complete code from the inline function, and no call is made. This could generate a lot of duplicate code, but should result in faster execution, unless the increase in code size causes issues with cache. In some cases, a compiler may ignore the inline option and use a normal call.
Inline functions is a good tool when you want to save running time because when you call this kind of a function(inline) its code is then written in the call's place. Why does it save time? Because calling a regular function simply consumes more time than just pasting its functionality. Example:
inline void sayHi()
{
std::cout<<"Hi!";
}
int main()
{
sayHi(); //Calling an inline function
std::cin.get();
}
compiler pastes std::cout<<"Hi!"; instead of sayHi();
So what you said is actually correct.
well last time I checked an inline function is a function whose body is substituted directly in every point in the program where the function is called.
So when I do this :
#include <iostream>
inline void increment(int n) { n = n + 1; }`
int main() {
int n = 0;
increment(n);
std::cout << "Result " << n;
}
I should have : Result 1.
Instead, I get 0.
So how does an inline function work ?
'inline' doesn't replace the text with the function body the way a macro does. It replaces the function call with the EQUIVALENT generated code, so that functionaly it's no different than if it weren't inline.
This is not a problem with it being inline but with the method signature being wrong:
inline void increment(int& n) {
++n;
}
You're asking for a copy, you're getting one. Instead ask for a reference. Don't confuse inline functions with macros, they are not the same. In fact, declaring things inline is usually counter-productive as the compiler will make this call for you depending on your optimization settings.
There are two things you should consider while inlining.
1) Inline is just a request to compiler rather than command to replace the function call with its body so as to avoid overhead related to function calls.
2) You should always inline the functions which are pretty small like getters/setters. Because inlining large functions OR recursive functions lead to code bloat which defeats the purpose of inlining.
Also inline functions have static linkage.
Let us say I have a function in my program and somewhere in my code, that function is called through a function pointer. What happens if the compiler happened to inline that function, or would the compiler realize that there is a function pointer assigned to that function and therefore avoid inlining it.
When a pointer to a function is taken, the compiler will generate an out-of-line body for the function. It is still possible to inline the function at other call sites.
Note that a function marked inline must have a definition available in all TUs which refer to it, and these definitions must be identical. Which means it's perfectly safe to inline the function at some call sites and keep it out-of-line at others.
Well, it will surely work. I don't see how inlining would prevent that. You just have some code that calls the function directly, and it might be inlined there, and you have some code which calls it through a function pointer, just as a regular function.
There's no reason that using a function pointer should prevent inlining. Inlining is done on a case-by-case basis and can exist alongside a usual function body. So a function can be inlined in one place, and called in another.
The compiler will, therefore, inline where it can and still produce a callable function for your function pointer.
Not only does the compiler inline "other calls of the function", but it may even inline calls through function pointers if it understands enough about which function is actually being used, something like this:
typedef void (*funcptr)();
void somefunc()
{
... do stuff here ...
}
void indirection(funcptr *f)
{
f();
}
void call_with_ptr()
{
funcptr f = somefunc();
for(int i = 0; i < 100; i++)
{
indirection(f);
}
}
I had code similar to this, and it inlined the indirection, and made the call to somefunc() a direct call without using the function pointer.
But of course, this assumes the compiler can figure out which function is called from the code - which is obvious in this case, but if there is runtime decisions involved, it may not do so.
Why should i do something like this:
inline double square (double x) { return x*x;}
instead of
double square (double x) { return x*x;}
Is there a difference?
The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.
Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.
On a modern compiler there is likely not much difference. It may be inlined without the inline and it may not be inlined with the inline.
Yes there is a difference. https://isocpp.org/wiki/faq/inline-functions.
When you specify that a function is inline you are causing the compiler to put the code of the method in where ever it is being called.
void myfunc() {
square(2);
}
is identical to
void myfunc() {
2 * 2;
}
Calling a function is good for code clarity, but when that function is called the local state has to be pushed to the stack, a new local state is setup for the method, and when it is done the previous state needs to be popped. That is a lot of overhead.
Now if you up your optimization level, the compiler will make decisions like unrolling loops or inlining functions. The compiler is still free to ignore the inline statement.
From Wikipedia: Inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.
http://en.wikipedia.org/wiki/Inline_function
inline works well with the concept of procedural abstraction:
inline double square (double x) { return x*x;}
int squareTwice(double x) {
double first = square(x);
double second = square(x);
return first * second;
}
The above is fundamentally similar to the following:
int squareTwice(double x) {
double first = x*x;
double second = x*x;
return first * second;
}
This happens because when the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream; therefore, it may be easier to procedurally abstract the second example to the first example.
Procedural abstraction makes it possible to break up a routine into smaller subroutines that are much easier to read (although this can be a style choice).
The inline function, if the compiler complies, will include the inline function in the code in which it was called as if no function was called (as though you had put the logic in the calling function) and avoid the function call overhead.
I need to make the compiler to not inline an inlined function.
eg:
I have an inline function A.
I have a function B that calls A.
In B, A is inlined and this is perfect.
Now I have a function C that calls A many times.
In C, A is inlined, and it is not good.
Is it possible to tell the compiler to not inline A when it is called from C ?
--edit--
The first Idea is to create the function __declspec(noinline) A1 (that simply calls A) and call A1 instead of A in C.
But I wondering if there is a more elegant solution ?
note
I know that inline is only a suggestion, but in my program, I have some unlikely or error cases where the compiler inline functions but should not because in these cases I prefer function calls to reduce code size. I also noticed that the compiler is not always able to make the best choice (in the point of view of the developer)
In general, you cannot tell your compiler to inline or not inline a function. This is an internal optimization and even if you declare a function inline, the compiler may chose to not do so.
Some compilers allow you to control inlining to some extent. For instance, GCC has a function attribute noinline that prevents it from being inlined.
In your case, I'd try something like this:
inline void a() { ... }
void __attribute__((noinline)) wrap_a()
{ a(); }
void b() { a(); }
void c() { wrap_a(); }
Inlining is only a suggestion to compiler -- it is quite possible that the function won't be pasted in the second case. I would just trust the compiler and leave it as is.
I have found the following solution:
template <class F> ALWAYS_INLINE F NOINLINE( F f ) {
return f;
}
It seems that the compiler (MSVC at least) don't inline functions called like this:
NOINLINE(my_inline_function)();
I think it is similar to the "calling it through a function pointer" solution from Nick D
The most straight forward solution is to put the function code into a separate file.