Skip to content

Commit cd1a46d

Browse files
committed
Auto merge of rust-lang#73882 - nnethercote:avoid-unwrap_or_else-in-allocate_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
2 parents 5f4abc1 + 3f79d2f commit cd1a46d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/liballoc/raw_vec.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,21 @@ impl<T, A: AllocRef> RawVec<T, A> {
170170
if mem::size_of::<T>() == 0 {
171171
Self::new_in(alloc)
172172
} else {
173-
let layout = Layout::array::<T>(capacity).unwrap_or_else(|_| capacity_overflow());
174-
alloc_guard(layout.size()).unwrap_or_else(|_| capacity_overflow());
173+
// We avoid `unwrap_or_else` here because it bloats the amount of
174+
// LLVM IR generated.
175+
let layout = match Layout::array::<T>(capacity) {
176+
Ok(layout) => layout,
177+
Err(_) => capacity_overflow(),
178+
};
179+
match alloc_guard(layout.size()) {
180+
Ok(_) => {}
181+
Err(_) => capacity_overflow(),
182+
}
183+
let memory = match alloc.alloc(layout, init) {
184+
Ok(memory) => memory,
185+
Err(_) => handle_alloc_error(layout),
186+
};
175187

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

0 commit comments

Comments
 (0)