Simple tracing (mark and sweep) garbage collector for Rust
\nThe design and motivation is illustrated in this blog post, with a sketch of the code in this gist.
\nThere is another post about the initial design of cgc
, its experimental concurrent branch.
To include in your project, add the following to your Cargo.toml:
\n[dependencies]\ngc = { version = \"0.5\", features = [\"derive\"] }
This can be used pretty much like Rc
, with the exception of interior mutability.
While this can be used pervasively, this is intended to be used only when needed, following Rust's \"pay only for what you need\" model. Avoid using Gc
where Rc
or Box
would be equally usable.
Types placed inside a Gc
must implement Trace
and Finalize
. The easiest way to do this is to use the gc_derive
crate:
use gc::{Finalize, Gc, Trace};\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n x: Option<Gc<Foo>>,\n y: u8,\n // ...\n}\n\n// now, `Gc<Foo>` may be used
Finalize
may also be implemented directly on the struct, in order to add custom finalizer behavior:
use gc::{Finalize, Trace};\n\n#[derive(Trace)]\nstruct Foo {...}\n\nimpl Finalize for Foo {\n fn finalize(&self) {\n // Clean up resources for Foo, because we think it will be destroyed.\n // Foo may not be destroyed after a call to finalize, as another\n // finalizer may create a reference to it due to reference cycles.\n }\n}
For types defined in the stdlib, please file an issue on this repository (use the unsafe_ignore_trace
method shown below to make things work in the meantime).
Note that Trace
is only needed for types which transitively contain a Gc
, if you are sure that this isn't the case, you may use the unsafe_empty_trace!
macro on your types. Alternatively, use the #[unsafe_ignore_trace]
annotation on the struct field. Incorrect usage of unsafe_empty_trace
and unsafe_ignore_trace
may lead to unsafety.
use gc::{Finalize, Gc, Trace};\nuse bar::Baz;\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n x: Option<Gc<Foo>>,\n #[unsafe_ignore_trace]\n y: Baz, // we are assuming that `Baz` doesn't contain any `Gc` objects\n // ...\n}
To use Gc
, simply call Gc::new
:
let x = Gc::new(1_u8);\nlet y = Gc::new(Box::new(Gc::new(1_u8)));\n\n#[derive(Trace, Finalize)]\nstruct Foo {\n a: Gc<u8>,\n b: u8\n}\n\nlet z = Gc::new(Foo {a: x.clone(), b: 1})
Calling clone()
on a Gc
will create another garbage collected reference to the same object. For the most part, try to use borrowed references to the inner value instead of cloning the Gc
wherever possible -- Gc
implements Deref
and is compatible with borrowing.
Gc
is an immutable container. Much like with Rc
, to get mutability, we must use a cell type. The regular RefCell
from the stdlib will not work with Gc
(as it does not implement Trace
), instead, use GcCell
. GcCell
behaves very similar to RefCell
, except that it internally helps keep track of GC roots.
#[derive(Trace, Finalize)]\nstruct Foo {\n cyclic: GcCell<Option<Gc<Foo>>>,\n data: u8,\n}\n\nlet foo1 = Gc::new(Foo {cyclic: GcCell::new(None), data: 1});\nlet foo2 = Gc::new(Foo {cyclic: GcCell::new(Some(foo1.clone())), data: 2});\nlet foo3 = Gc::new(Foo {cyclic: GcCell::new(Some(foo2.clone())), data: 3});\n*foo1.cyclic.borrow_mut() = Some(foo3.clone());
- \n
- Destructors should not access
Gc
/GcCell
values. This is enforced by theTrace
custom derive automatically implementingDrop
with a safe empty drop method.Finalize
should be used for cleanup instead. \n - There needs to be a better story for cross-crate deriving. \n
- The current GC is not concurrent and the GCed objects are confined to a thread. There is an experimental concurrent collector in this pull request. \n
- \n
- RuScript: Uses single-thread
rust-gc
to allocate memory for various objects \n
\n\n- \n
\n \n 17\n \n \n \n \n $\\begingroup$\n If you use Rust to implement a different language, then the fact that Rust - the host language - does not use a GC, does not imply anything about whether the language you are implementing will have a GC or not. That's a language design decision for you, the language designer, to make.\n $\\endgroup$\n \n– kaya3\n \n Sep 27, 2023 at 18:00\n \n \n \n - \n
\n \n 4\n \n \n \n \n $\\begingroup$\n The top question here about GC is answerable, but the two labelled \u201cmy questions\u201d are different and very broad currently. Which part of this are you looking for an answer to here? You can ask separate questions for different topics, although the broad ones would need to be dialed in a bit more for people to be able to give you good answers.\n $\\endgroup$\n \n– Michael Homer\n♦ \n Sep 27, 2023 at 18:08\n \n \n \n - \n
\n \n 1\n \n \n \n \n $\\begingroup$\n Welcome to PLDI! One thing worth noting is that the things you hear about Rust regarding safety are typically relative to lower level languages like C/C++. Nim is likely to be similar in how safe it is (I don't know much about it tho).\n $\\endgroup$\n \n– Rydwolf Programs\n \n Sep 27, 2023 at 18:09\n \n \n \n - \n
\n \n 1\n \n \n \n \n $\\begingroup$\n Nim has a GC (you can choose from multiple GCs, actually, or no GC if you want), so if you make an interpreter in Nim and don't do any unsafe stuff, you shouldn't need to implement garbage collection yourself. I'm not sure why you mentioned Nim and Rust specifically, because any language that has either a GC or borrow checking like Rust can be used to implement an interpreter without requiring another GC, as long as you don't do something unsafe that the GC or borrow checker don't know about/can't handle.\n $\\endgroup$\n \n– user\n \n Sep 27, 2023 at 18:17\n \n \n \n - \n
\n \n 3\n \n \n \n \n $\\begingroup$\n You should think about whether your language implementation is as an interpreter or with a compiler \u2014 you didn't say, and it can make a difference.\n $\\endgroup$\n \n– Erik Eidt\n \n Sep 27, 2023 at 19:01\n \n \n \n \n \n \n\n
\n\t