diff --git a/crates/oxc_allocator/src/vec2/raw_vec.rs b/crates/oxc_allocator/src/vec2/raw_vec.rs index 7d2e99a83ec5f..e3be8ea224559 100644 --- a/crates/oxc_allocator/src/vec2/raw_vec.rs +++ b/crates/oxc_allocator/src/vec2/raw_vec.rs @@ -541,11 +541,10 @@ impl<'a, T> RawVec<'a, T> { let new_size = elem_size * amount; let align = mem::align_of::(); let old_layout = Layout::from_size_align_unchecked(old_size, align); - match realloc(self.a, self.ptr.cast(), old_layout, new_size) { + let new_layout = Layout::from_size_align_unchecked(new_size, align); + match self.a.shrink(self.ptr.cast(), old_layout, new_layout) { Ok(p) => self.ptr = p.cast(), - Err(_) => { - handle_alloc_error(Layout::from_size_align_unchecked(new_size, align)) - } + Err(_) => handle_alloc_error(new_layout), } } self.cap = amount; @@ -631,8 +630,20 @@ impl<'a, T> RawVec<'a, T> { let res = match self.current_layout() { Some(layout) => unsafe { + // Marking this function as `#[cold]` and `#[inline(never)]` because grow method is + // relatively expensive and we want to avoid inlining it into the caller. + #[cold] + #[inline(never)] + unsafe fn grow( + a: &Bump, + ptr: NonNull, + old_layout: Layout, + new_layout: Layout, + ) -> Result, AllocError> { + a.grow(ptr.cast(), old_layout, new_layout) + } debug_assert!(new_layout.align() == layout.align()); - realloc(self.a, self.ptr.cast(), layout, new_layout.size()) + grow(self.a, self.ptr, layout, new_layout) }, None => self.a.allocate(new_layout), }; @@ -685,81 +696,6 @@ fn layout_from_size_align(size: usize, align: usize) -> Result, - layout: Layout, - new_size: usize, -) -> Result, AllocError> { - let old_size = layout.size(); - - if old_size == 0 { - return bump.allocate(layout); - } - - let new_layout = layout_from_size_align(new_size, layout.align())?; - if new_size <= old_size { - bump.shrink(ptr, layout, new_layout) - } else { - bump.grow(ptr, layout, new_layout) - } -} - /// Handle collection allocation errors /// // Causing a collection alloc error is rare case, so marked as `#[cold]` and `#[inline(never)]`