Jump to content
  • Advertisement
Sign in to follow this  
ArchangelMorph

Inline?

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

When would you need to use inline functions? I've heard alot of people talk about them and I looked for a definition and this is all I could find..: "inline functions. The inline directive can be included before a function declaration to specify that the function must be compiled as code at the same point where it is called. This is equivalent to declaring a macro. Its advantage is only appreciated in very short functions, in which the resulting code from compiling the program may be faster if the overhead of calling a function (stacking of arguments) is avoided. The format for its declaration is: inline type name ( arguments ... ) { instructions ... } and the call is just like the call to any other function. It is not necessary to include the inline keyword before each call, only in the declaration." So where and how is this practically applicable to games programming? Sorry for the noobish question..

Share this post


Link to post
Share on other sites
Advertisement
Inlining basically gives the compiler a hint that you would like a function inlined. If it does inline it, the compiler basically copies the contents of the function to the point it is called, eliminating the function call overhead. Short functions benefit most. Inlining long functions or inlining too much can bloat the code and potentially slow it down.

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
Inlining basically gives the compiler a hint that you would like a function inlined. If it does inline it, the compiler basically copies the contents of the function to the point it is called, eliminating the function call overhead. Short functions benefit most. Inlining long functions or inlining too much can bloat the code and potentially slow it down.


So what your saying is its good practice to inline smaller functions to allow the compiler to copy them (at compile-time)to where they are called (at run-time) to speed everything up a bit..?

So what would you say would be the limit in terms of code-size/complexity in which inlining would still be beneficial for speed optimisation?

Share this post


Link to post
Share on other sites
rule of thumb:
inline if all of theese apply:
  • function is short approx 1 || 2 lines
  • it's frequently called (use a profiler)
  • inlining improved performance (use the profiler)
    additionally there's some corner cases not covered by the above:
    inline if:
  • it's a template function (it's a safe bet that export isn't implemented (and if you don't know what it is don't worry))
  • the rest of the class is abstract or doesn't already have an implementation file and the function satisfies most of the criterion above. (common case are interface classes where the only implemented function is an empty virtual dtor)

    that should cover most of it.

    Also not that heavy use of inline can degrade performance due to cache pollution.

    Share this post


    Link to post
    Share on other sites
    Like he said, frequency of call is one of the biggest factors of whether something should be inlined. This is basically why just about any decent math library you see inlines just about everything in the vector/quaternion/matrix classes. Those things are called so much and they are short enough to see significant reductions of overheads by avoiding the actual function call.

    Share this post


    Link to post
    Share on other sites
    Be aware that most modern compilers (e.g. MSVC71) are pretty good at picking functions to inline themselves without you needing to specify the 'inline' keyword. It's still worth doing for those instances in which the compiler misses something (and it puts more intention into your code, which is good), but don't worry too much about doing it all upfront. If you want to try profiling it to see how much of a difference it makes, make sure you switch off the compiler's automatic inlining first, or your results will be skewed (in MSVC.NET this is Properties->C/C++->Optimization->Inline Function Expansion, set to 'Only __inline').

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by superpig
    Be aware that most modern compilers (e.g. MSVC71) are pretty good at picking functions to inline themselves without you needing to specify the 'inline' keyword. It's still worth doing for those instances in which the compiler misses something (and it puts more intention into your code, which is good), but don't worry too much about doing it all upfront. If you want to try profiling it to see how much of a difference it makes, make sure you switch off the compiler's automatic inlining first, or your results will be skewed (in MSVC.NET this is Properties->C/C++->Optimization->Inline Function Expansion, set to 'Only __inline').


    What about for VS 6.0?

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by ArchangelMorph
    What about for VS 6.0?

    [Project menu]->[Settings menu]->[C/C++ tab]->[Optimizations category]->[Inline Function Expansion list box]->[Only __inline]

    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!