Skip to content

Commit 3f79d2f

Browse files
committed
Avoid unwrap_or_else in RawVec::allocate_in.
This reduces the amount of LLVM IR generated by up to 1 or 2%.
1 parent c977b87 commit 3f79d2f

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
@@ -172,10 +172,21 @@ impl<T, A: AllocRef> RawVec<T, A> {
172172
if mem::size_of::<T>() == 0 {
173173
Self::new_in(alloc)
174174
} else {
175-
let layout = Layout::array::<T>(capacity).unwrap_or_else(|_| capacity_overflow());
176-
alloc_guard(layout.size()).unwrap_or_else(|_| capacity_overflow());
175+
// We avoid `unwrap_or_else` here because it bloats the amount of
176+
// LLVM IR generated.
177+
let layout = match Layout::array::<T>(capacity) {
178+
Ok(layout) => layout,
179+
Err(_) => capacity_overflow(),
180+
};
181+
match alloc_guard(layout.size()) {
182+
Ok(_) => {}
183+
Err(_) => capacity_overflow(),
184+
}
185+
let memory = match alloc.alloc(layout, init) {
186+
Ok(memory) => memory,
187+
Err(_) => handle_alloc_error(layout),
188+
};
177189

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

0 commit comments

Comments
 (0)