Skip to content

Commit 7a29aa5

Browse files
committed
vec: optimize the Add implementation
before: test add ... bench: 164 ns/iter (+/- 1) after: test add ... bench: 113 ns/iter (+/- 2)
1 parent e70aac5 commit 7a29aa5

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/libstd/vec.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'self, T> RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> {
561561

562562
#[cfg(not(test))]
563563
pub mod traits {
564-
use super::Vector;
564+
use super::*;
565565

566566
use clone::Clone;
567567
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
@@ -686,17 +686,17 @@ pub mod traits {
686686
impl<'self,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
687687
#[inline]
688688
fn add(&self, rhs: &V) -> ~[T] {
689-
let mut res = self.to_owned();
689+
let mut res = with_capacity(self.len() + rhs.as_slice().len());
690+
res.push_all(*self);
690691
res.push_all(rhs.as_slice());
691692
res
692693
}
693694
}
695+
694696
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
695697
#[inline]
696698
fn add(&self, rhs: &V) -> ~[T] {
697-
let mut res = self.to_owned();
698-
res.push_all(rhs.as_slice());
699-
res
699+
self.as_slice() + rhs.as_slice()
700700
}
701701
}
702702
}
@@ -3662,4 +3662,13 @@ mod bench {
36623662
}
36633663
}
36643664
}
3665+
3666+
#[bench]
3667+
fn add(b: &mut BenchHarness) {
3668+
let xs: &[int] = [5, ..10];
3669+
let ys: &[int] = [5, ..10];
3670+
do b.iter() {
3671+
xs + ys;
3672+
}
3673+
}
36653674
}

0 commit comments

Comments
 (0)