Skip to content

Commit

Permalink
Rollup merge of rust-lang#48065 - Xaeroxe:patch-1, r=alexcrichton
Browse files Browse the repository at this point in the history
Apply optimization from rust-lang#44355 to retain

As discussed in rust-lang#44355 this PR applies a similar optimization to `Vec::retain`.  For `drain_filter`, a very similar function, this improved performance by up to 20%.
  • Loading branch information
kennytm committed Feb 14, 2018
2 parents a5c3209 + fbad3b2 commit 3715f1e
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3715f1e

Please sign in to comment.