This is two translations of the simple mark-sweep GC described in Baby's First Garbage Collector
into Rust. One version uses unsafe
while the other does not. The unsafe GC is a close to
literal translation of the original C, with the notable exceptions that it uses NonNull<T>
pointers instead of raw pointers, and stores pointers to all live objects in a Vec
instead
of using an intrusive linked list. The safe GC is implemented by allocating all objects in a
Vec
owned by the VM and using indices into the Vec
as pointers. As a result, all
operations can be done using only safe Rust.
Additionally, this repository contains the original C source and rust bindings for it generated by rust-bindgen.
Preliminary benchmarking has shown the safe GC to be about 2-3x faster than the unsafe GC and about 8x faster than the original C version. As far as I can tell, this is because of slight differences in the algorithms used. In short, the safe GC has the least indirection of the three. So there's nothing magic here, but these results still bode well for the possibility of writing a production quality garbage collector in safe Rust.