Skip to content

Commit 3715f1e

Browse files
authored
Rollup merge of rust-lang#48065 - Xaeroxe:patch-1, r=alexcrichton
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%.
2 parents a5c3209 + fbad3b2 commit 3715f1e

File tree

1 file changed

+1
-16
lines changed

1 file changed

+1
-16
lines changed

src/liballoc/vec.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -805,22 +805,7 @@ impl<T> Vec<T> {
805805
pub fn retain<F>(&mut self, mut f: F)
806806
where F: FnMut(&T) -> bool
807807
{
808-
let len = self.len();
809-
let mut del = 0;
810-
{
811-
let v = &mut **self;
812-
813-
for i in 0..len {
814-
if !f(&v[i]) {
815-
del += 1;
816-
} else if del > 0 {
817-
v.swap(i - del, i);
818-
}
819-
}
820-
}
821-
if del > 0 {
822-
self.truncate(len - del);
823-
}
808+
self.drain_filter(|x| !f(x));
824809
}
825810

826811
/// Removes all but the first of consecutive elements in the vector that resolve to the same

0 commit comments

Comments
 (0)