Auto merge of #44355 - Xaeroxe:optimize_drain_filter, r=alexcrichton

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.
This commit is contained in:
bors 2017-09-20 06:29:42 +00:00
commit 94a82adbb4

View File

@ -2694,7 +2694,13 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F>
self.del += 1; self.del += 1;
return Some(ptr::read(&v[i])); return Some(ptr::read(&v[i]));
} else if self.del > 0 { } 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 None