Jump to content
  • Advertisement
Sign in to follow this  
Drunken_Monkey

inline keyword...

This topic is 4253 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

Okay, inline basically copies the body of the function in question and pastes it at the calling place to save time, right? I remember reading somewhere that the compiler will not make the function inlined if it will end up hurting performance instead of speeding it up. So is there a reason not to make every function inlined?

Share this post


Link to post
Share on other sites
Advertisement
Yes, the inline keyword suggests to the compiler that this code might be inlined. However, the compiler may inline functions without inline and vice versa.

The reason you don't want to do it with every function is twofold a) you're going to massively increase the size of your executable if you do that on any project with a reasonable scale and b) the compiler may make a mistake and inline a function which shouldn't be - you're trying to help it.

You can end up thrashing the cache or bloating the code (on the other hand, you can end up doing just one, neither, or even the opposite).

There are also other issues, see here for a far more authoritative source than I.

EDIT: Fixed link

Share this post


Link to post
Share on other sites
The reason you don't inline everything is because you, as the programmer, have a better idea where performance does *not* matter than the compiler. Telling the compiler to inline things that don't need to be will only lead to bloated executables.

While compilers are very good at determining which method is faster, they have a harder time determining when its needed or not. This is why we have "hint" keywords such as inline (and to a lesser extent the "register" keyword many years ago.)

Share this post


Link to post
Share on other sites
The compiler will also happily inline those functions *not* marked inline if the definition is available and doing so is is deemed advantageous.

Basically, in modern compilers, the inline keyword has exactly one meaning: to tell the compiler and linker that if multiple definitions of the function in question are found, they are all one and the same, and thus do not violate the One Definition Rule. That is, inline allows one to define (not only declare) a function in a header file, thus giving the compiler an opportunity to inline calls to that function in all translation units that include that header.

The main drawback, of course, is longer compile times, as multiple copies of the same function will have to be compiled.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravyne
The reason you don't inline everything is because you, as the programmer, have a better idea where performance does *not* matter than the compiler. Telling the compiler to inline things that don't need to be will only lead to bloated executables.

Or the other way around. Because the compiler has a better idea of whether inlining a function will lead to any performance boost at all, you're only allowed to provide hints, and even that is often discouraged. ;)

Inlined functions are not necessarily faster.
In particular, inlining large functions may simply thrash the cache. (You save the small bit of prologue/epilogue code for entering and leaving the function, and that is basically constant size. In return, you get more code, and if the function is called from different places, they will get different copies of the code, which means the cache will be of no use and performance may end up going down.

Share this post


Link to post
Share on other sites
Indeed. The compiler has massively more information available for making an informed decision about whether to inline or not, and what's more, it can do that decision separately for each and every call site (which probably matters at least as much as the contents of the function). As an optimization hint, the inline keyword is an extremely coarse one, and most modern compilers probably usually ignore it because they know better.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!