by jgaa · 3y ago · 0 comments
I'm not saying that it's a bad idea. I'm just curious about why/when garbage collection is better than conventional use of unique_ptr or shared_ptr.
[1] Multithreaded Python without the GIL https://docs.google.com/document/d/18CXhDb1ygxg-YXNBJNzfzZsD... [2] Low-Latency, High-Throughput Garbage Collection https://users.cecs.anu.edu.au/~steveb/pubs/papers/lxr-pldi-2...
When one wants the flexibility of C++ with the productivity of a GC.
So far my experience has been that (nondeterministic) GC is only a productivity improvement over languages with manual memory management like C, but a productivity degradation over languages with deterministic automatic memory management like C++ and Rust. GC manages only memory, while in the latter case, you get automatic management of resources of any type, which can be a huge productivity boost if you use it to your advantage.
Also RAII is great and all, yet there lots of ways to get it wrong.
Badly implemented destructors, missing virtual destructors in hierachys, throwing exceptions on destructurs, not using handle types on heap allocated data, incorrect pairing of allocators.
> Badly implemented destructors, missing virtual destructors in hierachys, throwing exceptions on destructurs, not using handle types on heap allocated data, incorrect pairing of allocators.
Those seem to be more specific problems with C++ rather than RAII. Rust has none of those.
Firstly, that's orthogonal to my original point is that, for example, a semi-space nursery can significantly reduce the cost of short-lived allocations (often being faster than malloc/free on such a workload), but only if there are no destructors.
Secondly, threads typically share resources so running something on a different thread does not magically cause it to not disturb the mutator. Such a claim makes me start to doubt the "real-time" claim in the title...
tracked_ptr: 279.95ms (151MB)
shared_ptr: 1035.07ms (480MB)
source: https://godbolt.org/z/jP7ohPehY