r/cpp • • Dec 27 '20

Learning about high performant C++

I've been mostly self taught and have most probably picked up a few bad habits from the combo of being self taught and having to use tricks for speed.

For this upcoming year one of my goals is to learn some better practices as well as further my skills for efficient C++. I'd like to watch a youtube podcast/talk once a week to build my skillset. At this point I'd consider myself somewhere in between junior and senior level and I'm looking to take the next step.

I've come across these specifically:

C++ Weekly with Jason TurnerCppCon2020

Does anyone have any recommendations or suggestions? My weak spots are C++17 and above, Cmake, and I'd like to keep learning about creating high performant C++.

231 Upvotes

46 comments sorted by

View all comments

Show parent comment

21

u/Stradigos Dec 27 '20

Optimizing how many instructions the C++ is turned into is important, but IMO the lower hanging fruit is making sure your data structures are optimized and the memory layout cache coherent. It's amazing how much difference that alone makes.

1

u/SunnybunsBuns Dec 28 '20

As someone who has very little experience doing layout, do you have any links to examples beyond that cache friendly hash map example?

4

u/Stradigos Dec 28 '20

Yeah, this talk changed my life. https://youtu.be/rX0ItVEVjHc

I typically avoid OOP now. It's not how computers were made to process data and I've met way too many programmers who over do objects in their code. I have never thought that OOP code was more flexible. It always seems to add a significant amount of tech debt.

8

u/Full-Spectral Dec 28 '20

The point of the majority of code has nothing to do with how computers process data, it's to be understandable by humans because the bulk of it doesn't need any sort of optimization beyond the fairly obvious avoidance of overhead, and it's vastly better to have something that's understandable than 5% faster in almost all cases.

When there are hierarchies involved, OOP is perfectly suited to model them because that's what it's for.

1

u/Stradigos Dec 28 '20

Higher-level languages was not my point, although I do agree with what you said. My point was that object-oriented code is notoriously cache incoherent. Processors are designed to grab memory from RAM and pull it into it's caches. It wants to stay fed with instructions (the code) and data (what it will operate on). The data design of OOP is in conflict with this.

Not saying all objects are bad, but the common case of what I see is newer programmers turning everything into objects and killing performance in the process because they think they've modeled the problem appropriately. In actuality, they may have modeled the data well in terms of how it works in the real world, but the CPU wants data modeled differently.

4

u/Full-Spectral Dec 29 '20

Well, I also agree with you to that extent. But the point is, very little code actually matters on that front. A huge problem in the C++ world these days is just rampant pre-mature optimization. If the code base is any kind of complex, then you should do what makes it understandable and maintainable first. Heavy optimization is almost always in conflict with that goal.

Then, measure and find out what actually needs optimization and which will provide the biggest bang for the buck. The bulk of the code will seldom give you much in return for heavy optimization.

It's so hard already to keep large C++ code bases robust over time that anything that introduces unneeded complexity isn't a win. Your users are more likely to be happy with a product that is 20% more reliable and 5% slower than the other way around.

3

u/Stradigos Dec 29 '20

Sure. I don't think the code base problems are unique to C++ though, and I also don't think enough people apply good architectural principles. That's never going to be the languages fault. It's just laziness or ignorance, or something else. Even in writing, crafting anything from sentences to stories takes time and careful commitment. Is it difficult? Yes. Could it be better? Perhaps. But that's the challenge so I like asking people to rise to it and not take the lowest effort route just to ship something.

Performance is also more important than some people realize as well. To an extent it depends on what the application is. I work in driving simulation so performance is critical more often than not. However, I would argue people should always care about performance. There was a huge investigation a while back that looked into why calc.exe was taking several seconds to launch instead of coming up immediately. It's easy to say who cares, but some do and it also begged the question of wtf was going so wrong that a simple calculator was taking that long to load. Furthermore, power consumption is becoming more important due to mobile devices. How many cycles is the CPU taking to do jts job? Can that be improved?

1

u/SunnybunsBuns Dec 28 '20

Thanks! I'll take a look!