about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-10-15 01:48:42 -0700
committerGitHub <noreply@github.com>2016-10-15 01:48:42 -0700
commit030bc49bb44834e691fd67452b39b438fa6d826d (patch)
tree1469eadc56fdd6dc35eb8d05521776ca33cfd788 /src
parente1b67776dbb65e4ad256d27d9d215c643a4df57b (diff)
parent63ee8d0bbc9911ee121ed4af4f7e81cbd600d8b1 (diff)
Auto merge of #37094 - fhartwig:spec-extend-from-slice, r=alexcrichton
Specialize Vec::extend to Vec::extend_from_slice

I tried using the existing `SpecExtend` as a helper trait for this, but the instances would always conflict with the instances higher up in the file, so I created a new helper trait.

Benchmarking `extend` vs `extend_from_slice` with an slice of 1000 `u64`s gives the following results:

```
before:

running 2 tests
test tests::bench_extend_from_slice ... bench:         166 ns/iter (+/- 78)
test tests::bench_extend_trait      ... bench:       1,187 ns/iter (+/- 697)

after:
running 2 tests
test tests::bench_extend_from_slice ... bench:         149 ns/iter (+/- 87)
test tests::bench_extend_trait      ... bench:         138 ns/iter (+/- 70)
```
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/vec.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index 2bb311cc5ae..52fc8b9dd70 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -1614,7 +1614,24 @@ impl<T> Vec<T> {
 #[stable(feature = "extend_ref", since = "1.2.0")]
 impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
     fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
-        self.extend(iter.into_iter().cloned());
+        <I as SpecExtendVec<T>>::extend_vec(iter, self);
+    }
+}
+
+// helper trait for specialization of Vec's Extend impl
+trait SpecExtendVec<T> {
+    fn extend_vec(self, vec: &mut Vec<T>);
+}
+
+impl <'a, T: 'a + Copy, I: IntoIterator<Item=&'a T>> SpecExtendVec<T> for I {
+    default fn extend_vec(self, vec: &mut Vec<T>) {
+        vec.extend(self.into_iter().cloned());
+    }
+}
+
+impl<'a, T: Copy> SpecExtendVec<T> for &'a [T] {
+    fn extend_vec(self, vec: &mut Vec<T>) {
+        vec.extend_from_slice(self);
     }
 }