▲ | Aurornis 3 days ago | ||||||||||||||||
> thus effectively switching to automatic garbage collection Arc isn't really garbage collection. It's like a reference counted smart pointer like C++ has shared_ptr. If you drop an Arc and it's the last reference to the underlying object, it gets dropped deterministically. Garbage collection generally refers to more complex systems that periodically identify and free unused objects in a less deterministic manner. | |||||||||||||||||
▲ | ninkendo 3 days ago | parent | next [-] | ||||||||||||||||
Also importantly, an Arc<T> can be passed to anything expecting a &T, so you’re not necessarily bumping refcounts all over the place when using an Arc. If you only store it in one place, it’s basically equivalent to any other boxed pointer. | |||||||||||||||||
▲ | Arnavion 3 days ago | parent | prev | next [-] | ||||||||||||||||
>Garbage collection generally refers to more complex systems that periodically identify and free unused objects in a less deterministic manner. No, this is a subset of garbage collection called tracing garbage collection. "Garbage collection" absolutely includes refcounting. | |||||||||||||||||
| |||||||||||||||||
▲ | hansvm 3 days ago | parent | prev | next [-] | ||||||||||||||||
That's fair. It's not really a good pattern though. You get all the runtime overhead of object-soup allocation patterns, syntactic noise making it harder to read than even a primitive GC language (including one using ARC by default and implementing deterministic dropping, a pattern most languages grow out of), and the ability to easily leak [0] memory because it's not a fully garbage-collected solution. As a rough approximation, if you're very heavy-handed with ARC then you probably shouldn't be using rust for that project. [0] The term "leak" can be a bit hard to pin down, but here I mean something like space which is allocated and which an ordinary developer would prefer to not have allocated. | |||||||||||||||||
| |||||||||||||||||
▲ | bluGill 3 days ago | parent | prev | next [-] | ||||||||||||||||
Reference counting has always been a way to garbage collect. Those who like garbage collection have always looked down on it because it cannot handle circular references and is typically slower than the mark and sweep garbage collectors they prefer. If you need a referecne counted garbage collector for more than a tiny minotiry of your code, then Rust was probably the wrong choice of language - use something that has a better (mark and sweep) garbage collectors. Rust is good for places where you can almost always find a single owner, and you can use reference counting for the rare exception. | |||||||||||||||||
| |||||||||||||||||
▲ | nayuki 3 days ago | parent | prev | next [-] | ||||||||||||||||
> Arc isn't really garbage collection. It's like a reference counted smart pointer Reference counting is a valid form of garbage collection. It is arguably the simplest form. https://en.wikipedia.org/wiki/Garbage_collection_(computer_s... The other forms of GC are tracing followed by either sweeping or copying. > If you drop an Arc and it's the last reference to the underlying object, it gets dropped deterministically. Unless you have cycles, in which case the objects are not dropped. And then scanning for cyclic objects almost certainly takes place at a non-deterministic time, or never at all (and the memory is just leaked). > Garbage collection generally refers to more complex systems that periodically identify and free unused objects in a less deterministic manner. No. That's like saying "a car is a car; a vehicle is anything other than a car". No, GC encompasses reference counting, and GC can be deterministic or non-deterministic (asynchronous). | |||||||||||||||||
▲ | jcelerier 3 days ago | parent | prev | next [-] | ||||||||||||||||
> Arc isn't really garbage collection. It's like a reference counted smart pointer like C++ has shared_ptr. In c++ land this is very often called garbage collection too | |||||||||||||||||
▲ | jandrewrogers 3 days ago | parent | prev [-] | ||||||||||||||||
This still raises the question of why Arc is purportedly used so heavily. I've written 100s of kLoC of modern systems C++ and never needed std::shared_ptr. | |||||||||||||||||
|