Skip to content

Implement extend_from_slice and insert_from_slice with memmove optimization #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ fn bench_extend(b: &mut Bencher) {
});
}

#[bench]
fn bench_extend_from_slice(b: &mut Bencher) {
let v: Vec<u64> = (0..100).collect();
b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
vec.extend_from_slice(&v);
vec
});
}

#[bench]
fn bench_insert_from_slice(b: &mut Bencher) {
let v: Vec<u64> = (0..100).collect();
b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
vec.insert_from_slice(0, &v);
vec.insert_from_slice(0, &v);
vec
});
}

#[bench]
fn bench_pushpop(b: &mut Bencher) {
#[inline(never)]
Expand Down
45 changes: 45 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,29 @@ impl<A: Array> SmallVec<A> {
}
}

impl<A: Array> SmallVec<A> where A::Item: Copy {
pub fn insert_from_slice(&mut self, index: usize, slice: &[A::Item]) {
self.reserve(slice.len());
Copy link
Member

Choose a reason for hiding this comment

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

Hmm... I'm kind of sleepy right now, but I think here you should reserve index + slice.len()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe reserve takes in an "additional" amount required and ensures the capacity is large enough, so I think this is ok.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, you're right, nvm, I thought reserve() did the usual stuff, that is, ensuring capacity.


let len = self.len;
assert!(index <= len);

unsafe {
let slice_ptr = slice.as_ptr();
let ptr = self.as_mut_ptr().offset(index as isize);
ptr::copy(ptr, ptr.offset(slice.len() as isize), len - index);
ptr::copy(slice_ptr, ptr, slice.len());
self.set_len(len + slice.len());
}
}

#[inline]
pub fn extend_from_slice(&mut self, slice: &[A::Item]) {
let len = self.len();
self.insert_from_slice(len, slice);
}
}

impl<A: Array> ops::Deref for SmallVec<A> {
type Target = [A::Item];
#[inline]
Expand Down Expand Up @@ -1068,6 +1091,28 @@ pub mod tests {
v.grow(5);
}

#[test]
fn test_insert_from_slice() {
let mut v: SmallVec<[u8; 8]> = SmallVec::new();
for x in 0..4 {
v.push(x);
}
assert_eq!(v.len(), 4);
v.insert_from_slice(1, &[5, 6]);
assert_eq!(&v.iter().map(|v| *v).collect::<Vec<_>>(), &[0, 5, 6, 1, 2, 3]);
}

#[test]
fn test_extend_from_slice() {
let mut v: SmallVec<[u8; 8]> = SmallVec::new();
for x in 0..4 {
v.push(x);
}
assert_eq!(v.len(), 4);
v.extend_from_slice(&[5, 6]);
assert_eq!(&v.iter().map(|v| *v).collect::<Vec<_>>(), &[0, 1, 2, 3, 5, 6]);
}

#[test]
#[should_panic]
fn test_drop_panic_smallvec() {
Expand Down