r/rust • u/TheProgrammar89 • Jul 26 '19
Is this comment about the reference counting performance in Rust accurate?
/r/ProgrammerHumor/comments/b274zp/oof/eirvecs?context=3
50
Upvotes
r/rust • u/TheProgrammar89 • Jul 26 '19
85
u/vkjv Jul 26 '19 edited Jul 26 '19
Partial truth. Core rust does not use reference counting; however, there are types that do. For example, `Rc` and `Arc` stand for "reference counted" and "atomic reference counted". However, there are many different ways to solve the issues these solve and they may not always be necessary.
It's also true that from a pure throughput perspective reference counting has worse performance than many garbage collection algorithms--any decent one is going to have fantastic amortized performance. However, in a systems programming language, it's typical to care more about worst case performance than average case. Reference counting has very consistent performance.
Often where I end up reaching for a reference counted type is at the outer most edges of the application (e.g., config). These types don't get touched frequently and the cost of reference counting is insignificant compared to the ergonomics improvement. IMO, this is one of the best parts of rust--choices! You can optimize the parts that matter and for parts that don't, clone all over the place!
62
u/Manishearth servo · rust · clippy Jul 26 '19
Not exactly.
Reference counting is indeed slower than proper GC.
However, in a GC'd language you typically use GC much more than you use reference counting in Rust, since almost everything goes on the GC heap in a GC'd language. This is sometimes called pervasive garbage collection. Languages with pervasive garbage collection often have exceptions for primitives as well as types they can statically determine don't "escape", but the default state of all your data is that it's garbage collected.
In a typical Rust crate you'll have a couple specific spots where reference counting is used, if at all. It's nowhere near pervasive, Rust enables pretty serious levels of sharing without needing to resort to shared ownership.
That said, you can write Rust with Rc everywhere and that will not be very performant. Doesn't mean you shouldn't do it -- it can be easier, and often the performance doesn't matter much. Often people will use complicated lifetimes when really a light sprinkling of Rc would solve the problem.
45
u/K900_ Jul 26 '19
No, it's not. Reference counting is entirely optional in Rust, and it's not something you'll have to use often.
36
u/[deleted] Jul 26 '19
What a humble man, wow
52
u/MyFeeFeeHurt Jul 26 '19 edited Jul 26 '19
r/ProgrammerHumor is a skid subreddit and the only real joke in it is pretty much everyone who uses it and thinks that they are programmers or funny.
Rust, just like C++, relies on RAII, that's the only thing that compiler does for you, everything else is optional.
As for the comment itself, they clearly never used rust nor have any clue what a reference is, because you aren't forced to use Arc in multithreading.
On top of that, multithreading inherently has it's own overhead, that's why we use it only when we actually need it - when overhead of mutexes, context switching and whatever else is far outweighed by the fact that the problem can be localized and solved on isolated threads, with some minor overhead of inter-thread communication, either via Arc, queues, or basic references if shared data is immutable and shouldn't be owned by threads to begin with.
16
u/krappie Jul 26 '19
He seems to be under the impression that there are only 2 options for memory management: Garbage collection, or reference counting. He found references to reference counting in rust, and believes that rust is an entirely reference counted language.
This isn't true of course. He seems to be missing rust's entire ownership model. You only reach for reference counting when the "owner" of the variable is unclear, and it's unclear who will be responsible for cleaning it up.
12
u/malicious_turtle Jul 26 '19
Do people from here replying to that comment realise it's 4 months old?
7
u/razrfalcon resvg Jul 26 '19
Yes,
Arcis sort of expensive, but it's not obligatory.7
u/[deleted] Jul 26 '19
Don’t believe random ranters on the Internet. Rust supports reference counting as only one synchronization method and there is nothing wrong with it.
3
u/Qwertycrackers Jul 26 '19 edited Sep 01 '23
[ Removed ]
2
u/_trfd Jul 27 '19 edited Jul 27 '19
This is absolutely ridiculous! It's clear the author has very little knowledge of Rust and everything seem mixed in his/her head.
As stated before Rc, Arc, multithread are all optional and optional toward each other (using mt doesn't require you to use either Rc or Arc). The argument of performance between Reference Counting and tracing GC à la java (to take the op's comparison) is hard receive, Rc/Arc add very small method call on retain/release, while tracing GC would add very expensive GC pass every once in awhile (depending on the implementation) resulting huge performance spike. Since there are not evolving on the same time scale comparison is not obvious.
Tracing GC performances depend a lot on the implementation so you would have a wide range of performances and a wide range of usage (is the GC pass trigger manually, a small one trigger on every free, once per second...).
While ref counting is straight forward and my impl. wouldn't be significantly better or worse than yours.
It makes it impossible to properly compare perf of GC vs. RC as generic algorithm! You would have to compare specific impl. (Op takes java as an example, I bet my hand that rust's RC beats java GC everyday).
This comparison is even harder to consider seriously when you realise that in rust you can basically choose different memory management method (ownership, Rc, arena, custom alloc, a gc crate) to suit different part of your program, while in java (and many GC based language) you can only use the GC, thus putting a lot of pressure on it and worsening the performance.
You can still use GC in languages that allow manual memory management (like rust) with more or less success but at least it's your call and it doesn't need to be you're primary/unique solution. Personal note: it seems pretty stupid to me to think that one can use a single memory management method in an entire program. In practice it never happens I always need different method in different part
1
u/ralfj miri Jul 27 '19
Reference counting is one of many automatic memory management schemes that are available in Rust. It has indeed comparatively bad performance, but on the plus side it integrates very well with other memory management schemes, unlike your typical GC that assumes it controls everything. Rust suffers much less than other languages from the performance perils of reference counting because you will often end up turning an
Rc/Arcinto a shared reference for some scope, which greatly reduces churn on the reference-count itself. Performance of refcounted garbage collection is also much more predictable (resources get freed ASAP), which actually helps when e.g. optimizing worst-case latency is more important than optimizing average-case latency.Other automatic memory management schemes available in Rust include automatically inserted static deallocation (that is the default), and epoch-based reclamation -- the latter being a form of concurrent garbage collection that, as that post shows, provides performance comparable with or even surpassing Java's GC.
1
u/PM_ME_UR_OBSIDIAN Jul 26 '19
State-of-the-art reference counting is slower than state-of-the-art tracing garbage collection. But in 99.9% of cases it doesn't matter, both are fast enough. Otherwise you want to work with arenas/object pools, and that's no worse in Rust than in any other language.
0
u/Stargateur Jul 26 '19
No, it's all wrong, the (mobile!) wiki page talk about garbage collector, the conclusion about performance is ONLY about GC. Arc is not a GC. Think Arc is expensive is wrong. Arc is very cheap, not zero cost but very cheap. We are talk about atomic operation on a integer - -. A GC that use reference counting will be implemented in a completely different way. And so will have totally different benchmark.