Skip to content

Commit

Permalink
collections: merge unsafe_push_all_clone and push_all
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Jul 7, 2014
1 parent e2d107c commit b4984a4
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/libcollections/vec.rs
Expand Up @@ -197,14 +197,8 @@ impl<T: Clone> Vec<T> {
/// ```
#[inline]
pub fn from_slice(values: &[T]) -> Vec<T> {
let mut vector = Vec::with_capacity(values.len());

// Directly call `unsafe_push_all_clone` so we can skip a call to
// `reserve_addtional`.
unsafe {
unsafe_push_all_clone(&mut vector, values);
}

let mut vector = Vec::new();
vector.push_all(values);
vector
}

Expand Down Expand Up @@ -248,8 +242,18 @@ impl<T: Clone> Vec<T> {
pub fn push_all(&mut self, other: &[T]) {
self.reserve_additional(other.len());

unsafe {
unsafe_push_all_clone(self, other)
for i in range(0, other.len()) {
let len = self.len();

// Unsafe code so this can be optimised to a memcpy (or something similarly
// fast) when T is Copy. LLVM is easily confused, so any extra operations
// during the loop can prevent this optimisation.
unsafe {
ptr::write(
self.as_mut_slice().unsafe_mut_ref(len),
other.unsafe_ref(i).clone());
self.set_len(len + 1);
}
}
}

Expand Down Expand Up @@ -1550,25 +1554,6 @@ pub mod raw {
}
}

// Unsafe code so this can be optimised to a memcpy (or something similarly
// fast) when T is Copy. LLVM is easily confused, so any extra operations
// during the loop can prevent this optimisation.
//
// WARNING: You must preallocate space on the vector before you call this
// method.
#[inline(always)]
unsafe fn unsafe_push_all_clone<T: Clone>(dst: &mut Vec<T>, src: &[T]) {
let mut dst_len = dst.len();

for i in range(0, src.len()) {
ptr::write(
dst.as_mut_slice().unsafe_mut_ref(dst_len),
src.unsafe_ref(i).clone());
dst_len += 1;
dst.set_len(dst_len);
}
}

#[cfg(test)]
mod tests {
extern crate test;
Expand Down

5 comments on commit b4984a4

@bors
Copy link
Contributor

@bors bors commented on b4984a4 Jul 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from acrichto
at erickt@b4984a4

@bors
Copy link
Contributor

@bors bors commented on b4984a4 Jul 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/push_all = b4984a4 into auto

@bors
Copy link
Contributor

@bors bors commented on b4984a4 Jul 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erickt/rust/push_all = b4984a4 merged ok, testing candidate = 66e1f11

@bors
Copy link
Contributor

@bors bors commented on b4984a4 Jul 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 66e1f11

Please sign in to comment.