Skip to content

Commit

Permalink
Specialize Vec::from_iter for vec::IntoIter
Browse files Browse the repository at this point in the history
It's fairly common to expose an API which takes an `IntoIterator` and
immediately collects that into a vector. It's also common to buffer
a bunch of items into a vector and then pass that into one of these
APIs. If the iterator hasn't been advanced, we can make this `from_iter`
simply reassemble the original `Vec` with no actual iteration or
reallocation.
  • Loading branch information
sfackler committed Mar 22, 2017
1 parent 8c4f2c6 commit dae66e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/libcollections/vec.rs
Expand Up @@ -1563,7 +1563,7 @@ impl<T> ops::DerefMut for Vec<T> {
impl<T> FromIterator<T> for Vec<T> {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> {
<Self as SpecExtend<_, _>>::from_iter(iter.into_iter())
<Self as SpecExtend<T, I::IntoIter>>::from_iter(iter.into_iter())
}
}

Expand Down Expand Up @@ -1631,7 +1631,7 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> {
impl<T> Extend<T> for Vec<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.spec_extend(iter.into_iter())
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter())
}
}

Expand Down Expand Up @@ -1662,7 +1662,7 @@ impl<T, I> SpecExtend<T, I> for Vec<T>
vector
}
};
vector.spec_extend(iterator);
<Vec<T> as SpecExtend<T, I>>::spec_extend(&mut vector, iterator);
vector
}

Expand All @@ -1674,7 +1674,7 @@ impl<T, I> SpecExtend<T, I> for Vec<T>
impl<T, I> SpecExtend<T, I> for Vec<T>
where I: TrustedLen<Item=T>,
{
fn from_iter(iterator: I) -> Self {
default fn from_iter(iterator: I) -> Self {
let mut vector = Vec::new();
vector.spec_extend(iterator);
vector
Expand Down Expand Up @@ -1706,6 +1706,27 @@ impl<T, I> SpecExtend<T, I> for Vec<T>
}
}

impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
fn from_iter(iterator: IntoIter<T>) -> Self {
// A common case is passing a vector into a function which immediately
// re-collects into a vector. We can short circuit this if the IntoIter
// has not been advanced at all.
if *iterator.buf == iterator.ptr as *mut T {
unsafe {
let vec = Vec::from_raw_parts(*iterator.buf as *mut T,
iterator.len(),
iterator.cap);
mem::forget(iterator);
vec
}
} else {
let mut vector = Vec::new();
vector.spec_extend(iterator);
vector
}
}
}

impl<'a, T: 'a, I> SpecExtend<&'a T, I> for Vec<T>
where I: Iterator<Item=&'a T>,
T: Clone,
Expand Down
16 changes: 16 additions & 0 deletions src/libcollectionstest/vec.rs
Expand Up @@ -680,3 +680,19 @@ fn test_placement_panic() {
let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| { vec.place_back() <- mkpanic(); }));
assert_eq!(vec.len(), 3);
}

#[test]
fn from_into_inner() {
let vec = vec![1, 2, 3];
let ptr = vec.as_ptr();
let vec = vec.into_iter().collect::<Vec<_>>();
assert_eq!(vec, [1, 2, 3]);
assert_eq!(vec.as_ptr(), ptr);

let ptr = &vec[1] as *const _;
let mut it = vec.into_iter();
it.next().unwrap();
let vec = it.collect::<Vec<_>>();
assert_eq!(vec, [2, 3]);
assert!(ptr != vec.as_ptr());
}

0 comments on commit dae66e0

Please sign in to comment.