r/cpp • u/no-bugs • Jun 19 '26
Efficient C++ Programming for Modern 64-bit CPUs, Chapter 4/part 2
Here comes the 2nd installment of (VERY DRAFT) Chapters from my (and Dmytro Ivanchykhin's) upcoming book, "Efficient C++ Programming for Modern 64-bit CPUs". Comments are extremely welcome (as before, we're committed to fixing all the issues highlighted in comments).
Second part of Chapter 4 (the one on CPU Physics and CPU Cycles): https://6it.dev/blog/infographics-operation-costs-in-cpu-clock-cycles-take-2-80736 . In addition to some interesting data (in particular, micro-research on the progress of MUL/DIV ops since 2017), it has that visualization of the different times quite a few ppl here have asked for.
DISCLAIMERS:
- it is VERY DRAFT (editing is coming)
- this is not a book on optimizations (though some techniques will be covered in Appendices A and B in Vol. 2) - this is a book on de-pessimizations; for optimizations - please refer to the excellent book by Denis Bakhvalov (though we're sure that de-pessimizations should be seen as a prerequisite for optimizations 😉).
15
u/schmerg-uk Jun 19 '26
The MUL/DIV point is well made but I notice you focus on integer operations only, which is fair, but the general point still applies (if different numbers) to floating point operations.
And pipelining of DIV is much worse than pipelining of MUL, so where a MUL may take 3 cycles, the typical modern chip can dispatch (initiate) two MUL operations (each of which can be SIMD for floating point) on each and every cycle
In contrast the DIV pipeline is awkward and (from memory) the chip can't dispatch another DIV for 7 or so cycles after a previous DIV, so not only is the operation latency slower, but the rate of dispatch even without dependencies is much worse.
Again you may address pipelining elsewhere but latency alone of operations such as these is less than half the picture IMHO (latency, throughput, and SIMD being the on-chip 3-way picture ignoring other issues such as cache/RAM latency and branch prediction) so if it's not explored in other sections, it may be worth at least a passing mention
6
u/g_0g Jun 19 '26
Damn, 150-300 cycles for a dynamic_cast is crazy (it should still be above Main RAM read at 200-300).
What does "[IgnatchnenkoEtAI]" after "alloc/dealloc pair" means?
3
u/ack_error Jun 19 '26
The low-level operation counts could use some more recent CPUs. Apple M1 can do integer division in ~8 cycles, for instance (ref). Oryon is similar although unfortunately there is no good reference for it. The difference between latency and throughput adds ambiguity, but the chart feels about 5 years out of date for low-level ALU ops. Multiplications are now almost at the same dirt-cheap cost as additions, and divisions are getting considerably faster (~several muls). Square roots have also been getting faster to the point that even simple approximations are now marginal.
The C++ Exceptions section mentions the shift to zero-cost exceptions, but doesn't mention cases where they are not actually zero cost. One of them is noexcept disabling tail calling, since a noexcept function must have a frame on the call stack to be visible to the unwinder unless its presence can be statically deduced.
Regarding the cost of indirect and more generally non-inlinable calls, another issue is the cost of establishing a new stack frame. This can require the caller to save more registers so it can move its data into non-volatile registers. Depending on ABI this can be substantial, with dozens of vector registers needing to be spilled and reloaded. The actual performance cost, though, is highly variable which makes it difficult to boil down to a nice cycle count range.
11
u/OneInchPunchMan Jun 19 '26
Hey, just wanted to ask why the sidebar on the left occupies ~30% of the screen on desktop? It's a bit distracting. Also, mid you this is personal preference, I think the extensive use of AI images does more harm than good to the website. There is a sea of AI generated content and on first glance I don't think I would spend more than 5 seconds looking at the site (thinking it was AI, without knowing)
2
u/chkmr Jun 19 '26
Tangential question: that table of latencies looks very familiar... Is this website the same IT hare website that I saw a few years ago when the illustrations were actual hares instead of this bunny lady? I don't recall it being called something like 6it.
2
u/Tringi github.com/tringi Jun 20 '26
Little nitpick about acronyms: You unpack what CAS means, but not LL/SC.
3
u/elperroborrachotoo Jun 19 '26
Excellent! I've been looking for an updated chart of this kind for a long time.
(Nitpick: it's too full to be readable withotu pan-and-zoom on 1080p)
1
u/pilpulon Jun 22 '26
where can we follow the progress of the book and announcements when it is released?
1
u/jonathanhiggs Jun 23 '26
On Exceptions vs return value: i would really appreciate including ‘std::expected’ comparisons thrown into the mix here given it is the ‘modern’ approach to structured error handling