fitzgen / bumpalo

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

feat: expose constant ABOUT_ONE_PAGE or similar #192

Closed morrisonlevi closed 1 year ago

morrisonlevi commented 1 year ago

When setting the Bump capacity, it would be nice to have a constant (or helper function) for allocating about one "page" worth of memory e.g.

let arena = Bump::with_capacity(Bump::ABOUT_ONE_PAGE);
// roughly equivalent to something like:
let arena = Bump::with_capacity(4096 - 64);

Essentially, I don't care about the exact value, I just want bumpalo to have nice chunk sizes as far as the underlying allocator is concerned. I just don't know how to tell it to do that, other than by experimentation, which could be broken by implementation changes.

fitzgen commented 1 year ago

bumpalo already internally is rounding to "about a page" to play nice with the underlying allocator, so there is no need to expose it to users to attempt to handle this twice.

https://github.com/fitzgen/bumpalo/blob/19891eed44c866c4efd91d331a2ad5e6d6bcb0ae/src/lib.rs#L652

morrisonlevi commented 1 year ago

@fitzgen At least for the initial allocation, this doesn't seem to be true:

let arena = Bump::new();
let _ = arena.alloc(true);
let capacity = arena.chunk_capacity();
assert_eq!(4096-64-1, capacity);

Fails with:

  left: `4031`,
 right: `448`,
fitzgen commented 1 year ago

When the capacity is below a page in size it does simple doubling, after which point the rounding to page sizes (while leaving a little room for malloc overhead) kicks in.

fitzgen commented 1 year ago

See the comment just above the line of code I linked and that whole general bit of code

morrisonlevi commented 1 year ago

Yes, I understand that. But I don't want that behavior. I want to start at 1 page, and I don't know the overhead. What do you recommend for this case? Bump::with_capacity(4000)?

fitzgen commented 1 year ago

In general, you're better off letting the dynamic chunk growth happen and adapt its strategy but if you absolutely need to start with ~a page, that should work fine.