kyren / gc-arena

Incremental garbage collection from safe Rust
Creative Commons Zero v1.0 Universal
436 stars 36 forks source link

Simplest possible forward write barriers. #104

Closed kyren closed 23 hours ago

kyren commented 4 days ago

This has some additional drive-by cleanup to make things more consistent. It adds two methods to Mutation, one for backwards write barriers (previously the only kind) and one for forward write barriers.

Both backwards and forward write barriers are changed to take one or optionally two pointers: the parent and the adopted child. If both pointers are given, then the barriers check if the parent is black and the child is white, and if so, invoke the barrier. The backwards write barrier will turn the parent gray, and the forwards write barrier will turn the child gray.

If the backwards write barrier is only given the parent pointer, then it unconditionally turns it gray if it was black, allowing it to adopt any pointers at all. If the forwards write barrier is only given the child pointer, then it unconditionally turns it gray if it was white, allowing it to be adopted by any possible pointer.

This is based on #103 because it requires Gc::erase to be less obnoxious to use.

kyren commented 4 days ago

There is a separate commit for forward write barriers on arbitrary Collect impls, dc95fb1473ec7c9c9f0b99ab4a8e96174e68b32f.

It works fine and is sound (and is what @moulins and I had discussed originally as a MVP for forward barriers), but the API is a bit footgunny.

I left the simpler version of Mutation::forward_barrier there for simplicity when only dealing with Gc pointers (besides not having the slight "wrong reference" risk, Gc::trace does not panic, so there is also no panic risk).

If for some reason we decide we don't like the the "trace immediately" version of forward barriers, we can drop this commit.