Writing Efficient C++ Code (2013) (asawicki.info)
asveikau 3 hours ago
stackghost 2 hours ago
Beyond the low-hanging fruit like ensuring you aren't creating O(n^2) complexity by accident, I think C++ is fast enough/has mature-enough compilers that by the time you're worrying about cache hits materially affecting performance, you're probably also sufficiently staffed and capitalized to pay people to A/B test that performance.
srean 2 hours ago
stackghost 30 minutes ago
asveikau an hour ago
I think it's more like: prioritize cache locality over big O compexity.
jeffbee an hour ago
spider-mario 2 hours ago
bluGill 2 hours ago
Run your own benchmarks on your own data of course. Also map is not considered the best key value store.
mandarax8 2 hours ago
This likely won't be true in a real application with a non-trivial allocation pattern.
einpoklum an hour ago
And then, on the other hand - I really doubt GP's map beats a vector, with all of those pointers bins and stuff, in a non-contrived benchmark with 10 elements.
Finally - it's not either-or: There are better hash maps whose memory is sequentially allocated and/or are otherwise cache-aware. And there are data structures geared towards parallel execution on multiple threads; and towards SIMD; etc. etc.
bluGill an hour ago
Still a custom map that allocated a bunch of nodes would be a useful optimization.
Jeaye 2 hours ago
I feel like the DoD movement is a slow-moving, but big, change through how systems programming is done, but that there's still insufficient material for how to do this in different scenarios. I would really like to apply this more to my areas of work, which are also in C++, but there seems to be a gap between what they're presenting and how it can be applied.
More specifically, I'm using C++ to build a dynamic programming language runtime for a Clojure dialect. That runtime is required to be garbage collected, type-erased, and highly polymorphic. So I surely can't just SoA or AoS everything. Yes, I can pack my data, and I can avoid the GC whenever possible, both in compiler/runtime code and in generated code via escape analysis. But what about everything else, which is the 80% or more of the system? It could be that this runtime is too far at odds with DoD, but I generally see things as a gradient rather than black and white.
hn_submit 6 hours ago
cjbgkagh 5 hours ago
gbin 5 hours ago
serbuvlad 2 hours ago
If your device has enough resources to power V8, modern GUIs are certainly very pleasant and snappier than a more minimal GUI like HN. Otherwise they are horrendous and very laggy.
gbin 2 hours ago
serbuvlad 43 minutes ago
QtQuick/QML/JS is very pleasant and I do wish more people would use it, but from I've seen it's 25-50% the resource use of electron, not some multi-order-of-maginute improvement, do I understand why many people still prefer electron for portability in this case.
glouwbug 5 hours ago
cenamus 5 hours ago
glouwbug 4 hours ago
The next step of going SOA benefits from all of the above, it just further unlocks you packed quad and oct instructions (AVX256 and 512 depending if you buy AMD or not).
tcfhgj 4 hours ago
creata 4 hours ago
vlovich123 3 hours ago
jasode 3 hours ago
GCC 11 (2021) std::visit was slower than virtual dispatch.
GCC 12 (2022) optimized std::visit so it can be faster than virtual dispatch.
https://shubhankar-gambhir.github.io/posts/your-stdlib-imple...
flowerbreeze 5 hours ago
hn_submit 2 hours ago
Even if you write them in hand-optimized assembly they would still clamor for more speed.
hackrmn 5 hours ago
The kicker is, in my case I chose C++ because templates allow me to reuse most of the code in the rendering pipeline _regardless_ of whether I go for AoS or SoA layout. I leverage operator overloading to do vector by matrix multiplication which is implemented in both variants. I do have to specify the desired variant during building, but I've profiled and for Intel x86 AVX in my case SoA is something like twice as efficient because I process ("shade") 8 vertices with 4-5 instructions instead of 1 vertex at a time (still shaded with vectorisation -- just "rotated", i.e in the pipeline axis and not vertex buffer axis).
TL;DR; C++ gives you plenty fast by default, but it's not always enough. The difference between 5 and 15 frames per second, well, makes all the difference -- our eyes are only fooled once the frames-per-second rate goes sufficiently up, anything below an acceptable threshold and it's completely different experience. You then either sacrifice resolution or level of detail etc, or decide to squeeze more from the language by helping the compiler.
mathisfun123 4 hours ago
8n4vidtmkvmk 4 hours ago
loeg 4 hours ago
creata 4 hours ago
https://devblogs.microsoft.com/oldnewthing/20060731-15/?p=30...
https://learn.microsoft.com/en-us/archive/blogs/ricom/perfor...
oso2k 4 hours ago
https://web.archive.org/web/20250201145327/https://users.ece...
wat10000 3 hours ago
senderista 2 hours ago
bluGill 2 hours ago
Note that we started our project before Rust was an option. These days I would certainly look at rust to see if that would cover our 5% of the needs but now we have a lot of C++ and mixing rust with C++ is a pain.
einpoklum an hour ago
It is less pain than for most other languages, except for C. The pain is in exposing a C API for your C++ code. Then you build a library and you're set - because basically every language has the ability to call C code. Python, Rust, Java, etc. etc.
palata an hour ago
The painful part is to have to go through a C API (modern languages can express much richer APIs and of course there are different constraints on the different runtimes, e.g. GC).
The annoying part is that each language adds overhead (its runtime). I wouldn't call it painful (I don't have much to do about it), I say "annoying" just because I would rather minimise the amount of code I ship.
palata an hour ago
palata an hour ago
Similarly I like to do video stuff in C just because I call gstreamer/ffmpeg directly in C, rather than having to bridge everything.
aldanor 2 hours ago
blacklion an hour ago
How 100Gbit NICs could your filter through your stateful firewall at line speed? And with 64 byte packets?
spacechild1 an hour ago
In (soft) realtime audio programming, your audio callback might only have a time budget of 1.3 milliseconds. Everytime you exceed that limit, you'll hear a dropout. That's when you'll start to optimize the hell out of your program :)
MaxBarraclough 6 hours ago
High-performance programming is a big topic. The scope is far too broad for a single blog post, which naturally gives only cursory discussion of C++ and computer architecture. The article isn't bad considering, but I do think it's the wrong format. A blog series, or even a book, would be more fitting.
glouwbug 5 hours ago
creata 5 hours ago
MaxBarraclough 5 hours ago
What you've written mostly makes sense to someone who already has a solid understanding of SIMD and of C++ (although I can't say I follow all of it), but the target audience is people who don't. For them, each point needs a much lengthier explanation.
glouwbug 4 hours ago
A quick restrict example:
#define fn __attribute__((used))
fn void copy1(int* to, const int* from, const int size)
{
for(int i = 0; i < size; i++)
to[i] = from[i];
}
fn void copy2(int* to, const int* from)
{
constexpr int size = 1024;
for(int i = 0; i < size; i++)
to[i] = from[i];
}
fn void copy3(int* restrict to, const int* restrict from)
{
constexpr int size = 1024;
for(int i = 0; i < size; i++)
to[i] = from[i];
}
gcc test.c -c -O3 && objdump -d ./test.o
copy1 is 52 lines, copy2 is 28 lines, copy3 is 2 lines (just a call to memcpy).This is a good starting point for self teaching. The impact of your TLB, L1, and overall instruction count (with IPC) can further be measured with `./perf stat -d -d -d ./a.out`. If you want a quick rule of thumb, no instructions are fast instructions.
asveikau 3 hours ago
creata 5 hours ago
MaxBarraclough 5 hours ago
Jeaye 4 hours ago
MaxBarraclough 3 hours ago
creata's comment [0] mentions the works of Agner Fog, which seem very good, and are freely available.
I haven't read C++ High Performance [1] but it looks like it covers the sorts of topics you'd expect, although it looks like it doesn't cover computer architecture in detail e.g. branch prediction. There are books on that too, of course.
[0] https://news.ycombinator.com/item?id=49868657
[1] https://www.packtpub.com/en-us/product/c-high-performance-97...
112233 7 hours ago
fooblaster 6 hours ago
beached_whale 6 hours ago
There are so many things that are expressible in C++ now that could not be without writing much more code or using per-compilation tools back then. The ability to run code at compile time that is not run at runtime is huge, #embed lets us make other tools output available without linker scripts or compiler specific tools that.
Also, most of the code from the past still works(from 10 years ago definitely works)
AlotOfReading 6 hours ago
cjbgkagh 6 hours ago
jandrewrogers 5 hours ago
112233 3 hours ago
On one hand, you have consteval and stuff, letting you FINALLY initialize data at compile time (hey, 20 years late but still!)
on other hand, it is done in most non-debuggable way possible. try setting breakpoint or adding print to constexpr function that causes your requires clause to fail...
so no, newer C++ the language is not possible to use for low level work. The dialects that compiler makers support are. We will see for how long
jll29 6 hours ago
Or take constexpr - it permits to move computations to compile time that are complex and in older versions either had to be done at runtime, or an ugly workaround had to be used (e.g. assigning a mysterious literal pre-computed in another run or by hand).
creata 5 hours ago
What C++23 feature allows that?
cherryteastain 5 hours ago
aw1621107 4 hours ago
aw1621107 4 hours ago
[0]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p27...
[1]: https://herbsutter.com/2025/11/10/trip-report-november-2025-...
jandrewrogers 5 hours ago
So many complex, esoteric, and difficult to maintain incantations that used to be required for efficient code generation are no longer necessary.
112233 3 hours ago
senderista 2 hours ago
FpUser 5 hours ago
uwagar 4 hours ago
tom_ 3 hours ago
hydrocephalitic 3 hours ago
bee_rider 3 hours ago
So, if you are thinking about the sort of “business logic” that’s often Python, but the performance comes from the parts that are usually not.