3 ms·
Handling circular references is pretty important! Things like closing over an environment in a scripting language usually creates a cyclic reference, for exampl
by chc4 4y ago
Handling circular references is pretty important! Things like closing over an environment in a scripting language usually creates a cyclic reference, for example (stack frame contains a closure, which contains a reference to...the parent stack frame), or any graph-like data structures.
There is also just a cost to maintaining reference counts, which may be expensive if you are cloning and dropping values very often. A garbage collector can mean that cloning or destroying a reference has zero cost instead, along with batching destructors like you mentioned.
I'd also like to point out that technically reference counting is a garbage collection scheme! You can even use a cycle collector that runs occasionally over reference counted objects in order to free unreachable cycles; Python for example does this!
- kragen 4y agoin ur-scheme i put the variables that are captured by closures into heap-allocated cells; both the original stack frame and the closures contain pointers to these cells, eliminating the circular references, but adding an additional indirection step to accessing those variables theoretically this gives you more precise garbage collection, but it does give the collector more allocations to worry about it also allows you to use a traditional stack for your stack, rather than a bunch of heap-allocated things, and that's probably a win