about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-14 16:14:32 +0800
committerGitHub <noreply@github.com>2018-02-14 16:14:32 +0800
commit3715f1e490e187c3635ef241104bd5abe119958b (patch)
treee675da0a97f32d6a5f554b838b9b00bfb3be689f /src/liballoc
parenta5c3209374261b2589e7be4d19953fd14f268ccc (diff)
parentfbad3b2468f46c14d0fd1283aa4935b3d79f007b (diff)
downloadrust-3715f1e490e187c3635ef241104bd5abe119958b.tar.gz
rust-3715f1e490e187c3635ef241104bd5abe119958b.zip
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%.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/vec.rs17
1 files changed, 1 insertions, 16 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index b26979c7f6d..5c7f8ef7321 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -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