Skip to content

Commit

Permalink
bench and optimize from_slice
Browse files Browse the repository at this point in the history
This adds "bench_from_slice(_vec)(_small)" benches, renaming the previous
versions to "bench_from_iter*" (because they were using `From<&[T]>`, which
calls `FromIterator` internally) and optimizes the `from_slice` constructor
considerably.

Before:
```
test bench_from_slice                  ... bench:          42 ns/iter (+/- 0)
test bench_from_slice_small            ... bench:          12 ns/iter (+/- 0)
```
After:
```
test bench_from_slice                  ... bench:          27 ns/iter (+/- 0)
test bench_from_slice_small            ... bench:           8 ns/iter (+/- 0)
```
  • Loading branch information
llogiq committed Aug 22, 2018
1 parent 52e7c01 commit 7633aeb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
23 changes: 22 additions & 1 deletion benches/bench.rs
Expand Up @@ -17,6 +17,7 @@ trait Vector<T>: for<'a> From<&'a [T]> + Extend<T> + ExtendFromSlice<T> {
fn remove(&mut self, p: usize) -> T;
fn insert(&mut self, n: usize, val: T);
fn from_elem(val: T, n: usize) -> Self;
fn from_elems(val: &[T]) -> Self;
}

impl<T: Copy> Vector<T> for Vec<T> {
Expand All @@ -43,6 +44,10 @@ impl<T: Copy> Vector<T> for Vec<T> {
fn from_elem(val: T, n: usize) -> Self {
vec![val; n]
}

fn from_elems(val: &[T]) -> Self {
val.to_owned()
}
}

impl<T: Copy> Vector<T> for SmallVec<[T; VEC_SIZE]> {
Expand All @@ -69,6 +74,10 @@ impl<T: Copy> Vector<T> for SmallVec<[T; VEC_SIZE]> {
fn from_elem(val: T, n: usize) -> Self {
smallvec![val; n]
}

fn from_elems(val: &[T]) -> Self {
SmallVec::from_slice(val)
}
}

macro_rules! make_benches {
Expand All @@ -92,6 +101,8 @@ make_benches! {
bench_remove_small => gen_remove(VEC_SIZE as _),
bench_extend => gen_extend(SPILLED_SIZE as _),
bench_extend_small => gen_extend(VEC_SIZE as _),
bench_from_iter => gen_from_iter(SPILLED_SIZE as _),
bench_from_iter_small => gen_from_iter(VEC_SIZE as _),
bench_from_slice => gen_from_slice(SPILLED_SIZE as _),
bench_from_slice_small => gen_from_slice(VEC_SIZE as _),
bench_extend_from_slice => gen_extend_from_slice(SPILLED_SIZE as _),
Expand All @@ -112,6 +123,8 @@ make_benches! {
bench_remove_vec_small => gen_remove(VEC_SIZE as _),
bench_extend_vec => gen_extend(SPILLED_SIZE as _),
bench_extend_vec_small => gen_extend(VEC_SIZE as _),
bench_from_iter_vec => gen_from_iter(SPILLED_SIZE as _),
bench_from_iter_vec_small => gen_from_iter(VEC_SIZE as _),
bench_from_slice_vec => gen_from_slice(SPILLED_SIZE as _),
bench_from_slice_vec_small => gen_from_slice(VEC_SIZE as _),
bench_extend_from_slice_vec => gen_extend_from_slice(SPILLED_SIZE as _),
Expand Down Expand Up @@ -179,14 +192,22 @@ fn gen_extend<V: Vector<u64>>(n: u64, b: &mut Bencher) {
});
}

fn gen_from_slice<V: Vector<u64>>(n: u64, b: &mut Bencher) {
fn gen_from_iter<V: Vector<u64>>(n: u64, b: &mut Bencher) {
let v: Vec<u64> = (0..n).collect();
b.iter(|| {
let vec = V::from(&v);
vec
});
}

fn gen_from_slice<V: Vector<u64>>(n: u64, b: &mut Bencher) {
let v: Vec<u64> = (0..n).collect();
b.iter(|| {
let vec = V::from_elems(&v);
vec
});
}

fn gen_extend_from_slice<V: Vector<u64>>(n: u64, b: &mut Bencher) {
let v: Vec<u64> = (0..n).collect();
b.iter(|| {
Expand Down
22 changes: 19 additions & 3 deletions lib.rs
Expand Up @@ -946,9 +946,25 @@ impl<A: Array> SmallVec<A> where A::Item: Copy {
///
/// For slices of `Copy` types, this is more efficient than `SmallVec::from(slice)`.
pub fn from_slice(slice: &[A::Item]) -> Self {
let mut vec = Self::new();
vec.extend_from_slice(slice);
vec
let len = slice.len();
if len <= A::size() {
SmallVec {
capacity: len,
data: SmallVecData::from_inline(unsafe {
let mut data: A = mem::uninitialized();
ptr::copy_nonoverlapping(slice.as_ptr(), data.ptr_mut(), len);
data
})
}
} else {
let mut b = slice.to_vec();
let ptr = b.as_mut_ptr();
mem::forget(b);
SmallVec {
capacity: len,

This comment has been minimized.

Copy link
@arthurprs

arthurprs Aug 23, 2018

Contributor

Should this be b.capacity()? I'm gessing to_vec may over allocate.

This comment has been minimized.

Copy link
@llogiq

llogiq Aug 23, 2018

Author Contributor

Good catch! I had .to_owned there initially, which doesn't, and failed to check. I'll send a followup PR.

This comment has been minimized.

Copy link
@mbrubeck

mbrubeck Aug 23, 2018

Collaborator

Currently slice::to_vec never over-allocates, so this is safe in current versions of Rust, but it's still worth fixing in case that behavior doesn't hold in future versions.

data: SmallVecData::from_heap(ptr, len),
}
}
}

/// Copy elements from a slice into the vector at position `index`, shifting any following
Expand Down

0 comments on commit 7633aeb

Please sign in to comment.