Skip to content

Commit

Permalink
auto merge of #13241 : stepancheg/rust/push-all, r=alexcrichton
Browse files Browse the repository at this point in the history
* push_all* operations should reserve capacity before pushing data to avoid unnecessary reallocations
* reserve_exact should never shrink, as specified in documentation
  • Loading branch information
bors committed Apr 2, 2014
2 parents e63346b + 026d206 commit af0783a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ impl<T: Clone> Vec<T> {
/// ```
#[inline]
pub fn push_all(&mut self, other: &[T]) {
for element in other.iter() {
self.push((*element).clone())
}
self.extend(other.iter().map(|e| e.clone()));
}

/// Grows the `Vec` in-place.
Expand Down Expand Up @@ -447,7 +445,7 @@ impl<T> Vec<T> {
/// assert_eq!(vec.capacity(), 11);
/// ```
pub fn reserve_exact(&mut self, capacity: uint) {
if capacity >= self.len {
if capacity > self.cap {
let size = capacity.checked_mul(&size_of::<T>()).expect("capacity overflow");
self.cap = capacity;
unsafe {
Expand Down Expand Up @@ -947,9 +945,7 @@ impl<T> Vec<T> {
/// assert_eq!(vec, vec!(~1, ~2, ~3, ~4));
/// ```
pub fn push_all_move(&mut self, other: Vec<T>) {
for element in other.move_iter() {
self.push(element)
}
self.extend(other.move_iter());
}

/// Returns a mutable slice of `self` between `start` and `end`.
Expand Down

0 comments on commit af0783a

Please sign in to comment.