GC Support in Rust


#1

We’ve been discussing adding hooks to Rust to make it easy to implement or integrate with GCs.

I’ve written a post illustrating the design we have so far.

Feedback welcome! We’d especially love to see some discussion about solutions for the unresolved problems mentioned in the post.


#2

Please, make sure that the design will support compacting and ideally multithreaded GCs.

Since you already have stackmaps (and register maps) in your design, compaction will just involve patching these locations/registers.

Multithreading is an order of magnitude more complex if objects can be accessed from multiple threads, though. It will require stopping threads that access GC objects at safepoints.


#3

Compacting should be doable, either by using double-pointers, or by doing the patching you mention. Will be tricky, but I don’t think it will be impossible.

Yes, you need some form of safepoint mechanism (which the implementor can add) for multithreaded gcs.


#4

Double-pointers will require more compiler support. Patching is easier (and still safe).

Safepoints are another issue, there are three major ways to implement them: memory protection games, code patching and polling. The first two can be implemented with zero overhead but are not available on all systems (e.g. XBox or iPhone). And polling requires runtime overhead, sometimes significant.

Then there’s a question of write barrier for card marking if a generational GC is needed.


#5

Double pointers require zero compiler support. When I say double pointers I just mean that Rust-side GC values are handles.

The safety of patching is questionable, because of noalias and the general safety issues around mutation. It can be implemented safely, but it’s not that straightforward.


#6

Do you need a Pinnable trait?


#7

No, you just need to use UnsafeCell properly.


#8

Simple double pointers will indeed help for single-threaded GC.

Why noalias is a problem? Stackmap should identify all the possible aliased locations (barring unsafe code).


#9

&-pointers aren’t put into the stack map. Though this could be a mode you switch on, I guess.