Skip to content

Commit 6801c81

Browse files
committed
feat(allocator): add Allocator::new and with_capacity methods (#8620)
Add `Allocator::with_capacity` method to allow specifying initial capacity when creating `Allocator`. Also add `Allocator::new` as an alternative to `Allocator::default` (does the same thing).
1 parent 6f95cd5 commit 6801c81

File tree

1 file changed

+29
-0
lines changed
  • crates/oxc_allocator/src

1 file changed

+29
-0
lines changed

crates/oxc_allocator/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ pub struct Allocator {
8989
}
9090

9191
impl Allocator {
92+
/// Create a new [`Allocator`] with no initial capacity.
93+
///
94+
/// This method does not reserve any memory to back the allocator. Memory for allocator's initial
95+
/// chunk will be reserved lazily, when you make the first allocation into this [`Allocator`]
96+
/// (e.g. with [`Allocator::alloc`], [`Box::new_in`], [`Vec::new_in`], [`HashMap::new_in`]).
97+
///
98+
/// If you can estimate the amount of memory the allocator will require to fit what you intend to
99+
/// allocate into it, it is generally preferable to create that allocator with [`with_capacity`]
100+
/// which reserves that amount of memory upfront. This will avoid further system calls to allocate
101+
/// further chunks later on.
102+
///
103+
/// [`with_capacity`]: Allocator::with_capacity
104+
//
105+
// `#[inline(always)]` because just delegates to `bumpalo` method
106+
#[expect(clippy::inline_always)]
107+
#[inline(always)]
108+
pub fn new() -> Self {
109+
Self { bump: Bump::new() }
110+
}
111+
112+
/// Create a new [`Allocator`] with specified capacity.
113+
//
114+
// `#[inline(always)]` because just delegates to `bumpalo` method
115+
#[expect(clippy::inline_always)]
116+
#[inline(always)]
117+
pub fn with_capacity(capacity: usize) -> Self {
118+
Self { bump: Bump::with_capacity(capacity) }
119+
}
120+
92121
/// Allocate an object in this [`Allocator`] and return an exclusive reference to it.
93122
///
94123
/// # Panics

0 commit comments

Comments
 (0)