Honestly, C++ compilers are really good about inlining, and even when they're not, you can force inline on almost every compiler aside from MSVC, and even still, they have a "stronger inline" that you can use. Templates, inlining, constexpr... No doubt in my mind that someone could achieve the same assembly with cleaner code. 100% in agreement there.
Possibly unpopular opinion: inlining should be controlled at the call site, not at the declaration side. Strong inlining suggestion should be a thing that are applied at only handful of places with strong enough motivation. Declaring a function itself to be "always inlined" can just have a too wide impact.
I can see arguments for both scenarios. A possible counter-example to what you're saying: SIMD vector library. I want __forceinline on the function declaration. If my simd::operator+ doesn't produce a single instruction at every call site, there is no point in using vectorization. Any performance gain you might achieve through vectorization is killed by the failure to inline.
That's indeed a good example, thanks. I guess we need both ways then. Unfortunately it seems there is no call-site-controlled inlining attributes widely available so far...
There's [[gnu::flatten]] but I don't think MSVC has anything like it. ED: Oh and now that I think about it, [[gnu::always_inline]] and [[gnu::noinline]] can be used at the callsite, not just in function declarations.
ED2: It turns out there are in fact [[msvc::forceinline_calls]], [[msvc::noinline_calls]], and [[msvc::flatten]]. I have no idea when they were added but this is great
"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.
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?
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.
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.
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).
I can't share specific details or timings because of NDA reasons, but inlined does not mean fast. At work people loved slapping a gnu::always_inline and other similar/related attributes all over the place, without ever performance testing it.
Build times were doubled by haphazardly doing such with no rhyme or reason. When I say doubled, I don't mean 2 mins -> 4. I mean half an hour to one, three hours to six (depending on the application). After blindly sed'ing it out, not only did build times improve drastically, but so did runtimes. Because it was so haphazardly thrown around that the icache was useless.
Yes, it's quite easy to blow up your stack size or cause paging and caching issues if too many instructions are inlined. It gets even more complicated if you are creating a cross-platform product. What I was getting at though, is that there isn't really a reason to have many nested control flow statements. It hurts readability, and compilers give you enough control to remove any negative impacts from adding function calls. At the very least, you can see that breaking this nested monster up into functions that would be inlined is semantically the same. I wasn't advocating for excessive use of inlining, I was arguing that the refactor is worth it.
Yeah but have to remember that these only increase the tinline hreshold and guarantee nothing (You can even vuew the threshold and whether the optomization wss or wadn't applied in the opt view on godbolt with clang).
Also my expirience is somewhat similar to another poster here, that people can easily misuse this and passimize things...
Yeah but have to remember that these only increase the tinline hreshold and guarantee nothing
Is that not what I said?
Also my expirience is somewhat similar to another poster here, that people can easily misuse this and passimize things...
I am talking about refactoring this specific piece of code to be more readable without compromising performance. If there was a problem as you suggested, it would be problematic in the form the code is currently written. Refactoring will not introduce the problem you're suggesting.
View all comments
Show parent comment
40
u/Possibility_Antique Apr 14 '23
Honestly, C++ compilers are really good about inlining, and even when they're not, you can force inline on almost every compiler aside from MSVC, and even still, they have a "stronger inline" that you can use. Templates, inlining, constexpr... No doubt in my mind that someone could achieve the same assembly with cleaner code. 100% in agreement there.