3 ms·
> People recommend tracing garbage collection because it performs better than atomic reference counting, even though reference counting is much simpler. Refere
by eru 3y ago
> People recommend tracing garbage collection because it performs better than atomic reference counting, even though reference counting is much simpler.
Reference counting is a lot more complicated! It turns every read into a write.
> [...] if my goal is performance and predictable latency?
Reference counters don't have predictable latency: a single reference decrement can lead to an arbitrary amount of follow up work, like more decrements and deallocations.
Performance of reference counting isn't generally better than that of garbage collectors. (Especially since you still need a backup solution to deal with cycles.)
Figuring out a lot of your memory management at compile time is definitely a win. But doesn't really have much to do with reference counting.
- sph 3y ago> Reference counting is a lot more complicated! It turns every read into a write. Why? Maybe it's crucial to the story, but I am exploring Rust-like single ownership semantics. You would have to "write" only when you create a new reference to something, or the reference is destroyed. But it wouldn't turn any read into a write, no?
- hu3 3y agohttps://news.ycombinator.com/item?id=32276580#:~:text=One%20of%20the%20biggest%20issues,increment%20the%20object's%20reference%20count https://news.ycombinator.com/item?id=32276580#:~:text=One%20....
- steveklabnik 3y agoYou don’t do this in Rust. Up u can take a normal &T into something held by an Rc<T>. The count will not be touched. It’s only when you need an additional owning reference that you call clone() and increment the count, or when one of the owners goes out of scope, and Drop decrements the count. But for non-owning cases, which are many of them in most code, the count stays the same.
- hu3 3y agoThat has a cost both to the compiler and to the developer. It's not a free win.
- steveklabnik 3y agoIt’s part of the standard library, not the language, so it’s not really a cost to the compiler. And this just uses all the same rules as normal Rust code, so like, sure, to use features you have to know them, but I find it a bit silly to frame that as a “cost.” If that’s a cost, so is literally everything. Anyway my main point is just that you have less refount traffic than in other implementations.
- hu3 3y agoIf were going down the calling silly route, I'd rather categorize handwaving of Rusts additional complexities by their community as silly. If "everything has a cost" anyway we might as well code is assembly for optimal performance. But no, because nuances and differences of cognitive load during development can be significant. And so does compilation speed. It's arguably much easier to code with a GC and language adoption reflects that.
- cesarb 3y ago> I'd rather categorize handwaving of Rusts additional complexities by their community as silly. I believe what steveklabnik meant, was that all this extra complexity (the borrow checker) already exists in the language for other reasons. It's not an additional cost to the compiler or the developer when using the reference-counted types (Rc or Arc).
- steveklabnik 3y agoThat's correct. I thought we were talking specifically about refcount traffic, not about broader issues.
- 3y ago
- marcosdumay 3y agoRust ownership works because it runs entirely at compile time. Rust has infrastructure for runtime inference counting, but you need to actively use it, and it's complicated. Also, every time you read a pointer you are creating a new reference. That's what turns "every read into a write". You are correct that it's not actually every read, it's just that every small block that reads the value will also write over it.
- jstimpfle 3y ago> Reference counters don't have predictable latency: a single reference decrement can lead to an arbitrary amount of follow up work, like more decrements and deallocations. You can push the cleanup work on a queue to do it asynchronously, to bound the amount of work per deref / to even the work out.
- eru 3y agoYes, definitely, you don't have to use the simplest version of reference counting, you can soup it up. But you can play the same games with GC. So this is not specific to reference counting. There are incremental and real time versions of GC.
- cesarb 3y ago> It turns every read into a write. Not exactly; it turns every passing of a reference, even when it's going to be used only for reading (or not used at all, or just passed on further) into a write of the reference count. But if you can read without passing the reference to somewhere else (or if the compiler can prove that the reference the caller has outlives the reference it would have passed), it can omit the write. And tracing garbage collection is not completely immune to this; some kinds also turn every read into a write (into a card marking table), copying gc periodically rewrites unmodified objects (when moving them during garbage collection), and the tracing itself might need to read cold objects which might even be located on disk (swapped out). > Reference counters don't have predictable latency: a single reference decrement can lead to an arbitrary amount of follow up work, like more decrements and deallocations. However, they have a predictable place for that latency: it can only happen when there's a reference decrement (and the amount of extra work at that point is bounded by the size of the graph which can be reached from that reference). It also will not affect other unrelated threads. With a stop-the-world GC, a thread which allocates too much can cause pauses in threads which are not even allocating or releasing memory at that moment. > Performance of reference counting isn't generally better than that of garbage collectors. (Especially since you still need a backup solution to deal with cycles.) However, the memory use of reference counting is generally better than that of tracing garbage collectors, since it releases memory as soon as it can be released. That is, tracing garbage collectors trade extra memory use for their speed; giving them more memory (up to a point) means they can do less work, and giving them too little extra memory can make them much slower. > Figuring out a lot of your memory management at compile time is definitely a win. But doesn't really have much to do with reference counting. It does, because it can help reduce the cost of reference counting (by eliding most of the reference count manipulation when it's not necessary).
- eru 3y ago> With a stop-the-world GC, [...] Yes, if you compare an implementation of reference counting that doesn't stop the world with an implementation of GC that does explicitly stop the world, you will find that GC stops the world more.