Sign in to follow this  

inline

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

I just wanted to gather some thoughts on the use of the inline keyword. I have generally been using the classic criteria for deciding whether to make my functions inline or not, and I generally put my inline functions in a seperate .inl file so that I don't clutter the header with function implementations. However I've been thinking more and more recently that this 'habit' is actually just a waste of time and that I should just stick those functions in the .cpp file, turn on the compiler option to inline all suitable functions and be done with it. It would certainly save a lot of faffing about splitting my implementation across two files. The .inl file would then just for my template implementations. I read somewhere that humans are poor at determining which functions should be inlined, while the compiler is generally pretty good at determining which functions should be inlined. I was just wondering how other people approach this issue?

Share this post


Link to post
Share on other sites
Personally I leave everything up to the compiler as long as it's fast enough. If it's not I profile it, and if a lot of time is spent in call overhead I inline those functions specifically.

Share this post


Link to post
Share on other sites
The inline keyword is also only a hint to the compiler that it is free to ignore if it wants. I assume that the compiler knows better than me what is appropriate to inline, so I let it do it's work without trying to interfere.

Share this post


Link to post
Share on other sites
Perost is right, inline is only a hint to the compiler. But i know from experience that the MVC++ 6.0 compiler is not always super human in its decisions. A had a simple 3 component vector class with additions and all, but the compiler did not always include these operators. When I used the somewhat tricky __forceinline instead, the code speedup was significant. I don't know about more recent compilers.

Share this post


Link to post
Share on other sites
Quote:
Original post by dietepiet
Perost is right, inline is only a hint to the compiler. But i know from experience that the MVC++ 6.0 compiler is not always super human in its decisions. A had a simple 3 component vector class with additions and all, but the compiler did not always include these operators. When I used the somewhat tricky __forceinline instead, the code speedup was significant. I don't know about more recent compilers.
VC6.0 is a terrible compiler, I'm not surprised it didn't inline. And the "standard" edition doesn't optimise at all, you need to buy the professional version for that. Unless you're doing something pretty strange in your operators, the compiler should optimise them without any hints - possibly even with "Optimise for Size" enabled (Since the function call code size might be just as large as the operator itself).

Share this post


Link to post
Share on other sites
Quote:
Original post by tok_junior
Personally I leave everything up to the compiler as long as it's fast enough. If it's not I profile it, and if a lot of time is spent in call overhead I inline those functions specifically.


QFE. Profile it and if the bottleneck is some very short non inline functions then try encouraging the compiler to inline them.

Share this post


Link to post
Share on other sites
Take this with a pinch of salt, but in my experience, from looking at the disassembly of code generated by Visual Studio, the later versions generally seem to be pretty good at inlining and optimisation.

At least that's the impression I got, from looking at the disassembly of a vector class I'd written.

For example, some vector operations written with for loops, were optimised to be the same code that would result from trying to be more clever about it.

Share this post


Link to post
Share on other sites
What about if I'm making a .lib? Will the compiler still inline the suitable functions in the app that links with the lib?

EDIT : I just did a really simple test :

in lib.h:

int seconds_to_milliseconds(int input);






in lib.cpp:

#include "lib.h"

int seconds_to_milliseconds(int input)
{
return input * 1000;
}






in exe.cpp:

seconds_to_milliseconds(someIntValue);






resulting asm:

00401005 call seconds_to_milliseconds (401010h)




The resulting asm when I have the header and cpp in the exe project is :


00401000 mov eax,dword ptr [esp+4]
00401004 imul eax,eax,3E8h




[Edited by - _moagstar_ on November 20, 2008 2:08:40 AM]

Share this post


Link to post
Share on other sites
The inline optimize setting only influence functions that are in the same compilation unit (.cpp). If you want anything to be done across compilation units, you need to enable link-time code generation (and you have to enable it in several places). My experience is that optimizations across compilation units are not nearly as advanced as those within a single compilation unit.

Share this post


Link to post
Share on other sites

This topic is 3309 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this