News for hackers. Stuff that matters.
Re:Garbage Collection for Systems Programmers by kccqzy on Saturday March 30, 2024 @09:32PM (#39880512)
The reason Rust has both Arc and Rc types for reference counting is precisely because most of the time when you need reference counting, you do not need thread safety. This is something I think about all the time in C++ when I use std::shared_ptr: it gives me thread safety using atomics but I don't need it.

More languages should distinguish between thread safe reference counting and single-threaded reference counting.

Re:Garbage Collection for Systems Programmers by bsder on Saturday March 30, 2024 @11:56PM (#39881266)
> The reason Rust has both Arc and Rc types for reference counting is precisely because most of the time when you need reference counting, you do not need thread safety.

Hmmm, I'm curious about your use cases as this is almost precisely opposite my experience. Normally, I regard Rc as a footgun as I've always found that I'm shortly going to have to change everything to Arc.

Re:Garbage Collection for Systems Programmers by jandrewrogers on Sunday March 31, 2024 @12:25PM (#39885611)
I almost never use atomic reference counters, when I use them at all. A major point of using thread-per-core software architectures is that it eliminates almost all atomics even in highly concurrent contexts that may require reference counting to manage resource lifetimes.

In these cases, your concurrency primitive is essentially a coroutine, either with or without its own stack. You may have thousands of these executing concurrently similar to as if they were multithreaded and using locks or atomics, but the execution is always on a single thread so locks are unnecessary and atomics can be elided entirely. In the rare cases where atomics are used, there are almost always at most two threads accessing them.

Basically, the only place you see atomics are the very thin and lightly accessed interfaces between threads, which are about resource control rather than true thread concurrency.

I have used these types of architectures on diverse high-scale server apps. The places I’ve had to use atomic reference counters were always extremely limited and largely outside the hot path.

Re:Garbage Collection for Systems Programmers by bsder on Monday April 01, 2024 @04:43PM (#39899061)
Then why are you using Rc at all? This seems like "arenas" would be a much better match where you can reclaim everything in one fell swoop as soon as the thread finishes its task.

Is the issue simply that Rust until recently didn't support memory allocators other than the standard one?

Re:Garbage Collection for Systems Programmers by jandrewrogers on Thursday April 04, 2024 @12:48AM (#39926642)
The resource management doesn't fit an arena model, that is an overly simplistic view of what is required. The resources have a lifecycle independent of the references any execution context has with them. A reference isn't ownership, it just keeps the resource from disappearing or pins it in a particular context.

Memory allocations may not actually exist in memory. They can be paged to disk in user space because, frankly, the virtual memory hardware on modern CPUs is often incapable of handling large storage and when it does the performance is poor. Sure, you can do better than the default allocators in Rust and elsewhere but that is far from the only reason you might want a reference counter on a resource. DMA requires managing reference counts for threads of execution that only exist in silicon, not in software.

Arenas solve none of this. The lifecycle of the resource has nothing to do with the lifecycle of the execution context of a coroutine.

Re:Garbage Collection for Systems Programmers by kccqzy on Sunday March 31, 2024 @09:57PM (#39890154)
I use Rc inside the implementation of data structures such as persistent trees, and also to implement functionality such as Undo. The synchronization happens on a higher level.