Rollup merge of #48065 - Xaeroxe:patch-1, r=alexcrichton

Apply optimization from #44355 to retain

As discussed in #44355 this PR applies a similar optimization to `Vec::retain`.  For `drain_filter`, a very similar function, this improved performance by up to 20%.
This commit is contained in:
kennytm 2018-02-14 16:14:32 +08:00 committed by GitHub
commit 3715f1e490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 16 deletions

View File

@ -805,22 +805,7 @@ impl<T> Vec<T> {
pub fn retain<F>(&mut self, mut f: F)
where F: FnMut(&T) -> bool
{
let len = self.len();
let mut del = 0;
{
let v = &mut **self;
for i in 0..len {
if !f(&v[i]) {
del += 1;
} else if del > 0 {
v.swap(i - del, i);
}
}
}
if del > 0 {
self.truncate(len - del);
}
self.drain_filter(|x| !f(x));
}
/// Removes all but the first of consecutive elements in the vector that resolve to the same