Skip to content

Commit fd98c66

Browse files
committed
FEAT: Improved extend performance
1 parent 675182b commit fd98c66

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
@@ -933,6 +933,7 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
933933
unsafe {
934934
let len = self.len();
935935
let mut ptr = self.as_mut_ptr().offset(len as isize);
936+
let end_ptr = ptr.offset(take as isize);
936937
// Keep the length in a separate variable, write it back on scope
937938
// exit. To help the compiler with alias analysis and stuff.
938939
// We update the length to handle panic in the iteration of the
@@ -944,10 +945,16 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
944945
**self_len = Index::from(len);
945946
}
946947
};
947-
for elt in iter.into_iter().take(take) {
948-
ptr::write(ptr, elt);
949-
ptr = ptr.offset(1);
950-
guard.data += 1;
948+
let mut iter = iter.into_iter();
949+
loop {
950+
if ptr == end_ptr { break; }
951+
if let Some(elt) = iter.next() {
952+
ptr::write(ptr, elt);
953+
ptr = ptr.offset(1);
954+
guard.data += 1;
955+
} else {
956+
break;
957+
}
951958
}
952959
}
953960
}

0 commit comments

Comments
 (0)