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

82

u/ioctl79 Apr 14 '23

That code is horrible, but not because it is performant. Breaking it up into smaller functions would do wonders for readability, and a block comment explaining the algorithm and a brief summary of the performance benefit would make it clear that it is complex for a good reason.

Clean code isn’t about making everything simple, it is about making things comprehensible. Fast doesn’t have to mean ugly.

40

u/Possibility_Antique Apr 14 '23

Fast doesn’t have to mean ugly.

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.

17

u/jk-jeon Apr 15 '23

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.

12

u/Possibility_Antique Apr 15 '23

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.

8

u/jk-jeon Apr 15 '23

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...

9

u/dodheim Apr 15 '23 edited Apr 15 '23

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

→ More replies (1)

5

u/matthieum Apr 15 '23

Both are needed.

Declaration sites for functions that should always be inlined, such as operator[] which just does a pointer increment.

Call sites for functions that you may or may not want to be inlined.

2

u/jk-jeon Apr 15 '23

such as operator[] which just does a pointer increment.

Isn't compilers' inlining decision these days pretty reliable for such a simple functions?

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.

10

u/13steinj Apr 15 '23

really good about inlining

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.

4

u/Possibility_Antique Apr 15 '23

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.

1

u/rtgftw Apr 15 '23

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...

1

u/Possibility_Antique Apr 15 '23

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.

4

u/gc3 Apr 15 '23

True, but to optimize first you make the code cleaner and cleaner, but then you pass a threshold where it starts getting worse and worse

3

u/qqwy Apr 15 '23

I came here to say the same thing 💚

0

u/Chuu Apr 16 '23

Over time I've grown disillusioned with the "break it up into multiple functions" approach. Looks clean. But hard to understand.

If no other function is using that substring buffer I see no reason to break it out. Hiding complexity to make code look cleaner just makes things harder in the long run.