Skip to content

Commit c042b49

Browse files
committed
FEAT: Improved extend performance
1 parent 41b9073 commit c042b49

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
885885
unsafe {
886886
let len = self.len();
887887
let mut ptr = self.as_mut_ptr().offset(len as isize);
888+
let end_ptr = ptr.offset(take as isize);
888889
// Keep the length in a separate variable, write it back on scope
889890
// exit. To help the compiler with alias analysis and stuff.
890891
// We update the length to handle panic in the iteration of the
@@ -896,10 +897,16 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
896897
**self_len = Index::from(len);
897898
}
898899
};
899-
for elt in iter.into_iter().take(take) {
900-
ptr::write(ptr, elt);
901-
ptr = ptr.offset(1);
902-
guard.data += 1;
900+
let mut iter = iter.into_iter();
901+
loop {
902+
if ptr == end_ptr { break; }
903+
if let Some(elt) = iter.next() {
904+
ptr::write(ptr, elt);
905+
ptr = ptr.offset(1);
906+
guard.data += 1;
907+
} else {
908+
break;
909+
}
903910
}
904911
}
905912
}

0 commit comments

Comments
 (0)