bumpalo icon indicating copy to clipboard operation
bumpalo copied to clipboard

Recipe for Rc?

Open rw opened this issue 3 years ago • 4 comments

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!

rw avatar Sep 03 '20 23:09 rw

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.

fitzgen avatar Sep 09 '20 20:09 fitzgen

@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?

rw avatar Sep 10 '20 19:09 rw

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

Yep!

fitzgen avatar Sep 10 '20 19:09 fitzgen

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

fitzgen avatar Sep 10 '20 19:09 fitzgen