Inline

Use this tag for questions specifically about the effects of the inline keyword, together with the appropriate language tag.

Inlining

Inlining is a compiler optimization which eliminates function calls by replacing them with the function's code, thus executing it inline



Compiler keyword hint

Example

"If your compiler can do this then writing functions in a way that your compiler is able to inline is better than manually inlining the calls yourself"

from question  

Should I consider resource expense of stack frame creation/ movement in method calls - are they significant?

"This inline does not necessarily mean the usual macro style inlining;the macro style inlining is decided by compiler and the programmer doesn t have much control over it"

from question  

Intellisense warning: Incomplete type is not allowed (in inline method but defintion of type is in cpp)

"The keyword inline is more about telling the compiler that the symbol will be present in more than one object file without violating the one definition rule than about actual inlining which the compiler can decide to do or not to do"

from question  

Multiple definition of template specialization when using different objects

"The inline keyword makes it easier for the compiler to apply this optimization by allowing the function definition to be visible in multiple translation units but using the keyword doesn t mean a c++ compiler has to inline the function and not using the keyword doesn t forbid a c++ compiler from inlining the function"

from question  

Why are C++ inline functions in the header?

"The inline keyword is more of a hint to the compiler than an explicit directive these days;there is no specific rule about not inlining functions with return statements"

from question  

Situation where inline expansion may not work?

"The specific reasons why a function might not be inline when the inline keyword aren t documented by microsoft;the closest i can find is the documentation for compiler warning c4710 . the inline keyword is only a hint and the compiler uses heuristics to determine whether inlining is a worthwhile optimization"

from question  

Visual Studio 2010 - Function not inlined - Why?

"As the others have noted in a modern compiler inline is little more than a linkage modifier;actual inlining is controlled by optimisation flags linkage requirements and compiler-specific attributes"

from question  

Inline method: inside vs outside class definition

"Generally speaking the inline keyword is used more now to allow you to violate the one definition rule when you define a function in a header than to give the compiler a hint about inlining"

from question  

C++ Inline methods for performance

"However if the compiler decides that the compiler would rather not inline your static inline-functions either because the compiler think s that inlining is worse or because inlining is impossible then the compiler will have to make a separate copy of that function in each file the compiler s used in"

from question  

Static and extern inline functions in C

"But it is up to the compiler if the method is actually inline or not;the compiler inlining is sophisticated enough to only in-line if this will help in the optimization stratergy being used"

from question  

Does using STL increase footprint significantly?

Call fatter faster

Example

"Inline expansion or inlining is an optimization where a function call is avoided by copying the called function into the frame of the caller;a function call can be expanded inline whether the function has been declared inline or not"

from question  

Why are pointers to inline functions allowed?

"Inlining inlining produces fatter code which is faster the inline functions will not appear in the call stack"

from question  

Which optimization compiler switches make debugging very hard?

Size bytecodes larger

Example

"Callee is too large message is printed by c1 when the size in bytecodes of the method being inline is larger than maxinlinesize 35 multiplied by nestedinliningsizeratio 90 on each next level of inlining;too big and hot method too big messages are printed by c2 when the size of the method being inline is larger than maxinlinesize 35 or freqinlinesize 325 respectively"

from question  

Meaning of "callee is too large" in jvm +LogCompilation output

"But as the inline code gets longer the savings for inlining become smaller so maybe you could just have a hard limit on the size of the code - any code to be inline must be smaller than the limit"

from question  

Extract code from method body and execute it inline using expressions?

"Code memory if you are inlining a function which is used in many places then the code size will increase;stack usage if your inline function uses more variables then more stack space is used"

from question  

Inline functions and their implementation

Others

Example

Templates will be inline in the standard meaning of inline which is more related to the one definition rule than to actual code inlining

from question  

How does templates work, are they always inlined?

On a good compiler for a modern platform inline will affect only a very few functions;it is just a hint to the compiler modern compilers are fairly good at making this decision themselves and the the overhead of a function call has become rather small often the main benefit of inlining is not to reduce call overhead but opening up further optimizations

from question  

Why not mark everything inline?

The process of inlining is more than just copy and paste the inline code has to make sense within the inline code the inline code is being injected into

from question  

How compiler manage return inline function?

The gdb error message cannot evaluate function -- may be inline is a bit misleading as inlining isn t the only reason for a function being unavailable

from question  

Gdb stl functions still show as inlined after disabling optimizations

The fact that the array pointer could be kept in a register when the method was inline was probably more significant;so the conclusion is that the big difference you re seeing is because of inlining

from question  

Why is it that, in C#, referencing a variable from a function argument is faster than from a private property?

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

from question  

Why should I ever use inline code?

In the case of very small inline functions or a large amount of subsequent optimization the resulting code may be even smaller after inlining since the remaining code if any may be smaller than the overhead involved in the calling the function 2

from question  

Inlining and Instruction Cache Hit Rates and Thrashing

Be aware though that this should only be on a release build since inline code is difficult to debug;also you said that you don t mind making your program larger as long as your program gets faster but that often inlining makes it slower

from question  

Delphi 2010 inlining useless?

But this shouldn t worry you too much as in many cases inline code is much smaller than the original function due to the fact that inlining misses register saving restoring has some parts of inlining removed due to them being evaluated at compile time etc

from question  

How to make inline function only take up one copy's worth of memory space in executable file

Sometimes it might perhaps go slightly slower because inlining increases machine code size which is detrimental to cpu cache efficiency read about locality of reference;also a header file with many static inline functions needs more time to be compiled

from question  

C - Header Files versus Functions

I m well aware that inline is more of compiler decision than of user going so far as even to inlining non-specified inline-functions so the user control is almost negligible

from question  

Are <function number of lines> and <times function called> considered inverse proportional for inline?

A similar argument can be made for inlining functions inline is generally faster but will remain in the same big-o complexity class although there is an additional size tradeoff inlining makes your compiled program larger if the code was being used in many places

from question  

Performance, big O notation, of a recursive an a non-recursive algorithm

The inline keyword is more or less useless in this era regarding early inlining original intent

from question  

When should I consider the performance impact of a function call?

If the call is inline there is no difference between the two approaches - as long as the parameter is known at compile time which it must be a decent compiler will remove the unnecessary switch in both cases;the only case where you would see a difference is if inlining does not occur - in this case the templated approach would allow the switch to be removed while the other would not

from question  

C++ function vs template with an integer parameter?

In reality the inline keyword has less to do with inlining code and more to do with allowing legal violation of the one definition rule

from question  

Is the order of function definitions important for inlining in C++

Again i doubt that inlining here will change anything but my instincts say that if anything inlining here is more likely to hinder performance than help

from question  

Is it more optimized to use local function or global function?

S an important side note it is quite common to find that inlining makes no noticeable improvement in performance and there are many cases where not inlining code is better than inlining it;i ve found this to even be true of simple one line accessor functions just because the code when inline requires fewer instructions than it does to call the function doesn t always mean the compiler will do a better job with the compiler

from question  

C++: proper inlining syntax

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0