Jump to content
  • Advertisement

Archived

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

GD-Silver

How short is short?

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

What is a good rule of thumb to use when determining whether or not to inline a function? I understand that it should only be used for short and simple functions, but how long is too long to consider inlining? For instance, is the following function too long? return pVisible ? pPict.Draw(pPosition - pHotSpot, pDrawColor) : S_OK; You have here three accesses of private data members, one call to a member function of a private data object, and two operators. Does this push it over the line of being too complex to inline? Or how about this? return Rect((LONG)pPosition.x, (LONG)pPosition.y, (LONG)(pPosition.x + pPict.Width()), (LONG)(pPosition.y + pPict.Height())); Here you have one call to a constructor, four data casts, six calls to members of private objects, and two operators. Is this too much for a practical inline? I''m interested to hear thoughs on the matter.

Share this post


Link to post
Share on other sites
Advertisement
What I''ve heard from most other people is to not inline any function until after 1) the program works, and 2) you''ve profiled and are sure that the function is a bottleneck. If you know a function is a bottleneck after profiling, then I''d say to at least try an inline even on a bit larger function. But don''t waste the compile/testing time if you don''t know it''s a critical function.

Share this post


Link to post
Share on other sites
Don''t count the number of operations... Just use common sense: don''t inline huge functions, etc. Also, some compilers will help you with this by not inlining functions you tell them to because the inline keyword is only a suggestion to inline something.

Share this post


Link to post
Share on other sites
Ok, look:

Always, always leave function inlining disabled for debug builds. This is default in MSVC, don''t change it. Otherwise, debugging will be annoying as hell.

Inline functions that would make sense to be inlined. Remember what inlining does--replaces the function call with the function. This is useful for functions which are called rarely and/or are short.

The compiler reserves the right not to inline functions if cost/benefit analysis comes up short. Inlining is nota sure thing.


In summary, inline functions which make sense inline.

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
Ok, look:

Always, always leave function inlining disabled for debug builds. This is default in MSVC, don''t change it. Otherwise, debugging will be annoying as hell.


Indeed, I have done so. I just want to ensure that I am building my classes correctly.

quote:
This is useful for functions which are called rarely and/or are short.


Really? I thought that it would be useful for functions that are frequently called, especially for simple private data member getter functions, like

int SomeClass::GetData()
{
return PrivateDataMember;
}

since this would remove the function overhead of a simple variable retrieval. In a case like this, wouldn''t the inlining be more beneficial the more the getter function was called?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh it''s short baby

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
This is useful for functions which are called rarely


I believe "rarely" in this case refers to how many times in code they are actually seen, not how often they are called during execution.

If, for example, you have a big long non-looping function that calls another function 80 times, then inlining would inline that function 80 times, causing huge coad-bloat.

But if a function call is written only once in your entire program, but it is a part of code that executes ten thousand times a second, then making it inline won''t impact the size of your code at all, but might make it faster.

So if your function is called from eight bajillion different locations in your code, it would probably be unwise to inline it, but if it is only called from a few locations, then it might be good to inline it. The few locations from which it is called, the larger the function can be without causing major problems.

Share this post


Link to post
Share on other sites
quote:

You know, they have drugs for that...


quote:

oh it''s short baby



Hey, it''s not the size that counts, but how you use it.

Share this post


Link to post
Share on other sites

  • 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!