Skip to content

Commit

Permalink
Auto merge of rust-lang#73882 - nnethercote:avoid-unwrap_or_else-in-a…
Browse files Browse the repository at this point in the history
…llocate_in, r=Amanieu

Avoid `unwrap_or_else` in `RawVec::allocate_in`.

This reduces the amount of LLVM IR generated by up to 1 or 2%.

r? @Amanieu
  • Loading branch information
bors committed Jul 3, 2020
2 parents 5f4abc1 + 3f79d2f commit cd1a46d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,21 @@ impl<T, A: AllocRef> RawVec<T, A> {
if mem::size_of::<T>() == 0 {
Self::new_in(alloc)
} else {
let layout = Layout::array::<T>(capacity).unwrap_or_else(|_| capacity_overflow());
alloc_guard(layout.size()).unwrap_or_else(|_| capacity_overflow());
// We avoid `unwrap_or_else` here because it bloats the amount of
// LLVM IR generated.
let layout = match Layout::array::<T>(capacity) {
Ok(layout) => layout,
Err(_) => capacity_overflow(),
};
match alloc_guard(layout.size()) {
Ok(_) => {}
Err(_) => capacity_overflow(),
}
let memory = match alloc.alloc(layout, init) {
Ok(memory) => memory,
Err(_) => handle_alloc_error(layout),
};

let memory = alloc.alloc(layout, init).unwrap_or_else(|_| handle_alloc_error(layout));
Self {
ptr: unsafe { Unique::new_unchecked(memory.ptr.cast().as_ptr()) },
cap: Self::capacity_from_bytes(memory.size),
Expand Down

0 comments on commit cd1a46d

Please sign in to comment.