Isn't inline premature optimization? [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Inline functions in C++
Modern compilers are better than programmers at deciding what should be inlined and what should not. Just like, register, shouldn't inlining functions be a job for the compiler only, and be considered premature optimization ?

inline has a double meaning that some are unaware of - it allows a function to be defined in more than one translation unit (i.e. if you define an unbound function in a header and include it from within various translation units, you are forced to declare it inline or the linker would complain about doubly defined symbols).
The second meaning is a hint to the compiler that this function may profit from inlining its machine code at the caller site. You're right, modern compilers/optimizers should be able to figure this out on his own.
My advice is to use inline only when it is needed (first case) and never to pass an optimization hint to the compiler. This way, this crazy double meaning is resolved in your source code.

inline is only tangentially related to optimization.
You should choose to apply inline to a function if you need the exceptions to the one definition rule that it gives you, and leave it out if you don't. Most of the time you can rely on the compiler to perform the appropriate optimizations independent of whether a function is declared inline or not.

Check out this answer: Inlining this function or not?
Essentially, yes, inlining is a task of the compiler at this point. Inlining was initially created to indicate to the compiler that it should try. The keyword is indicate - The compiler has the choice as to whether or not it inlines the function or not.

Related

Why does the `inline` keyword exists in C++? [duplicate]

This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 2 years ago.
I'm learning c++ via this page.
In the page above and this page, it says that a modern compiler is smart enough to decide which side will have the better performance regarding the inlining of the function. The compiler is smarter than a human in most cases.
So, I thought that we could use the inline keyword if we want to force the compiler to treat the function as inline, but that was not the case. It says that even if the function is declared as inline, the compiler is free to (and usually does) ignore the keyword.
Does this mean that the function's inline-ness is not decided by the inline keyword, but rather the compiler has the ultimate privilege to decide it? If so, then isn't it useless for a programmer to explicitly declare a function as inline?
On the pretext on when it was defined, it seemed to be a good optimization, however it doesn't hold any value now since C++17.
What it implies now is that it can have multiple identical definitions, and needs to be defined in every translation unit that uses it.
Does it mean that the function's inline-ness is not decided by the inline keyword, but the compiler has the entire privilege to decide it?
Yes. It doesn't depend on whether you use inline or not as well. Compilers can self-identify whether to inline a function or not.
If so, then isn't it useless for a programmer to explicitly declare function as inline?
Refer to its usage mentioned above for inline functions.
Pertaining to the question title, the inline keyword is applicable to inline variables (C++ 17) as well, which allows such variables to be defined in multiple translation units, following the one definition rule. If defined more than once, the compiler merges them all into a single object in the final program.
The inline keyword has nothing to do with inlining calls to a function. All it does is allow the function (or object, as of C++17) to be defined in multiple compilation units. That is, it allows a function to be defined inline in a header where it's declared.
This may assist the compiler in being able to inline calls to a function, since the full definition of a function defined in a header is visible in multiple translation units. The inline keyword is not a requirement, or even a hint, that the compiler should inline the calls. The compiler will make that determination on its own.
There is no standard way to force the compiler to inline calls to a function, but most compilers provide an extension to do so. For example, GCC has the always_inline attribute.

When can i not inline a function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to use inline function and when not to use it?
Under what conditions can (not should) a function not be inlined (C++ Only) ?
Two conditions that i know of are :
1 . If the function has a recursive call
2 . If there are static variables in the function
inline is a keyword of C++, but inlining is a generic process performed by a compiler backend, usually after instruction sequences are already generated.
A C compiler will also inline functions, and a C++ compiler will inline functions that aren't inline. A C++ compiler can also fail to inline an inline function for any arbitrary reason. The keyword actually exists to specify that a function may have multiple, identical definitions in different translation units (source files).
Static variables have no special bearing on whether something can be inlined. Perhaps some compilers have difficulty linking the resulting structure of global variable references, but that's more of a bug than a rule of thumb.
Recursive functions can be inlined, too. The recursive call should be translated to a branch. The branch could then be targeted by loop unrolling.
A function that compiles to more than a kilobyte of code will usually not be inlined. But a compiler may provide #pragma directives or platform-specific attributes to force inlining in such a case.
The biggest factor that would stop a function from being inlined is if its source isn't available to the compiler at the time of code generation. Link-time optimization opens the possibility of inlining functions that are extern and not inline but a function supplied by a DLL is certainly off limits. But then, you could still run it through a JIT style execution engine and that could inline (splice together) any random fragments it likes.
The only situation in which a function cannot be inlined is if there is no definition for the function in the compilation unit. Even that will not prevent link-time inlining by a link-time optimizer.
Note that the inline keyword is really just a hint -- a compiler may choose not to inline functions with it and choose to inline functions without it.

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.

