7 ms·
A Tour of Safe Tracing GC Designs in Rust
- kgeist 5y ago>In Go, the compiler needs to automatically insert code that checks the “pulse” of the heap every now and then, and potentially runs garbage collection. It also needs to automatically insert code that can tell the scheduler “hey now is a safe time to interrupt me if a different goroutine wishes to run” Is it still the case after Go 1.14 (February 2020) when asynchronous preemption was implemented? As far as I know, goroutines are now preempted using signals instead of inserting checks at every function call.
- uluyol 5y agoThe signal based mechanism is only used for stragglers. Go still uses the function call check. The runtime devs found a way to combine it with a check to see if the goroutine's stack needs to be resized, so it doesn't actually cost anything.
- pjmlp 5y agoHaving something like this available as vocabulary types would be quite nice for GUI libraries.
- mjw1007 5y agoSome history of the question of whether Rust should have GC as standard: 2014-09. RFC 256 https://rust-lang.github.io/rfcs/0256-remove-refcounting-gc-of-t.html https://rust-lang.github.io/rfcs/0256-remove-refcounting-gc-... « Remove the reference-counting based Gc<T> type from the standard library and its associated support infrastructure from rustc. Doing so lays a cleaner foundation upon which to prototype a proper tracing GC » Contemporary with that: https://news.ycombinator.com/item?id=8312327 https://news.ycombinator.com/item?id=8312327 nikomatsakis: « I wouldn't be so quick to give up on GC support yet! "We have a plan!" But I don't think we'll have it in for Rust 1.0. » 2015-04: https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.h... "Memory safety without garbage collection." is now advertised as a "key value proposition" (this isn't quite the same as saying "we're not thinking of ever adding garbage collection", of course). 2016-08: https://manishearth.github.io/blog/2016/08/18/gc-support-in-rust-api-design/ https://manishearth.github.io/blog/2016/08/18/gc-support-in-... « Recently we (Felix, Niko, and I) have been working on getting compiler-level GC support for Rust. The plan is to provide a base set of APIs and intrinsics on which GCs can be built, without including an actual GC itself. » This is the "Unfinished design for a builtin Rust GC" described in TFA. 2018-10: the introductory post for the shifgrethor library (described in TFA): https://boats.gitlab.io/blog/post/shifgrethor-i/ https://boats.gitlab.io/blog/post/shifgrethor-i/ includes a bolded « I do not expect we will ever “add GC to Rust” ».
- dgb23 5y agoIt’s amazing how open and organic this evolved.
- amelius 5y agoWouldn't it make more sense to have garbage collectors at the level of LLVM? That way, you could have direct access to all the pointers in a program without jumping through the language's hoops. And other languages could benefit from it too.
- joppy 5y agoYes, it makes total sense for the garbage collector to be tightly coupled to the memory allocation system, which is also tightly coupled to code generation (as is done in the case of various high-performance managed languages: Java, C#, Haskell, …). I remember hearing a talk a few years ago about a system of the kind you’re talking about - a kind of “managed language” micro-core that ran alongside LLVM so that it could hook into code generation, and was intended to be a kind of toolkit for making new managed languages. Can’t remember the name…
- an_opabinia 5y agoThe performance is still too bad for anything but Java on real applications. The other killer feature, ahead of time compilation, is really clunky, and its most innovative change, static initialization at build time by default, was reverted to runtime initialization.
- duped 5y agoThat sounds a lot like Graal
- saurik 5y agoThe concept of what a pointer "is" is somewhat language-dependent, particularly as people finally attempt to mitigate the waste of moving to 64-bit by using tagged pointers (which maybe could try to be modelled in LLVM also, but I think you will rapidly realize that LLVM just isn't modeling a lot of things that affect scanning).
- tialaramex 5y ago> particularly as people finally attempt to mitigate the waste of moving to 64-bit by using tagged pointers It's very fraught to try to "mitigate the waste" at the top of the pointer. Everybody involved is clear that what you're doing there is saving up pain for yourself because periodically more of those "wasted" bits become significant and each time that happens if you've been using them now you've incurred a maintenance burden. The waste at the bottom of the pointer is a far more sensible place to hide information because it won't get taken away when somebody ships a new CPU, it was already there in 32-bit architectures. LLVM itself makes use of the latter trick extensively as I understand it, but doesn't use the former. If they've got a 64-byte structure, they can align them, then steal six bits at the bottom of each pointer to those structures for bit flags, and when Intel ships a new CPU their code still works. Whereas if I steal ten bits at the top of the pointer, when Intel ships Ice Lake CPUs my compiler now crashes. Good luck debugging that!
- boredprograming 5y agoOne day, Rust needs a GC. Reference counting is a just crappy GC. Modern GC can perform better than this so Rust is actually hurting its own performance by not having one. A good GC would make heavily concurrent apps much easier to build with Rust. And would have better performance than the typical Arc Mutex objects passed around right now
- _flux 5y agoTracing GC is troublesome for any non-memory resource, such as network connection or file handle, due to its untimely release, but otherwise I actually agree: reference counting is a GC mechanism—not a very good one, but it's the only one I'm aware of that works both for memory and resources. I would enjoy someone test a model where the type system guarantees (or at least lets you detect the situation) that you cannot store such non-memory objects behind a traced GC node (these would include plain memory objects that need to be registered/unregistered precisely). It might be that it would be needlessly annoying to use just compared to just RC. Or maybe it would be best of both worlds?
- pjmlp 5y agoNot when the language also supports value types and region allocators (e.g. IDispose in .NET). You can even turn it into RAII proper, by turning into a compilation error not handling those interfaces properly. Again with .NET, there are SafeHandles as well, alongside the MarshalInterop APIs. This is nothing new actually, Mesa/Cedar for Xerox PARC used reference counting with a cycle collector, while other descendent languages down to Modula-3 and Active Oberon always combined value types, tracing GC and C++ like resource handling capabilities. Oh Common Lisp also has similar capabilities, specially the ZetaLisp predecessor from Lisp Machines. Then Eiffel not only had this, it was also probably the first Algol like language to support non nullable references. Sadly they decided to ignore all of this in Java, and then its world domination kind of made everyone else ignore it as well. Thankfully even Java is improving their story in this regard, while languages like D, Nim and yes .NET kind of show what was already available for several decades.
- 5y ago
- zozbot234 5y agoThe OP does not seem to mention CactusRef https://crates.io/crates/cactusref https://crates.io/crates/cactusref which provides deterministic deallocation (on par with simple refcounting) of reference cycles, with minimal tracing requirements and minimal memory overhead.
- sizediterable 5y agoProbably because cactusref was only published 5 days ago and this article is from three months ago...
- pcwalton 5y agoIt requires unsafe.
- zozbot234 5y agoIt used to require unsafe. The project has been reworked to add safer alternatives (at the cost of some increased overhead).
- tomp 5y agoDo you know of any link to a description of how it works? These claims seem to me like very vague and very hard to deliver…
- celeritascelery 5y agoFor me, the most interesting one is Josephine[1]. This allows tracking of stack roots, doesn't limit the time when GC can run, and takes advantage of rust's affine type system. However I have to say the API feels fairly clunky, with the need to root everything before mutation and the need to pass context objects to every method. [1] https://github.com/asajeffrey/josephine https://github.com/asajeffrey/josephine
- aaron_m04 5y agoGreat article. What I really want to know is what GC crate is best for writing a Lisp runtime with.