News for hackers. Stuff that matters.
Re:Garbage collected smart pointers in Rust via concurrent c... by bluGill on Friday December 13, 2024 @01:57PM (#42411257)
When smart pointers first came to C++ Herb Sutter gave a take where he claimed that 80% (numbers from memory as I don't want to rewatch several hours of cppcon videos to find the real claim) of the time you should unique_ptr, 15% shared_ptr - but that last 5% you need a mark and sweep garbage collector. I'm not a rust guy (I've only done hello world), but my understanding is it only gives you unique pointers - I've done enough C++ to state that over the base decade unique_ptr has always been enough for everything, though sometimes I've needed some thought to make it work in weird situations, while shared_ptr would have just worked.

There are known real world data structures that are sometimes useful (though I've never encountered a need to use them) where because you have circular references to things you have to use a mark and sweep garbage collector. If you happen to need such a thing in your Rust code than this will be useful.

Re:Garbage collected smart pointers in Rust via concurrent c... by zozbot234 on Friday December 13, 2024 @02:01PM (#42411299)
Rust does give you reference-counted shared pointers, both within a single thread (Rc<>) and across multiple threads (Arc<>). But reference counting does not properly clean up objects in the presence of reference cycles: that's when proper tracing GC may be required.
Re:Garbage collected smart pointers in Rust via concurrent c... by Yoric on Friday December 13, 2024 @02:10PM (#42411374)
To complete your post and the parent: most Rust applications don't need GC. Lifetimes, Box and Rc or Arc will generally amply cover all your needs.

For a few applications, though (e.g. a JavaScript VM, or something that deals with complex graph structures with unpredictable lifetimes), a GC is a lifesaver. Even in these applications, most of the data structures won't need GC pointers. Rust has a few GCs at hand, and lets you opt-in to GC specific pointers. In theory, this means that the GC can be extremely fast, because the graphs it needs to walk will be much smaller than in languages with a general-purpose GC. The cost, of course, is that you, as a developer, need to pay attention to what needs a GC and what doesn't.

Re:Garbage collected smart pointers in Rust via concurrent c... by wongarsu on Friday December 13, 2024 @02:21PM (#42411481)
> The cost, of course, is that you, as a developer, need to pay attention to what needs a GC and what doesn't.

But even there, lifetimes make your life easier. If lifetimes work out on their own you know the object will be dropped at some point (at the end of the lifetime). Only when you opt out of unique_ptr-like behavior you have to consider whether Rc/Arc will work (90% of the cases), will work with some special care by using some weak references, or whether you will require a GC

Re:Garbage collected smart pointers in Rust via concurrent c... by gf000 on Friday December 13, 2024 @02:23PM (#42411498)
Tracing GC has much better throughput than reference counting GCs (yeah, they both are GC algorithms), especially when compared against Arc<>, which would be quite expensive if many objects were making use of it, due to the synchronization overhead. (Note: I'm not saying that Rust would be slow or anything, as you can precisely go as low-level as you wish and circumvent the price of any form of GC. But if you were to have a lot of Arc objects around, e.g. you use them to represent stuff the user can also manipulate and thus have completely opaque lifetimes, then the overhead can become non-negligible)

Tracing GCs can do most of the work completely independently of the user code, and in parallel. Also, only live objects are touched, which can be also beneficial for certain kinds of applications.

Re:Garbage collected smart pointers in Rust via concurrent c... by zozbot234 on Friday December 13, 2024 @02:33PM (#42411584)
> Note: I'm not saying that Rust would be slow or anything, as you can precisely go as low-level as you wish and circumvent the price of any form of GC.

Rust can also circumvent some of the overhead of Arc<> as it can update the reference count whenever a true "owner" is added or removed, as opposed to just any reference to the object. But yes, you're right that relying on automated reference counting as the default memory management strategy involves some overhead, and tracing GC may be preferable to that. This is a big issue wrt/ Swift.

Re:Garbage collected smart pointers in Rust via concurrent c... by loeg on Friday December 13, 2024 @05:04PM (#42412765)
> I'm not a rust guy (I've only done hello world), but my understanding is it only gives you unique pointers

Some very rough analogies:

  C++        : Rust
  unique_ptr : Box
  shared_ptr : Rc or Arc
  references : references
Re:Garbage collected smart pointers in Rust via concurrent c... by zozbot234 on Friday December 13, 2024 @06:03PM (#42413131)
A C++ reference is more like a Rust &UnsafeCell<T> in practice - and there are thorny issues around the possibility of null references in C++ (which are supposed to be insta-UB per the C++ standard but are allowed by most compilers) that introduce additional questions about what the best mapping might be.
Re:Garbage collected smart pointers in Rust via concurrent c... by umanwizard on Friday December 13, 2024 @10:32PM (#42414472)
One key difference between Rust smart pointers and C++ ones is that Rust ones can’t be null. So C++ unique_ptr is more like an Option<Box> that automatically calls unwrap_unchecked every time you try to use it.
Re:Garbage collected smart pointers in Rust via concurrent c... by loeg on Saturday December 14, 2024 @01:15AM (#42415040)
Fair enough.