When should I use __forceinline instead of inline?

前端 未结 12 1344
旧巷少年郎
旧巷少年郎 2021-01-01 09:34

Visual Studio includes support for __forceinline. The Microsoft Visual Studio 2005 documentation states:

The __forceinline keyword overrides the co

相关标签:
12条回答
  • 2021-01-01 09:48

    For SIMD code.

    SIMD code often uses constants/magic numbers. In a regular function, every const __m128 c = _mm_setr_ps(1,2,3,4); becomes a memory reference.

    With __forceinline, compiler can load it once and reuse the value, unless your code exhausts registers (usually 16).

    CPU caches are great but registers are still faster.

    P.S. Just got 12% performance improvement by __forceinline alone.

    0 讨论(0)
  • 2021-01-01 09:48

    There are several situations where the compiler is not able to determine categorically whether it is appropriate or beneficial to inline a function. Inlining may involve trade-off's that the compiler is unwilling to make, but you are (e.g,, code bloat).

    In general, modern compilers are actually pretty good at making this decision.

    0 讨论(0)
  • 2021-01-01 09:51

    Actually, even with the __forceinline keyword. Visual C++ sometimes chooses not to inline the code. (Source: Resulting assembly source code.)

    Always look at the resulting assembly code where speed is of importance (such as tight inner loops needed to be run on each frame).

    Sometimes using #define instead of inline will do the trick. (of course you loose a lot of checking by using #define, so use it only when and where it really matters).

    0 讨论(0)
  • 2021-01-01 09:55

    The one place I am using it is licence verification.

    One important factor to protect against easy* cracking is to verify being licenced in multiple places rather than only one, and you don't want these places to be the same function call.


    *) Please don't turn this in a discussion that everything can be cracked - I know. Also, this alone does not help much.

    0 讨论(0)
  • 2021-01-01 09:55

    The inline directive will be totally of no use when used for functions which are:

    recursive, long, composed of loops,

    If you want to force this decision using __forceinline

    0 讨论(0)
  • 2021-01-01 09:59

    You know better than the compiler only when your profiling data tells you so.

    0 讨论(0)
提交回复
热议问题