1

I am learning to use inline functions While looking at different projects I don't see the use of __forceinline usually in code

So I want to know if there are any reasons you won't use __forceinline in the GetInstance function of a singleton class

These functions are usually small and are called multiple times

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • 3
    Your compiler usually knows better what to inline. – tkausl Jul 13 '22 at 13:50
  • 2
    1. It's not portable between compilers 2. Compiler is much better than you at optimizing code. 3. Given 2, it only obfuscates code at best, it prevents other optimizations at worst. – Yksisarvinen Jul 13 '22 at 13:51
  • @tkausl But in this case we know this must be a good place where inlining is beneficial, so why rely on the compiler – Techno Gamer Jul 13 '22 at 13:51
  • `But in this case we know this must be a good place where inlining is beneficial` Because the compiler knows this as well. – tkausl Jul 13 '22 at 13:52
  • @Yksisarvinen Oh, I didn't know there are other optimizations – Techno Gamer Jul 13 '22 at 13:52
  • The compiler will inline it either way (except in debug mode maybe). All you do is make the code non-portable to compilers which don't have that attribute or spell it differently. You don't need to ever use the attribute except in rare cases where you noticed the compiler isn't inlining the way you want, where it is absolutely critical that the function is inlined or where the inlining somehow changes the behavior of your program (which can't usually happen if you are writing just portable/standard C++). – user17732522 Jul 13 '22 at 13:53
  • @user17732522 Ok, How do we know if the compiler is inlining the function – Techno Gamer Jul 13 '22 at 13:56
  • @TechnoGamer You look at the assembly output of the compiler. – user17732522 Jul 13 '22 at 13:56

1 Answers1

0

Honestly, I would advise against putting __forceinline especially on a singleton GetInstance function since it's not bottleneck.

If you want performance, you must mesure first or assume any change is gonna make the code slower.

Compiler flags will help performance much more than trying to __forceinline stuff. The compiler knows much more than you think. Adding LTO, PGO, compiling for a specific CPU architecture can help a long way.

What if for example the GetInstance function is forced to be inline and result in more binary size and screw with the instruction cache and slows down your program? I would say it's very unlikely, but if you haven't measured, you cannot assume it won't.

Also in a OOP designed program where you usually see singleton like so, a simple GetInstance function is very unlikely to cause any significant performance problem. Allocations, memory scattering, inheritance based polymorphism, virtual functions are usually abused in strongly OOP based designs. Those are far more likely to be performance bottlenecks.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141