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
View all comments
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.