Skip to content

Commit d19f375

Browse files
authored
Rollup merge of #81826 - tesuji:inline-box-zeros, r=Amanieu
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>.
2 parents 52bc54e + fb4e734 commit d19f375

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

library/alloc/src/boxed.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,12 @@ impl<T, A: Allocator> Box<T, A> {
390390
// #[unstable(feature = "new_uninit", issue = "63291")]
391391
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
392392
let layout = Layout::new::<mem::MaybeUninit<T>>();
393-
Box::try_new_uninit_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout))
393+
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
394+
// That would make code size bigger.
395+
match Box::try_new_uninit_in(alloc) {
396+
Ok(m) => m,
397+
Err(_) => handle_alloc_error(layout),
398+
}
394399
}
395400

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

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

0 commit comments

Comments
 (0)