Skip to content

Commit

Permalink
Auto merge of #44355 - Xaeroxe:optimize_drain_filter, r=alexcrichton
Browse files Browse the repository at this point in the history
Optimize drain_filter

This PR cuts out two copies from each iteration of `drain_filter` by exchanging the swap operation for a copy_nonoverlapping function call instead.  Since the data being swapped is not needed anymore we can just overwrite it instead.
  • Loading branch information
bors committed Sep 19, 2017
2 parents 325ba23 + 10384ab commit 8bc544a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2694,7 +2694,13 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F>
self.del += 1;
return Some(ptr::read(&v[i]));
} else if self.del > 0 {
v.swap(i - self.del, i);
let del = self.del;
let src: *const T = &v[i];
let dst: *mut T = &mut v[i - del];
// This is safe because self.vec has length 0
// thus its elements will not have Drop::drop
// called on them in the event of a panic.
ptr::copy_nonoverlapping(src, dst, 1);
}
}
None
Expand Down

0 comments on commit 8bc544a

Please sign in to comment.