r/rust • • Jul 26 '19

Is this comment about the reference counting performance in Rust accurate?

/r/ProgrammerHumor/comments/b274zp/oof/eirvecs?context=3
52 Upvotes

52 comments sorted by

View all comments

Show parent comment

9

u/mdsherry Jul 26 '19

The Rust equivalent of unique_ptr is Box. shared_ptr can have some interesting optimizations where it doesn't bother using atomic counts until a new thread is started. That is, in a single-threaded program it behaves the same as Rc; in a multi-threaded one, Arc. (Compare to Rust, where you can reserve Arc for values that will cross between threads, and use Rc for those that remain in a single thread.)

2

u/simonask_ Jul 27 '19

std::shared_ptr also have some very interesting (or "interesting" if you like) features, where for example instances std::shared_ptr<T> and std::shared_ptr<U> can share the same control block (if T has unique ownership of an instance of U). These are clever, but can also introduce some very interesting debugging scenarios...