4 ms·
> the Rc type does not support being sent between threads So why even have such a thing in a language designed for concurrent programming from the ground up?
by kazinator 3y ago
> the Rc type does not support being sent between threads
So why even have such a thing in a language designed for concurrent programming from the ground up?
Arc should be called Rc, and that's it.
- LegionMammal978 3y agoWhy not have such a thing, when it is strictly more performant for any program that has data which isn't accessed concurrently?
- kazinator 3y agoBecause then you need to complicate the compiler with a diagnostic against misuse, which has to work 100% right in all situations and be maintained forever.
- tialaramex 3y agoBecause Rust has a safety culture, and provides threading, it is crucial that that the compiler will reject types you cannot safely send to another thread. So it does. So the "diagnostic against misuse" you're concerned about is a necessary part of the compiler anyway. Indeed, although Rc has this line: impl<T: ?Sized, A: Allocator> !Send for Rc<T, A> {} (which means roughly "You can't send this type to another thread") It also has these lines: // Note that this negative impl isn't strictly necessary for correctness, // as `Rc` transitively contains a `Cell`, which is itself `!Sync`.
- LegionMammal978 3y agoIt's not just Arc<T> vs. Rc<T> that's relevant for thread safety, though. Pretty much any kind of shared mutability requires extra protection (locks or atomicity) to work safely across threads, so there has to be some way to indicate whether or not that extra protection is present. Not to mention objects that interact with FFI, such as mutex locks, which must be unlocked from the same thread. It would be a huge performance drain to demand that "every value everywhere must be usable from every thread".
- FreeFull 3y agoNot every program that's written will even be multithreaded, and there's a significant cost to using atomic operations when you don't need them. What is the disadvantage of having non-atomic Rc be available?
- deleted 3y ago[deleted]
- skitter 3y agoAnd Rc can still be useful for multithreaded programs. Not every value needs to be shared between threads.
- deleted 3y ago[deleted]
- MaxRegret 3y agoThat's what C++ does because it has no way to ensure that you use the atomic reference counts in multi-threaded code. But, as the author writes in the blog post, Rust can in fact ensure this. So it lets you to use the more efficient non-atomic reference count for single-threaded use, saving the unnecessary cost of various memory access barriers. Just because a language is designed for concurrent programming, it shouldn't make it impossible to achieve full single-threaded performance, as long as you're not compromising safety.
- PaulDavisThe1st 3y agothis is a bit weird. if you have only 1 thread, you don't atomic, and thus not using atomic reference counts is fine but if you have more than 1 thread, you can't use a non-atomic refcount, so you can't use Rc but must use Arc. "but that's such a simple change, just change the decl with a 1 char addition! Pluse, Rust won't let you do bad stuff if you've forgotten to change the type". I guess I'm just old. Old enough that I've already implemented all the data structures and methods I need in C++, including safely passing around shared_ptr<T>.
- Rusky 3y agoIn Rust, you may have multiple threads yet still use non-atomic reference counts for objects that are never shared between multiple threads.
- kazinator 3y agoAnd then hit annoying roadblocks when you do want to pass those objects between threads? You should write code to minimize the reference count bumps; they are waste of time whether atomic or not. If the code spends 0.5% of its time bumping references, and you magically reduce that to zero using alien optimization technology, that only gives you a 0.5% improvement. If the code spends 10% of its time bumping references up and down, something is wrong.
- LegionMammal978 3y ago
- MarkSweep 3y agoSee the docs for Rc, including an example: https://doc.rust-lang.org/std/rc/index.html#examples https://doc.rust-lang.org/std/rc/index.html#examples