# Are macros like 'FORCEINLINE' worth it?

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

## 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 on other sites
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 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 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 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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633667
• Total Posts
3013254
×