Skip to content

Commit

Permalink
FEAT: Improved extend performance
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Nov 28, 2018
1 parent 675182b commit fd98c66
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
unsafe {
let len = self.len();
let mut ptr = self.as_mut_ptr().offset(len as isize);
let end_ptr = ptr.offset(take as isize);
// Keep the length in a separate variable, write it back on scope
// exit. To help the compiler with alias analysis and stuff.
// We update the length to handle panic in the iteration of the
Expand All @@ -944,10 +945,16 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
**self_len = Index::from(len);
}
};
for elt in iter.into_iter().take(take) {
ptr::write(ptr, elt);
ptr = ptr.offset(1);
guard.data += 1;
let mut iter = iter.into_iter();
loop {
if ptr == end_ptr { break; }
if let Some(elt) = iter.next() {
ptr::write(ptr, elt);
ptr = ptr.offset(1);
guard.data += 1;
} else {
break;
}
}
}
}
Expand Down

0 comments on commit fd98c66

Please sign in to comment.