Skip to content

Commit bceeb90

Browse files
committed
Inline Layout::array() into RawVec::finish_grow().
1 parent 8c3296e commit bceeb90

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
22

3-
use core::alloc::LayoutError;
43
use core::cmp;
54
use core::intrinsics;
65
use core::mem::{self, ManuallyDrop, MaybeUninit};
@@ -395,10 +394,9 @@ impl<T, A: Allocator> RawVec<T, A> {
395394
let cap = cmp::max(self.cap * 2, required_cap);
396395
let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap);
397396

398-
let new_layout = Layout::array::<T>(cap);
399-
400397
// `finish_grow` is non-generic over `T`.
401-
let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
398+
let elem_layout = Layout::new::<T>();
399+
let ptr = finish_grow(cap, elem_layout, self.current_memory(), &mut self.alloc)?;
402400
self.set_ptr(ptr);
403401
Ok(())
404402
}
@@ -415,10 +413,10 @@ impl<T, A: Allocator> RawVec<T, A> {
415413
// is called for a zero-sized `T` after `needs_to_grow()` has
416414
// succeeded, this early return will occur.)
417415
let cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
418-
let new_layout = Layout::array::<T>(cap);
419416

420417
// `finish_grow` is non-generic over `T`.
421-
let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
418+
let elem_layout = Layout::new::<T>();
419+
let ptr = finish_grow(cap, elem_layout, self.current_memory(), &mut self.alloc)?;
422420
self.set_ptr(ptr);
423421
Ok(())
424422
}
@@ -446,17 +444,18 @@ impl<T, A: Allocator> RawVec<T, A> {
446444
// much smaller than the number of `T` types.)
447445
#[inline(never)]
448446
fn finish_grow<A>(
449-
new_layout: Result<Layout, LayoutError>,
447+
cap: usize,
448+
elem_layout: Layout,
450449
current_memory: Option<(NonNull<u8>, Layout)>,
451450
alloc: &mut A,
452451
) -> Result<NonNull<[u8]>, TryReserveError>
453452
where
454453
A: Allocator,
455454
{
456-
// Check for the error here to minimize the size of `RawVec::grow_*`.
457-
let new_layout = new_layout.map_err(|_| CapacityOverflow)?;
455+
let array_size = elem_layout.size().checked_mul(cap).ok_or(CapacityOverflow)?;
456+
alloc_guard(array_size)?;
458457

459-
alloc_guard(new_layout.size())?;
458+
let new_layout = unsafe { Layout::from_size_align_unchecked(array_size, elem_layout.align()) };
460459

461460
let memory = if let Some((ptr, old_layout)) = current_memory {
462461
debug_assert_eq!(old_layout.align(), new_layout.align());

0 commit comments

Comments
 (0)