Archived

This topic is now archived and is closed to further replies.

'inline' functions

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it a good idea to try to make all functions inline (whenever possible)? I don't see them used too often, but it seems to me that all it would do is make the program run a little faster. Am I wrong or what? [edited by - Mulligan on June 4, 2002 11:35:06 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Mulligan
Is it a good idea to try to make all functions inline? I don''t see them used too often, but it seems to me that all it would do is make the program run a little faster. Am I wrong of what?



The inline statement is just a suggestion to the compiler to make the function inline. There are certian criteria a function must apply to before the compiler will inline the statement.

#1. Inline functions must not call non-inline functions or the benefits of the inline are lost.

#2. No loops! If the compiler sees a loop in the inline it will typically ignore the inline statement.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

The inline statement is just a suggestion to the compiler to make the function inline. There are certian criteria a function must apply to before the compiler will inline the statement.

#1. Inline functions must not call non-inline functions or the benefits of the inline are lost.

#2. No loops! If the compiler sees a loop in the inline it will typically ignore the inline statement.



Actually this is not quite correct. inline is a request but you _can_ have inline functions that the compiler will (or should since the implementation is based on each compiler) inline even if it calls non-inline functions. I think what you are thinking of is that you (normally) cannot have recursive inline functions.

Other (common) times when inline isn''t preformed is when the code contains a loop (as you mentioned), switch, goto, or static variables.

- mongrelprogrammer

Share this post


Link to post
Share on other sites
The original post said you don''t see inline''s being used. That''s not quite true, cuz all functions implemented within a class definition, such as set/gets, are inherently inline.

Share this post


Link to post
Share on other sites
quote:
Original post by Mulligan
Is it a good idea to try to make all functions inline (whenever possible)? I don''t see them used too often, but it seems to me that all it would do is make the program run a little faster. Am I wrong or what?


Making a function inline is a tradeoff between efficiency, code size and insulation.
- inlining gives you a gain in efficiency because it can insert code directly into where a function call would go, eliminating the stack manipulation and jumps that need to occur for out-of-line functions in C/C++.
- code size can be increased by inlined functions. Out-of-line functions contain the instructions to perform a function in only one place, whereas inline functions insert the code of the function every place it''s used. (This is also where some efficiency can be gained, since the compiler can treat all uses of the function in their current contexts. For example, if you have a function that operates on two pointers conditionally, and some instances you only operate on one pointer and the other is set to NULL, the inline function can potentially factor-out the unused code).
- the definition (e.g. function body) of the entire inline function must be known by every module that uses it at compile time. For out-of-line functions, client modules only need the declaration (e.g. function signature) and can link to the code at link time. This causes extremely tight dependencies, hurting insulation. Insulation is a very big deal for large projects.

Whether or not you choose to inline depends on what part of your project you''re evaluating and your particular needs. I personally work on large, modular projects all the time so insulation is my most important value here; I put everything out-of-line that I can unless I have a strong suspicion that I can increase efficiency by inlining without demolishing my insulation.

I''d also like to note that the previous poster is incorrect about inlines calling out-of-line functions--you do not lose all benefits of the inlines if you do this. Look up the pImpl article for a good example of when a function can be very large and call many out-of-line functions, but when inlined provides a great benefit.

Share this post


Link to post
Share on other sites
Wow, thanks.
One last question. In VC++, in the "Project Settings" dialog, under the C/C++ tab, with "Optimizations" selected in the drop down menu, an option called "inline function expansion" appears. The short discription about what it does confuses me. What is the difference between the three options?

Share this post


Link to post
Share on other sites
That option tells the compiler what functions to consider inlining:

Disable * - Compiler doesn''t inline anything, even if you request a function to be inlined explicitly. Most common in DLL files, where other modules must extract just function addresses and not actual code.

Only __inline - The compiler only considers inlining functions with the __inline keyword (but that doesn''t gaurantee a function will be inlined).

Any suitable - The compiler will inline functions that it finds suitable for inlining on its own free will. It''s almost like putting an __inline keyword on every function and having the second option selected.

Share this post


Link to post
Share on other sites
quote:
Original post by Mulligan
Wow, thanks.
One last question. In VC++, in the "Project Settings" dialog, under the C/C++ tab, with "Optimizations" selected in the drop down menu, an option called "inline function expansion" appears. The short discription about what it does confuses me. What is the difference between the three options?


Disable - No functions are inlined, including those that are specifically declared ''inline''

Only __inline - Normally what you would think the compiler would do. It considers your request for inline for functions that are declared inline or those that are defined within a class (or struct, ect) declaration.

Any Suitable - Does the same as ''Only __inline'' but also may inline other functions not defined as inline. Nice optimization feature since the compiler is often better at picking the functions that need inline.

In regards the the very first question it is not always good to inline. If the compiler actually inlined everything you wanted and you inlined a large portion of your code it could actually end up slower. If you had an inlined function in the body of a loop you would normally think that it would be faster than if it wasn''t inlined. However, if the code that makes up the loop does not fit within the processors very limited supply of cache memory the code would have to be copied in and out of levels of memory - creating a huge slowdown. Fortunetly the complier normally doesn''t let something like this happen.

- mongrelprogrammer

Share this post


Link to post
Share on other sites