danielfranca danielfranca - 3 months ago 15
C++ Question

How C++14 memory model works internally and how it compares to Bacon's Unified Theory of Garbage Collection

Reading David Bacon's Unified Theory of Garbage Collection
I found him mentioning that modern GCs are an hybrid of tracing and reference counting ones:


Given the similarity in structure that we discovered between tracing
and reference counting, we began to re-examine various collector
architectures to understand the interplay between these styles of
collection. We observed that all realistic garbage collectors are in
fact some form of hybrid of tracing and reference counting. This
explains why an optimized “tracing collector” and an optimized
“reference counting collector” become more and more similar: because
they are in fact taking on characteristics of each other.


One of the things he mention is the use of ZCT (Zero Count Table) to keep track of objects referenced from stack.
Another thing usually mentioned about ARC is:


  1. It's not thread-safe as the increment is not atomic

  2. If it's thread-safe it's slower than a GC



My questions:
How modern C++ (11/14/17) compare to this statements? Is this true that the ARC of C++ is hybrid and also use some elements of a tracing GC? I couldn't find anything on the web that points me to this, but the paper states very clearly that


We observed that all realistic garbage collectors are in
fact some form of hybrid of tracing and reference counting.


Is this true about modern C++? Or it's not considering a C++ ARC a "realistic garbage collector"?
Some people could argue that C++ has no GC, C++ has no tracing GC, but ARC is an approach to GC as it's stated on the paper:


Tracing and reference counting are uniformly viewed as being
fundamentally different approaches to garbage collection that possess
very distinct performance properties. We have implemented
highperformance collectors of both types, and in the process observed
that the more we optimized them, the more similarly they behaved —
that they seem to share some deep structure.


And is also presented on different other sources, like wikipedia
here and here


Reference counting is a form of garbage collection whereby each object
has a count of the number of references to it. Garbage is identified
by having a reference count of zero. An object's reference count is
incremented when a reference to it is created, and decremented when a
reference is destroyed. When the count reaches zero, the object's
memory is reclaimed.


Also, there's any benchmark of modern C++ ARC against a GC allocation/deallocation (note: I'm not asking for a general comparison but a specific to memory management).

Last but not least, if I'm developing a single-thread application what's the advantage of having increment/decrement as atomic operations on C++? There's anyway to disable that?

Answer

How modern C++ (11/14/17) compare to this statements?

There's no comparison. C++ never had, and does not have, any kind of automatic garbage collection of no longer referenced objects.

Is this true about modern C++?

No, it's not true. C++ does not have garbage collection.

Some people could argue that C++ has no GC,

There's no argument there. It's a fact. You will not find any kind of a description of any kind of a garbage collection implementation in the 1400+ pages of techical specifications that define the current C++ standard. It's not there. I looked.

C++ has no tracing GC, but ARC is an approach to GC as it's stated on the paper:

You are certainly free to come up with your own implementation of garbage collection, for your own classes, in C++. But that wouldn't make that a part of the language itself.

And, no, std::shared_ptr, et. al., is not garbage collection, even though it's reference counted. Hint: circular references.

what's the advantage of having increment/decrement as atomic operations on C++?

They, much less everything else in C++, has no inherit "advantage" of its own. This is like asking "what is the advance of having a car". For some people, owning a car makes their life easier. For others, it gives no benefit at all.

Similarly, atomic reference counting has advantages in specific use cases. For others it offers no advantages at all.

The correct question here would be "what are the advantages of having atomic increment/decrement versus [some alternative] in [a specific use case]".