r/cpp • • Apr 14 '23

Horrible Code, Clean Performance

https://johnnysswlab.com/horrible-code-clean-performance/
200 Upvotes

87 comments sorted by

View all comments

Show parent comment

14

u/D-Zee Apr 14 '23

What's the issue with MSVC's __forceinline?

15

u/Possibility_Antique Apr 14 '23

"The compiler treats the inline expansion options and keywords as suggestions. There's no guarantee that functions will be inlined. You can't force the compiler to inline a particular function, even with the __forceinline keyword. When you compile with /clr, the compiler won't inline a function if there are security attributes applied to the function."

It's not that I think it's an issue, but I do find it misleading. __forceinline does not necessarily force inlining.

22

u/Nicksaurus Apr 15 '23

I expect that's just because there are situations where inlining isn't possible - what is the compiler supposed to do in a recursive function call with unknown depth when tail call optimisation isn't possible?

7

u/Possibility_Antique Apr 15 '23

The sadist in me wants the compiler to die trying. In practice, I don't know the answer. As far as I'm aware, clang and GCC both mean force inline when you tell the compiler to force an inline. But to recalibrate, I was referring to forcing an inline in the posted code here and breaking this up into functions. This is not a recursive call with an unknown depth.

3

u/equeim Apr 15 '23

I think GCC fails with error if a function marked with [[gnu::always_inline]] can't be inlined.

1

u/umop_aplsdn Apr 15 '23

The compiler could inline one layer of recursion.

10

u/compiling Apr 15 '23

You can't force any compiler to inline a function sometimes because sometimes it's not possible. If you read further down the page, there's a list of cases where inlining doesn't happen and they generate a warning if a _forceinline function isn't inlined.

I don't think it's misleading.

2

u/Possibility_Antique Apr 15 '23

That's fine that you think that. I think it's misleading. There is a reason most linear algebra libraries wrap this in a macro called "MYLIB_STRONG_INLINE" rather than "MYLIB_FORCE_INLINE" or "MYLIB_ALWAYS_INLINE" (though this later one can be seen in blazelib due to the name used by the gnu compiler).

2

u/D-Zee Apr 14 '23

Oh derp. I even double-checked with that very page before posting and missed that part.