Skip to content

Commit 4fff335

Browse files
committed
Avoid MIR bloat in inlining
In 126578 we ended up with more binary size increases than expected. This change attempts to avoid inlining large things into small things, to avoid that kind of increase, in cases when top-down inlining will still be able to do that inlining later.
1 parent 6c3359c commit 4fff335

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

alloc/src/raw_vec.rs

+17
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) };

alloc/src/vec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ impl<T, A: Allocator> Vec<T, A> {
11011101
/// ```
11021102
#[cfg(not(no_global_oom_handling))]
11031103
#[stable(feature = "rust1", since = "1.0.0")]
1104+
#[inline]
11041105
pub fn shrink_to_fit(&mut self) {
11051106
// The capacity is never less than the length, and there's nothing to do when
11061107
// they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit`

0 commit comments

Comments
 (0)