Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(allocator): add Allocator::new and with_capacity methods #8620

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ pub struct Allocator {
}

impl Allocator {
/// Create a new [`Allocator`] with no initial capacity.
///
/// This method does not reserve any memory to back the allocator. Memory for allocator's initial
/// chunk will be reserved lazily, when you make the first allocation into this [`Allocator`]
/// (e.g. with [`Allocator::alloc`], [`Box::new_in`], [`Vec::new_in`], [`HashMap::new_in`]).
///
/// If you can estimate the amount of memory the allocator will require to fit what you intend to
/// allocate into it, it is generally preferable to create that allocator with [`with_capacity`]
/// which reserves that amount of memory upfront. This will avoid further system calls to allocate
/// further chunks later on.
///
/// [`with_capacity`]: Allocator::with_capacity
//
// `#[inline(always)]` because just delegates to `bumpalo` method
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn new() -> Self {
Self { bump: Bump::new() }
}

/// Create a new [`Allocator`] with specified capacity.
//
// `#[inline(always)]` because just delegates to `bumpalo` method
#[expect(clippy::inline_always)]
#[inline(always)]
pub fn with_capacity(capacity: usize) -> Self {
Self { bump: Bump::with_capacity(capacity) }
}

/// Allocate an object in this [`Allocator`] and return an exclusive reference to it.
///
/// # Panics
Expand Down
Loading