Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6fd1a56

Browse files
committedAug 4, 2020
Tweak RawVec::reserve{,_exact}.
1 parent d8cbd9c commit 6fd1a56

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed
 

‎library/alloc/src/raw_vec.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
297297
/// # }
298298
/// ```
299299
pub fn reserve(&mut self, len: usize, additional: usize) {
300-
match self.try_reserve(len, additional) {
301-
Err(CapacityOverflow) => capacity_overflow(),
302-
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
303-
Ok(()) => { /* yay */ }
300+
// This function is marginally shorter if it calls `try_reserve`, but
301+
// that results in more LLVM IR being generated.
302+
if self.needs_to_grow(len, additional) {
303+
match self.grow_amortized(len, additional) {
304+
Ok(()) => { /* yay */ }
305+
Err(CapacityOverflow) => capacity_overflow(),
306+
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
307+
}
304308
}
305309
}
306310

@@ -331,10 +335,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
331335
///
332336
/// Aborts on OOM.
333337
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
334-
match self.try_reserve_exact(len, additional) {
335-
Err(CapacityOverflow) => capacity_overflow(),
336-
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
337-
Ok(()) => { /* yay */ }
338+
// This function is marginally shorter if it calls `try_reserve_exact`,
339+
// but that results in more LLVM IR being generated.
340+
if self.needs_to_grow(len, additional) {
341+
match self.grow_exact(len, additional) {
342+
Ok(()) => { /* yay */ }
343+
Err(CapacityOverflow) => capacity_overflow(),
344+
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
345+
}
338346
}
339347
}
340348

0 commit comments

Comments
 (0)
Please sign in to comment.