Skip to content

Commit

Permalink
Rollup merge of #81826 - tesuji:inline-box-zeros, r=Amanieu
Browse files Browse the repository at this point in the history
Prefer match over combinators to make some Box methods inlineable

Hopefully this patch would make two snippets generated identical code: <https://rust.godbolt.org/z/fjrj4E>.
  • Loading branch information
Dylan-DPC authored Feb 9, 2021
2 parents 52bc54e + fb4e734 commit d19f375
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,12 @@ impl<T, A: Allocator> Box<T, A> {
// #[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
let layout = Layout::new::<mem::MaybeUninit<T>>();
Box::try_new_uninit_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout))
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
// That would make code size bigger.
match Box::try_new_uninit_in(alloc) {
Ok(m) => m,
Err(_) => handle_alloc_error(layout),
}
}

/// Constructs a new box with uninitialized contents in the provided allocator,
Expand Down Expand Up @@ -447,7 +452,12 @@ impl<T, A: Allocator> Box<T, A> {
// #[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
let layout = Layout::new::<mem::MaybeUninit<T>>();
Box::try_new_zeroed_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout))
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
// That would make code size bigger.
match Box::try_new_zeroed_in(alloc) {
Ok(m) => m,
Err(_) => handle_alloc_error(layout),
}
}

/// Constructs a new `Box` with uninitialized contents, with the memory
Expand Down

0 comments on commit d19f375

Please sign in to comment.