inline

Started by
7 comments, last by Extrarius 15 years, 5 months ago
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?
Advertisement
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.
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.
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.
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).
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.
Construct (Free open-source game creator)
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.
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]
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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement