Avoid MIR bloat in inlining · model-checking/verify-rust-std@4fff335

Original file line numberDiff line numberDiff line change

@@ -429,6 +429,7 @@ impl<T, A: Allocator> RawVec<T, A> {

429429

///

430430

/// Aborts on OOM.

431431

#[cfg(not(no_global_oom_handling))]

432+

#[inline]

432433

pub fn shrink_to_fit(&mut self, cap: usize) {

433434

if let Err(err) = self.shrink(cap) {

434435

handle_error(err);

@@ -511,9 +512,25 @@ impl<T, A: Allocator> RawVec<T, A> {

511512

}

512513
513514

#[cfg(not(no_global_oom_handling))]

515+

#[inline]

514516

fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> {

515517

assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");

518+

// SAFETY: Just checked this isn't trying to grow

519+

unsafe { self.shrink_unchecked(cap) }

520+

}

516521
522+

/// `shrink`, but without the capacity check.

523+

///

524+

/// This is split out so that `shrink` can inline the check, since it

525+

/// optimizes out in things like `shrink_to_fit`, without needing to

526+

/// also inline all this code, as doing that ends up failing the

527+

/// `vec-shrink-panic` codegen test when `shrink_to_fit` ends up being too

528+

/// big for LLVM to be willing to inline.

529+

///

530+

/// # Safety

531+

/// `cap <= self.capacity()`

532+

#[cfg(not(no_global_oom_handling))]

533+

unsafe fn shrink_unchecked(&mut self, cap: usize) -> Result<(), TryReserveError> {

517534

let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };

518535

// See current_memory() why this assert is here

519536

const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };