4 ms·
Shameless plug: I ported the code to Rust: https://github.com/baguette/rs-babygc https://github.com/baguette/rs-babygc (feedback is welcome).
by groovy2shoes 11y ago
Shameless plug: I ported the code to Rust: https://github.com/baguette/rs-babygc https://github.com/baguette/rs-babygc (feedback is welcome).
- Manishearth 11y agoYou should be able to use RAII to pop things from the stack so that it mirrors the actual stack. I think. Otherwise, looks pretty good. Interestingly, this doesn't contain any unsafe code, probably because Rc and RefCell are doing the actual memory/safety management. Still, the purpose of this exercise is to focus on the mark-and-sweep part, and that seems to have been done pretty well here!
- groovy2shoes 11y agoAll the GC implementations I've seen in Rust use unsafe code, and I really wanted to avoid it. I struggled a bit with some of the RefCell stuff, but the folks on #rust were really helpful. I'm still not sure that a practical GC can be implemented without unsafe, given that RefCells can't be recursively borrowed (so as soon as you have mutable recursive data structures, you have to find some way to work around the RefCell limitation that the contents can only be mutably borrowed once). I wonder if it wouldn't be nice to have something similar to a RefCell that allows multiple mutable borrows, which I think would be safe (only) if the application is single-threaded. Or perhaps a variant that mutably borrows only non-RefCell bits of the contents, leaving the nested RefCell free to be mutably borrowed. Thanks for your feedback :) I'd had it sitting there for a while with no idea if it was any good or not.
- steveklabnik 11y ago> I think would be safe (only) if the application is single-threaded. Unfortunately, it's not that easy: http://manishearth.github.io/blog/2015/05/17/the-problem-with-shared-mutability/ http://manishearth.github.io/blog/2015/05/17/the-problem-wit...
- groovy2shoes 11y agoGood to know. Thanks.
- Manishearth 11y agoIn fact, RefCell can't even be shared between threads, it _has_ to be used in a single threaded context. RefCell and the "no mutable aliasing" isn't something to be "worked around", it's something to embrace, because safety and many other things come from it.