fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.42k stars 112 forks source link

Recipe for Rc? #84

Open rw opened 4 years ago

rw commented 4 years ago

Is there a recipe for creating an Rc with bumpalo? I'm trying to avoid the heap allocation(s) that happen inside of Rc.

(For reference, using the Rc::from_box function still appears to cause heap allocations.)

Thanks!

fitzgen commented 4 years ago

There would need to be a version of Rc that is specialized to bumpalo or (eventually) Rc would need to be parameterized by an allocator (and the allocator's lifetime).

This is sort of similar to bumpalo::boxed::Box, and how it is different from std::boxed::Box.

But backing up a bit: why do you want to use an Rc with bumpalo? The lifetime of objects allocated in bumpalo means that the Rc wouldn't be 'static and that the contents would live as long as the bump arena lives, so it seems like using &T is equivalent but cheaper since it doesn't do reference counting operations.

rw commented 4 years ago

@fitzgen Thanks for the reply!

As to your question, I may have gotten ahead of myself. What I thought I wanted was bumpalo::rc::Rc<RefCell<MyType>>. Are you suggesting that something like &'bump RefCell<MyType> would work instead?

fitzgen commented 4 years ago

Are you suggesting that something like &'bump RefCell<MyType> would work instead?

Yep!

fitzgen commented 4 years ago

If you need to run MyType's destructor, then you'd pass around &'bump bumpalo::boxed::Box<RefCell<MyType>>