diff options
| author | bors <bors@rust-lang.org> | 2020-07-03 03:18:28 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-07-03 03:18:28 +0000 |
| commit | cd1a46d644791c79433db934ad4e6131c577efcc (patch) | |
| tree | dbdebdd59f9a08bca0372d68b2da8fb5f6539ebf | |
| parent | 5f4abc16e1b2cb035eee6a5079ce45ce924c1f33 (diff) | |
| parent | 3f79d2f33e8b69812c9b981ff4adea0a1e7b9cb8 (diff) | |
| download | rust-cd1a46d644791c79433db934ad4e6131c577efcc.tar.gz rust-cd1a46d644791c79433db934ad4e6131c577efcc.zip | |
Auto merge of #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
| -rw-r--r-- | src/liballoc/raw_vec.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index b2cf98a5de6..ed81ce71ddf 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -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), |
