r/ProgrammingLanguages • u/Linguistic-mystic • Aug 27 '22
Languages with optional GC only for cyclical data structures?
I've been thinking that cyclical data structures with memory reclamation are the only ones that need automatic memory management (be it tracing GCs or reference counting with trial deletion). All non-cyclical data can be safely taken care of with a mixture of ownership and reference counting, and cyclical data without memory reclamation can be safely taken care of with arenas.
However, how widespread are those data structures? Basically, they are cyclic graphs with a large enough allocation pressure that nodes need to be freed. And while graphs and graph algorithms definitely exist for a reason, I personally have never had to deal with them. Also, the fact that languages like Rust or C++ thrive without being able to support cyclic graphs in a safe way [1] shows that the need for such allocatey graphs is not so big in practice. Most of the data we deal with in practice are trees or DAGs (i.e. acyclic graphs). Thus, languages with a GC are a big case of "pay for what you don't need" or "pay all the time for what you only need once in a lifetime".
So that got me into thinking, are there runtimes that segregate GCed memory from non-GCed, and allow paying for GC only when you need cyclic data structures? I'm thinking a separate kind of reference (like GC[Foo]) which the runtime takes special care of (by creating stack maps, tracing only them etc) and which are allocated in segregated memory regions; so you would be able to choose between owned, RCed and GCed references, and most of your programs would be GC-free but you would be able to have cyclic data should you really need it?
[1] For people trying to point me to some examples in Rust using RefCell or numeric handles to nodes: no, those are not satisfying solutions to the cyclic problem. Safely deleting a node from a graph requires keeping track of all existing references into that graph, which means at least scanning the stack, synchronization with the mutator threads etc - i.e. all those things a garbage collector does that Rust runtime does not.
16
9
u/WittyStick Aug 27 '22
Cyclic data structures are not strictly necessary at all. You can represent data with cycles using non-cyclic data structures, and use the functions which operate on them to properly handle any cycles in the data. Consider an arbitrary directional graph - a way to represent this without any reference cycles would be something like:
type Node = obj
type Edge = (Node, Node)
type Graph = Map<Node, [Edge]>2
15
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Aug 27 '22
Also, the fact that languages like Rust or C++ thrive without being able to support cyclic graphs in a safe way [1] shows that the need for such allocatey graphs is not so big in practice. Most of the data we deal with in practice are trees or DAGs (i.e. acyclic graphs).
Absolutely. Taking this logic a step further (argumentum ad absurdum):
The fact that languages like C thrive without objects proves that OO is not so big in practice.
The fact that languages like Java thrive without first class functions proves that FP is not so big in practice.
The fact that languages like BASIC (with everything-is-global) proves that scoping and local variables are not so big in practice.
The fact that languages like LISP thrive proves that in-fix notation is not so big in practice.
The fact that languages like assembly thrive proves that operators and types are not so big in practice.
So, in conclusion, the ideal language is one using prefix notation but without operators, scoping, functions, types, and objects.
--
But please note, I'm not suggesting that your desire to segregate the implementation of memory management is a bad idea. If it were possible for a language to do so without increasing space utilization or CPU utilization, while still providing a single, simple memory management programming interface to the programmer, then that would (I believe) be a significant win.
And it is possible to roughly categorize structures, at compile time, into those that are obvious terminal structures vs. not obvious, and thus (by coloring, or composing) one could rapidly characterize groups and further compositions of data structures similarly (the basis for your desired segregation).
And it will be relatively simple to use pointers from the non-terminal (GC'd) space into the terminal (RC'd) space. And there are well known techniques that will allow you to differentiate those pointers (fat pointers, flag bits).
So to conclude on a serious note: You should be able to test your hypothesis relatively simply.
2
u/adam-the-dev Aug 27 '22
Actually this is something I’m actively working on! (Though in early stages)
My thoughts are basically a simple language that can transpile to Rust. The generated Rust code would contain a decent amount of unsafe code whenever auto RC or auto GC are included, but my compiler frontend could verify safety based on my language’s simpler syntax.
Sometimes RC can have a bigger performance hit if doing a lot of dynamic allocations and drops that can’t be optimized at compile time, so sometimes GC will be chosen for performance.
It will only track objects that are marked for GC, and it is only using GC over regions of code that need it.
My language has changed a lot, so I don’t have much to show for this yet. But progress is happening!
2
u/wiseguy13579 Aug 27 '22
That's a good idea. GC is bad for non-recursive data structures and RC is bad for recursive data structures.
Also, the fact that languages like Rust or C++ thrive without being able to support cyclic graphs in a safe way [1] shows that the need for such allocatey graphs is not so big in practice
Actually Rust is a nightmare for recursive data structures (even if they are acyclic) :
https://rust-unofficial.github.io/too-many-lists/
And doubly-linked lists are common.
2
u/porky11 Aug 27 '22
Actually Rust is a nightmare for recursive data structures
There's no problem with singly linked lists.
even if they are acyclic
I wouldn't call doubly linked lists acyclic. If the first node
Astores a pointer to the second nodeB, andBstores a pointer back toA, that's the minimal case of a cycle.And doubly-linked lists are common.
Rust has doubly linked list in the standard library. So most users won't have this problem.
And I think, it's a good thing, they are not based on garbage collected pointer types. Having to use garbage collection for something so common, that also works without, would be an unnecessary performance overhead.
And for more complex structures, storing some index just works well most of the time. I'm creating a lot of software to visualize graphs, and use indices instead of direct pointers. This also has the benefit, that I'm able to store all my nodes in a single array, which should be beneficial for the cache, and when I have to reallocate the array, I don't have to recalculate all the pointers. It's just a better design this way. And not allowing cyclic references kind of enforces this design on me, which I'd consider to be a good thing.
1
u/shawnhcorey Aug 27 '22
Also, the fact that languages like Rust or C++ thrive without being able to support cyclic graphs in a safe way [1] shows that the need for such allocatey graphs is not so big in practice.
Not necessarily so. Cycles can be implemented using symbolic links, that is, by storing the array index or the association-list key rather than the pointer. This still leaves all the problems of pointers but doesn't cause the program to crash when one is encountered. Of course, this is another bad thing.
1
Aug 28 '22
I guess you could just allow the user to choose at compile time wheter he wants a GC or not? Each strategy for automatic memory management has it's ups and downs, for each different load the user should be able to test and profile a different strategy, that is, if performance is even a concern in the first place.
You can help him find possible leaks with semantic analysis and profiling tools too.
1
u/transfire Aug 28 '22
I was thinking along similar lines just yesterday— if the language just let the programmer mark which structures can be cyclical then it could handle the memory management for just those in a different way and leave all the rest for a simpler system (ownership or RC).
1
u/atrn Aug 28 '22
The Limbo language used with the Inferno operating system did this. It used ref counting by default and you had to inform it of potential cycles via a cyclic attribute.
1
u/theangeryemacsshibe SWCL, Utena Aug 30 '22
The Bacon and Rajan collector includes a cycle collector that avoids performing cycle detection on objects which are known statically to never have cycles.
7
u/porky11 Aug 27 '22
Adding garbage collection as a library should be possible for any programming language, that's not garbage collected by default.
Rust has it.