#垃圾回收 #垃圾 #清理 #标记 #收集 #gc

已撤销 goxoy-interpreter-broom

一个支持标记和清理垃圾回收的易用跟踪垃圾回收器

1 个不稳定版本

0.0.1 2023年5月7日

#11#清理

Apache-2.0/MIT

30KB
525

Broom

一个支持标记和清理垃圾回收的易用跟踪垃圾回收器。

Cargo Documentation License

特性

  • 易用API
  • 标记和清理堆清理
  • 即使有循环也能轻松(且安全)地修改堆值
  • 通过句柄以零成本访问堆对象

示例

use broom::prelude::*;

// The type you want the heap to contain
pub enum Object {
    Num(f64),
    List(Vec<Handle<Self>>),
}

// Tell the garbage collector how to explore a graph of this object
impl Trace<Self> for Object {
    fn trace(&self, tracer: &mut Tracer<Self>) {
        match self {
            Object::Num(_) => {},
            Object::List(objects) => objects.trace(tracer),
        }
    }
}

// Create a new heap
let mut heap = Heap::default();

// Temporary objects are cheaper than rooted objects, but don't survive heap cleans
let a = heap.insert_temp(Object::Num(42.0));
let b = heap.insert_temp(Object::Num(1337.0));

// Turn the numbers into a rooted list
let c = heap.insert(Object::List(vec![a, b]));

// Change one of the numbers - this is safe, even if the object is self-referential!
*heap.get_mut(a).unwrap() = Object::Num(256.0);

// Create another number object
let d = heap.insert_temp(Object::Num(0.0));

// Clean up unused heap objects
heap.clean();

// a, b and c are all kept alive because c is rooted and a and b are its children
assert!(heap.contains(a));
assert!(heap.contains(b));
assert!(heap.contains(c));

// Because `d` was temporary and unused, it did not survive the heap clean
assert!(!heap.contains(d));

此crate适用于谁

  • 编写Rust动态类型语言的程序员,想要一个简单、可靠的垃圾回收器
  • 想要具有复杂图数据结构、可变性和循环,但又不想内存泄漏的程序员

此crate不适用于谁

  • 编写普通Rust代码时想要垃圾回收的程序员

性能

此crate对性能没有具体承诺。它采用“尽力而为”的方法;这意味着它应该足够快,足以满足大多数用途,但可能不如那些投入了多年开发工作的垃圾回收器具有竞争力。

待办事项

如果有时间,我想在 broom 上做一些事情

  • 比标记和清理更智能的清理策略
  • 部分清理以防止垃圾回收延迟峰值

如果你对从事这些工作中的任何一项感兴趣,请随时提交一个pull request!

许可证

Broom受以下其中一项许可

依赖项

~1MB
~14K SLoC