[C++] Optimizing away function call for shortcut?

This topic is 4372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've got a little section of code:
void SomeObject::function1()
{
for( /* snip */ ){
if( /* snip */ ){
switch(value){
case blah:
function2(object);
break;
/* snip */

}
}
}
}

void SomeObject::function2()
{
m_memberPointer->function3(object.param1, object.param2, object.param3);
}

void function3(param1, param2, param3)
{
/* snip */
}


Now, I'm using VC++ 2005 express, and whenever I've tried to start the program by running straight to the function call in function2, it completely skips and does not stop. However, when I skip to the line that calls function2, inside of function1, it stops, and then when I step into the line, it goes straight to function3, without going to function2 first. < ... a brainstorm and 2 minutes later> Okay, I remembered that I was actually compiling in release mode. <smacks the side of my head> I didn't know that complete function calls will be optimized away like this, especially since I haven't explicitly named any of those functions as inline, and all of the implementations are in a seperate source file and not in the header file. What other small things will be optimized away, that I might have to look out for?

Share on other sites
Some common optimizations that making debugging a release mode exe difficult:

• Inlined functions won't get called - the code will be inserted into the calling function, so breakpoints in inlined functions never get hit. And of course you don't know which functions the compiler decided to inline

• Statements may be reordered or eliminated entirely

• Variables may be created/removed

• Two functions that generate identical machine code may be collapsed into a single function (even if the parameters are of different types)

• And of course, anything else the compiler writer can think of that improves efficiency.

Share on other sites
In builds with optimisation some function calls will be inlined, which means the function body's code is basically pasted in to where the call takes place, eliminating the overhead for a function call. This works well for small functions like one-liners where the function call overhead is a significant proportion of the function's processing time.

You shouldn't ever debug in release build! Usually it's just misleading - often variables don't correspond to once place in memory so they can even show incorrect values.

edit: too sloooow

Share on other sites
Quote:
 Original post by AshleysBrainYou shouldn't ever debug in release build!

But it's good to know how, for those situations when your code misbehaves only in Release mode and works perfectly in Debug, which is always great fun [smile]

Quote:
 I didn't know that complete function calls will be optimized away like this, especially since I haven't explicitly named any of those functions as inline, and all of the implementations are in a seperate source file and not in the header file.
inline is just a hint to the compiler - they have various heuristics to decide whether or not they should inline a function (e.g. if it's got a loop then they probably won't; it's less likely as the function gets longer; it's more likely if you add inline, even more if you add __forceinline, but never guaranteed). They normally won't inline functions that are defined in one source file and called in another, unless you enable whole-program optimisation, but otherwise you can't precisely tell whether or not a certain function call will be inlined.

Share on other sites
Quote:
 Original post by ExcorsThey normally won't inline functions that are defined in one source file and called in another, unless you enable whole-program optimisation, but otherwise you can't precisely tell whether or not a certain function call will be inlined.

So, even though I don't suggest something as inline, the compiler could just decide to inline it anyway?

Share on other sites
Quote:
Original post by Endar
Quote:
 Original post by ExcorsThey normally won't inline functions that are defined in one source file and called in another, unless you enable whole-program optimisation, but otherwise you can't precisely tell whether or not a certain function call will be inlined.

So, even though I don't suggest something as inline, the compiler could just decide to inline it anyway?

sure, depending on the compiler itself and the settings you passed to it.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• Forum Statistics

• Total Topics
634048
• Total Posts
3015237
×