Jump to content
  • Advertisement
Sign in to follow this  
Trenton05

Inlining, visual c++

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

Everytime I try to do simple inlines, the generated code is the same, no optimizations. Even code as simple as: inline long ScalarAdd(long x, long y) { return x+y; } generates the same code when used as: long ScalarAdd(long x, long y){ return x+y; } I've enabled all the optimization controls in the project settings, but it still never works. Am I doing something wrong, or is inline not actually suppose to "inline" it with the code.

Share this post


Link to post
Share on other sites
Advertisement
inline is a suggestion to the compiler, which is free to ignore it. It all depends on how the function is used.

If the function's body is not visible in the current translation unit, it cannot be inlined.
If the function has its address taken, it cannot be inlined.
etc.

Share this post


Link to post
Share on other sites
I realize it's only a suggestion, but why would something like

inline int add(int a, int b) { return a+b; }
void main(){
int a = 1;
int b = 2;
int c = add(a,b);
}

not be inlined by the compiler, the only reason i want to do this is rather than use so many defines, use the inline, but it's never actually inlining any code i write.

Share this post


Link to post
Share on other sites
What optimization options are you using?

When I turn optimizations on, g++ either just generates an empty main (if I don't use c), or directly replaces it with 3 (if I do use it).

Share this post


Link to post
Share on other sites
I'm not sure, maybe it doesn't show it's inlining it because I'm debugging it, I think it'll optimize it if i do release build.

Share this post


Link to post
Share on other sites
Yea, I tested it, it just doesn't optimize anything, including inlines, for debugging. Even though it gives you the option to inline functions for debugging.

Share this post


Link to post
Share on other sites
If you're debugging, the MSVC sort of assumes that you *want* to step through every function and thus won't inline. If you want to see if inlining is actually done you should look at the generated assemly listing of a release build.

Share this post


Link to post
Share on other sites
Fwiw, if you're using MSVC and you feel the need to force the compiler to inline, you can use something like this:


#ifndef FORCEINLINE
#if (MSC_VER >= 1200)
#define FORCEINLINE __forceinline
#else
#define FORCEINLINE __inline
#endif
#endif

FORCEINLINE long ScalarAdd(long x, long y)
{
return x+y;
}


I'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by HarryW
I'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.


AFAIK in MSDN they say even __forceinline is only a suggestion to compiler and it can be ignored if needed.

I think the problem lies in Debug mode. In Release, even while debugging (F10, F11), you can actually see where the function IS inlined, the debugger just don't follow F11 command but stays in place for a few instructions.

I always use Release in MSVC++, don't even touch Debug. I just find no benefits of it. I just have two configurations: Release_optimized and Release_NotOpt for debugging, so the code generated has same structure as original c++ code.

Cheers.
/def

Share this post


Link to post
Share on other sites
Quote:
Original post by deffer
Quote:
Original post by HarryW
I'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.


AFAIK in MSDN they say even __forceinline is only a suggestion to compiler and it can be ignored if needed.

I think the problem lies in Debug mode. In Release, even while debugging (F10, F11), you can actually see where the function IS inlined, the debugger just don't follow F11 command but stays in place for a few instructions.

I always use Release in MSVC++, don't even touch Debug. I just find no benefits of it. I just have two configurations: Release_optimized and Release_NotOpt for debugging, so the code generated has same structure as original c++ code.

Cheers.
/def

To the OP: Can you show us your compiler switches? In MSVC, they should be under C/C++ -> Command Line. If your using the /Od switch, I'm pretty sure the compiler won't inline your functions.

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!