Broom: An ergonomic tracing garbage collector

栏目: IT技术 · 发布时间: 5年前

内容简介:An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection.This crate makes no specific promises about performance. It is designed with a 'best attempt' approach; this means that it should be fast enough for most purposes but is

Broom

An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection.

Features

  • Ergonomic API
  • Mark and sweep heap cleaning
  • Easy (and safe) mutation of heap values, despite cycles
  • Zero-cost access to heap objects through handles

Example

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));

Who this crate is for

  • People writing dynamically-typed languages in Rust that want a simple, reliable garbage collector
  • People that want to have complex graph data structures with mutation and cycles but who don't want memory leaks

Who this crate is not for

  • People that want garbage collection when writing ordinary Rust code

Performance

This crate makes no specific promises about performance. It is designed with a 'best attempt' approach; this means that it should be fast enough for most purposes but is probably not competitive with garbage collectors that have had years of development work ploughed into them.

TODO

There are a few things I want to do with broom if I get the time:

  • Smarter cleanup strategies than mark 'n sweep
  • Partial cleans to prevent garbage collection lag spikes

If you're interested in working on any of these things, feel free to open a pull request!

License

Broom is licensed under either of:


以上所述就是小编给大家介绍的《Broom: An ergonomic tracing garbage collector》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

设计之下

设计之下

搜狐新闻客户端UED团队 / 电子工业出版社 / 2014-1-1 / CNY 69.00

形而上者谓之道,形而下者谓之器。匠者,器也。处身平凡的匠人不断追求向上的设计之道。本书没有华丽的辞藻和长篇大论的理论,作者是搜狐一线的设计团队,写作过程中他们尽力还原真实的工作场景,并总结出了一些实用的经验和方法。 《设计之下》共三部分,全面讲解了用户体验设计的流程和方法。第一部分为“交互设计”,阐述了从项目启动、解析需求到原型设计的过程,并且总结了交互设计的要点:大局观、操作流程简捷、形式......一起来看看 《设计之下》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换