Isn’t it usually “use raw pointers, if you have to”?
Isn’t it usually “use raw pointers, if you have to”?
So, "use smart points, if you have to", should have been: "if you need to allocate memory dynamically, use smart pointers".
Oooh. Okay. :p I was like, "As opposed to what, raw pointers?!"
Technically this works and it's reasonably safe, because RAII takes care of memory management, just like GC takes care of memory management in Java.
But the problem there is deeper: it's not smart pointers vs raw pointers, but that each small object lives in its own heap allocation.
That's the typical scenario where you will see memory management becoming a performance problem (for at least two reasons: (1) heap allocation and especially deallocation isn't free, and (2) lots of cache misses when accessing objects that are spread more or less randomly over the address space).
Ideally you'd group many similar objects into long-lived and compact arrays, process the data in those arrays in tight loops, minimize pointer indirections, and minimize allocations (ideally you'd only allocate those arrays once at program start, or when those arrays need to grow). Once you did all those things, memory managament suddenly becomes a non-problem (because you only have a handful of long-lived allocations to care about), and RAII becomes a lot less useful (because the 'objects' in those arrays will most likely just be C-style plain-old-data structs). This is basically the 'antithesis' to OOP. And before you know it you're back to writing C code, like it happened to me ;P
TL;DR: there is no silver bullet for automatic memory management if performance matters.
Are you saying smart pointers and there overhead make the heap allocations even bigger and that’s the cause of the cache misses?
> But the problem there is deeper: it's not smart pointers vs raw pointers, but that each small object lives in its own heap allocation.
Smart-pointers do encourage to create small heap allocations though (because unlike raw pointers they manage ownership).
IMHO from gamedev: HN is not going to get it ever. They either have moved to high level languages or do not need performance at all.
1. https://sean-parent.stlab.cc/presentations/2013-09-11-cpp-se...