News for hackers. Stuff that matters.
Re:Chris Lattner on garbage collection vs. Automatic Referen... by zarzavat on Saturday April 23, 2022 @10:12PM (#31140668)
In the modern sense of the word, a GC is an algorithm that manages memory automatically and without cycles. A GC must maintain the property that a leak is only possible if an object is reachable from a root. RC on its own does not obey this property.

Reference counting is not GC in the modern sense unless it also includes a cycle collector. So Swift is not GC’d (RC only) yet Python is GC’d (RC+CC).

Re:Chris Lattner on garbage collection vs. Automatic Referen... by Kranar on Saturday April 23, 2022 @10:43PM (#31140816)
> A GC must maintain the property that a leak is only possible if an object is reachable from a root. RC on its own does not obey this property.

This is certainly a false claim then. What you're describing is a precise garbage collector, but there also exist conservative garbage collectors which will fail to reclaim objects even when no cycle exists so long as any integer interpretation of currently allocated memory shares the same integer interpretation of an object's memory address.

GNU Java, and the Mono implementation of .NET both use the Boehm conservative garbage collector and they are most certainly still considered garbage collected systems:

https://en.wikipedia.org/wiki/Boehm_garbage_collector

Re:Chris Lattner on garbage collection vs. Automatic Referen... by zarzavat on Sunday April 24, 2022 @12:36AM (#31141332)
This isn’t formal science. We are practitioners and so definitions have to be practical. If Boehm GCs really leak as much as you claim then nobody would use them.

The issue with cycles is that it can easily lead to significant memory leaks if a large subgraph gets caught in a cycle. A definition of GC that excludes this capability does not have the contract of “You only have to worry about reachability from roots”.

Re:Chris Lattner on garbage collection vs. Automatic Referen... by Kranar on Sunday April 24, 2022 @01:43AM (#31141578)
>This isn’t formal science.

Computer science is absolutely a formal science in every sense of the word.

>If Boehm GCs really leak as much as you claim then nobody would use them.

I mean if anything this argument applies more to your characterization that reference counting easily leads to memory exhaustion due to cycles than it does to conservative garbage collectors. If RC based garbage collectors leaked as much as your post implies then nobody would use them. And yet Swift, a language that is almost exclusively used with large graphs(that's how most UIs are represented in memory) and that exhibits all kinds of non-deterministic shared ownership uses RC based garbage collection. Objects involved in UI code are shared in wildly unpredictable ways with no clear hierarchy among layouts, event handlers, observables, IO systems, etc...

>A definition of GC that excludes this capability does not have the contract of “You only have to worry about reachability from roots”.

Exactly, because that's not part of the contract for a GC. That's part of the contract for a tracing garbage collector:

https://en.wikipedia.org/wiki/Tracing_garbage_collection

When your definitions don't fit reality then it's time to adjust your definitions, not reality.

Re:Chris Lattner on garbage collection vs. Automatic Referen... by knome on Saturday April 23, 2022 @10:32PM (#31140776)
>RC on its own does not obey this property.

It is sufficient in a functional language that lacks haskell's lazy capacity to "tie the knot". Erlang's data structures, for example, are fully immutable, and it is not possible to create a cyclic data structure. Since there cannot be a cyclic data structure, reference counting would be sufficient to manage its memory automatically.

It would still suck since it has to constantly write to all objects being referenced, but it would work.

Re:Chris Lattner on garbage collection vs. Automatic Referen... by cmrdporcupine on Saturday April 23, 2022 @11:09PM (#31140958)
There are RC systems where the counts are held external to the objects, which can be a bit more cache friendly.
Re:Chris Lattner on garbage collection vs. Automatic Referen... by Kranar on Sunday April 24, 2022 @01:45AM (#31141581)
You want the opposite behavior in order to be cache friendly, an RC system where the counts are stored next to the object.
Re:Chris Lattner on garbage collection vs. Automatic Referen... by cmrdporcupine on Sunday April 24, 2022 @08:56AM (#31143502)
Not necessarily. The issue is that releasing a reference at the top of a tree of objects has the potential to walk a large graph of objects (doing downcounts at each) that are not in cache, causing some thrash.

In an ideal world and in a program running with a not-huge # of objects you could keep the entire basket of reference counts in L1 cache, separate from the objects. Then decrements would be potentially cheap. A bit utopian though.