Crates.io | moving_gc_arena |
lib.rs | moving_gc_arena |
version | 0.3.3 |
source | src |
created_at | 2020-01-06 21:01:06.773071 |
updated_at | 2021-03-26 23:24:14.832633 |
description | Lightweight Garbage-collectable regions using indices and explicit roots |
homepage | |
repository | https://gitlab.com/jcarr0/moving_gc_arena |
max_upload_size | |
id | 195937 |
size | 101,950 |
This is a library for indexed regions supporting blazingly fast allocation, with efficient garbage collection, traversals, and deep cloning.
You should use this library if you want to keep a safe cyclic graph data structure, with simple, performant garbage collection. Access to GC operations exposes efficient traversals and cloning. This library does not read the Rust stack, instead, roots are acquired resources, which can be used like any other resource and dropped as normal. It compiles on stable 2018 Rust and requires only minimal unsafe code for most operations. Dereferencing indices uses a reference to the region, giving strong safety guarantees.
You should not use this library if you have very hard real-time requirements, and cannot handle amortized costs on the order of Vec-reallocation. In the current version, only single-threaded use is possible. Because of the algorithms chosen, this library is well-suited for frequent allocations, much like arena allocation.
use moving_gc_arena as gc;
let mut r = gc::Region::new();
struct Adj(Vec<gc::Ix<Adj>>);
impl gc::HasIx<Adj> for Adj {
fn foreach_ix<'b, 'a : 'b, F>(&'a mut self, mut f: F) where
F: FnMut(&'b mut gc::Ix<T>)
{
self.0.foreach_ix(f);
}
}
impl Adj {
fn new() -> Self {
Adj(Vec::new())
}
}
let mut obj1 = r.alloc(|_|{Adj::new()}).root();
let mut obj2 = r.alloc(|_|{Adj::new()}).root();
let mut obj3 = r.alloc(|_|{Adj::new()}).root();
// mutual cycle
obj1.get_mut(&mut r).0.push(obj2.ix());
obj2.get_mut(&mut r).0.push(obj1.ix());
// self-cycle
obj3.get_mut(&mut r).0.push(obj3.ix());
std::mem::drop(obj1);
std::mem::drop(obj3);
r.gc(); // manually-triggered collection
//obj3 now collected but obj1 and obj2 are live