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 25, 2018
1 parent 75955c5 commit 13ee3a4
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 @@ -885,6 +885,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 @@ -896,10 +897,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 13ee3a4

Please sign in to comment.