Herb Sutter deferred heaps and pointers
I’ve seen a recently released talk by Herb Sutter, quite interesting (and not too much hard to understand even if you aren’t at the skills level of a C++ library designer), “Lifetime Safety By Default - Making Code Leak-Free by Construction - Herb Sutter - CppCon 2016”:
For a quicker overview of the talk you can find the slides here:
https://github.com/CppCon/CppCon2016/tree/master/Presentations/Lifetime%20Safety%20By%20Default%20-%20Making%20Code%20Leak-Free%20by%20Construction
The talk is about smart C++ pointers that help safety and correctness of C++ code. The unique_ptr<> is like Box<> of Rust, then there’s shared_ptr<>, etc.
In the second part of the talk Herb shows that to handle graphs and ownership cycles you can invent a new smart pointer, and he explains deferred_ptr, deferred_allocator and deferred_vector.
deferred_allocator creates a memory zone (a deferred_heap) where you can use cyclic pointers, like when you need to build a graph data structure. The pointers are handled with reference counter inside a deferred_heap. There is a basic implementation of those things:
https://github.com/hsutter/gcpp
I remember that recently there’s interest in introducing back a Gc<> type in Rust, perhaps those ideas Herb Sutter can be quite useful.
There are even notes about making the Rust compiler optimizer aware of some of that semantics:
The current implementation is not production-quality. In particular, it’s a pure library solution that requires no compiler support, it’s single-threaded, it dynamically registers every deferred_ptr, and it doesn’t try to optimize its marking algorithm. The GC literature and experience is full of ways to make this faster; for example, a compiler optimizer that is aware of deferred_ptr could optimize away all registration of stack-based deferred_ptrs by generating stack maps. The important thing is to provide a distinct deferred_ptr type so we know all the pointers to trace, and that permits a lot of implementation leeway and optimization. (GC experts, feel free to plug in your favorite real GC implementation under the deferred_heap interface and let us know how it goes. I’ve factored out the destructor tracking to keep it separate from the heap implementation, to make it easier to plug in just the GC memory and tracing management implementation.)