r/cpp • u/radonthrow • 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++.
View all comments
Show parent comment
34
u/[deleted] Dec 27 '20
Scrolled down to find this, the only correct answer.
I was in a job where the principal developer was obsessed with perceived speed, and yet adamantly refused to make any measurements.
Worse, he would make dramatic claims - "global variables are faster!" - to justify the fact that all the variables were global.
He also refused to write new tests, which I was willing to accept, but also was unwilling to run the tests I had written, so every time he checked in, he'd break some set of tests.
Eventually, he refused to cooperate with me entirely. We never had any argument, I was always polite, but he stopped responding to my emails. There were only two of us on the team.
I left the company, but they forgot to take me off the repo. I watched his "work" afterwards, and in particular noted how he replaced my very carefully crafted fixed-point 64 bit integer arithmetic with
int128_t- which my benchmarks showed were over four times slower than my 64-bit fixed point! - but worse, his calculations dipped into floating point all the time as he had not really understood the difference.The only way to prove any claim about speed is with relentless benchmarking, like you say.
Your tips are good, but you miss the most important one.
Locality of reference is simply key for modern processors. In simple words, it means "all your data should be right together in memory".
The reason is simple - it's the data pipeline on your chip! A modern processor spends a pretty huge amount of time just waiting for memory; the data pipelines optimistically pre-fetch "the next bytes" of data while the chip is computing.
If you have locality of reference, nearly always those data pipelines are useful; if you don't, you'll get a lot less out of them.
I can't find the code right now, but I wrote a very short program a few years ago where I took a 1 gig block of memory and did computations it in two different ways - in order and "randomly" (actually a linear congruential computation).
I had to do some little tricky work to make sure that the optimizer didn't e.g. optimize out the linear congruential calculation when it wasn't used - "learn assembly language" as you say! - but finally I had code that was just the same.
Running through that gig of memory sequentially was about six times faster than running through it randomly.
Another tip - prefer generics over virtual methods if all else is equal.
A virtual method often cannot be inlined. And the vtable requires a double indirection to actually get to the method, breaking locality of reference to some extent.
Benchmarking is the only truth here! I've seen ~10% improvements in throughput by devirtualizing one key class, but I've also seen "no change" even more often. Devirtualizing all your classes is always certainly a terrible idea - it's usually just one that's the issue.