Skip to content
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

Optimize TinyVec::extend by rewriting ArrayVec::fill to not call push #131

Merged
merged 1 commit into from
Jan 18, 2021
Merged
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
13 changes: 11 additions & 2 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,19 @@ impl<A: Array> ArrayVec<A> {
pub fn fill<I: IntoIterator<Item = A::Item>>(
&mut self, iter: I,
) -> I::IntoIter {
// If this is written as a call to push for each element in iter, the
// compiler emits code that updates the length for every element. The
// additional complexity from that length update is worth nearly 2x in
// the runtime of this function.
let mut iter = iter.into_iter();
for element in iter.by_ref().take(self.capacity() - self.len()) {
self.push(element);
let mut pushed = 0;
let to_take = self.capacity() - self.len();
let target = &mut self.data.as_slice_mut()[self.len as usize..];
for element in iter.by_ref().take(to_take) {
target[pushed] = element;
pushed += 1;
}
self.len += pushed as u16;
iter
}

Expand Down