Jump to content
  • Advertisement
Sign in to follow this  
wcassella

Are macros like 'FORCEINLINE' worth it?

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

While working on my own code, sometimes I'll take a look at other game engines (such as Unreal Engine 4) for inspiration on how to solve various design or technical problems. One thing I noticed throughout their code base is that they frequently use a 'FORCEINLINE' macro, which evaluates to '__forceinline' on windows. I remember seeing a similar 'idinline' macro in code from id software, and I was wondering what the point of it is. I know that the actual 'inline' keyword has pretty much gone the way of the 'register' keyword, where compilers basically ignore it (though 'inline' is still useful to prevent ODR violations of course), so does it actually do anything to improve optimization in real code? I currently have Visual Studio's inline setting set to "any suitable" for release builds, and when compiling on GCC or Clang I just use '-Ofast'. Would adding a similar macro to my own code base make any actual difference, or would it just be more noise?

 

Thanks

Edited by Salty Boyscouts

Share this post


Link to post
Share on other sites
Advertisement
The purpose of the macro is cross-compatibility, as many platforms, including iOS/Mac OS X, do not have _forceinline.
The macro will resolve to either blank, “inline”, or some equivalent of _forceinline on those platforms.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

The purpose of the macro is cross-compatibility, as many platforms, including iOS/Mac OS X, do not have _forceinline.

 
They have the always_inline attribute, which FORCEINLINE is defined to in UE4 as well (specifically, #define FORCEINLINE inline __attribute__ ((always_inline))).

 

As for usage, I use it to enable inlining of trivial (single-line) functions in debug builds, to improve performance without sacrificing much of the debuggability.

Share this post


Link to post
Share on other sites

 

The purpose of the macro is cross-compatibility, as many platforms, including iOS/Mac OS X, do not have _forceinline.

 
They have the always_inline attribute, which FORCEINLINE is defined to in UE4 as well (specifically, #define FORCEINLINE inline __attribute__ ((always_inline))).

 

Exactly, one platform has foo_inline, another one has bar_inline, a third has bla_inline

 

 

So what would be preferable

#if PLATFORM_ONE
foo_inline
#elif PLATFORM_TWO
bar_inline
#else
bla_inline
#endif
void my_func(...) { ... }

with every function/method in every source file

 

or

#if PLATFORM_ONE
#define foo_inline FORCEINLINE
#elif PLATFORM_TWO
#define bar_inline FORCEINLINE
#else
#define bla_inline FORCEINLINE
#endif

once in a common file, and

FORCEINLINE
void my_func(...) { ...}

everywhere?

Share this post


Link to post
Share on other sites
Macros are just another tool, used properly in situations that they make better, sure, why not?

Problem with tools like macros in coding is they usually have a lot of side effects so you should really be sure you -need- or will greatly benefit from said tool before using it.

Also the old addage comes to mind that the compiler usually knows the code much better than the programmer, so unless lots of profiling and testing has told you otherwise, it is usually rarely a good idea to override something the compiler wants to do for you.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!