Are there any compilers that IGNORE C++ standard about default inline functions?

C++ ISO standard says, that:
"A function defined within a class definition is an inline function."
Are there any compilers that IGNORE this rule?
(please, do not mistake inline with inlineD - my question is if there is a compiler, that wont put there that inline suggestion that it should)
You seem to be misunderstanding what "inline" means. It doesn't mean functions will automatically be inlined; according to 7.1.2-2 it indicates that inline substitution is to be preferred.
Therefore, you can't tell whether a function is labeled inline or not from the code, since the compiler is free to decide one way or another. It's just a compiler hint.
The standard says that all compilers can ignore inline requests, whether implicit or explicit. Whether or not they do so will nornally depend on whether the function can practically be inlined - for example recursive functions cannot be.
Edit: Just to clarify - the questioner is ignoring this, from the previous para in the standard to that he quoted from:
An implementation is not required to
perform this inline substitution at
the point of call
I suspect your test is flawed. You can't test with only one such file whether the compiler ignores the inline specifier or not.
You need to include the header containing the inline function definition and include it into multiple implementation files that are then linked together. If you get linker errors about multiple defined instances of that functions, then the compiler is ignoring the inline specifier regarding its most important property: Allowing it to be defined multiple times across the entire program while still retaining the same address for it and its local static variables.
What your test probably checks is whether or not the compiler inlines the call to the function, which is actually only a hint to the compiler and only a small of many other more important consequences of the inline specifier. If the compiler does not not inline a call to the function, it is fine doing so. The standard does not require it to do anything in this matter.
See my answer to a very similar question: When is "inline" ineffective? (in C)
Summary: inline is only required to allow multiple definitions. Any function calling changes is purely optional.
Compiler's usually inline based on the number of calls to the function, the number of pseudo-instructions in the function, and a bunch of other things. Take a look at the GCC documentation on optimization options for an idea of how it does things. Basically, the inline keyword is just a hint that bumps up the likelihood that the compiler will inline. The actual decision to inline is usually complex.

Are there any compilers that IGNORE C++ standard about default inline functions? [duplicate]

This question already has answers here:
Closed 13 years ago.
C++ ISO standard says: "A function defined within a class definition is an inline function." *
Do you know about any compilers that IGNORE this rule?
Do you know about any compilers that WILL NOT put that 'inline suggestion' there?
(please do not repeat the theory about inlines, I am aware of that - I need a practical answer)
All compilers are allowed to ignore any inline suggestions they decide to. If they decide the function is too complex, for example, it won't be inlined. If you ever take the address of the function, the function may be inlined in some places, but a full function generated somewhere else for the address to point to.
Think of inline and the implicit inline when you define a function in a class definition as suggestions to the compiler.
It is not a rule, it is simply a statement of fact. The spec is simply stating that the function is defined inline. That makes it easier for the compiler to actually inline the generated code as well, but nowhere does the standard require this.
They're different concepts. What the generated code looks like is up to the compiler alone, and the standard doesn't really impose any requirements (except of course that it should behave as specified).
In gcc you can use
-finline-limit=n
-fno-inline
See my answer to a very similar question: When is "inline" ineffective? (in C)
Summary: inline is only required to allow multiple definitions. Any function calling changes is purely optional.