]> git.lizzy.rs Git - rust.git/commitdiff
Apply optimization from #44355 to retain
authorJacob Kiesel <kieseljake@gmail.com>
Wed, 7 Feb 2018 19:35:52 +0000 (12:35 -0700)
committerJacob Kiesel <kieseljake@gmail.com>
Thu, 8 Feb 2018 04:23:16 +0000 (21:23 -0700)
src/liballoc/vec.rs

index b26979c7f6d8c75863e8386f1d77447fb42b42c6..a906628dbc734567918ad2714663e3c8725789a6 100644 (file)
@@ -813,14 +813,19 @@ pub fn retain<F>(&mut self, mut f: F)
             for i in 0..len {
                 if !f(&v[i]) {
                     del += 1;
+                    unsafe {
+                        ptr::read(&v[i]);
+                    }
                 } else if del > 0 {
-                    v.swap(i - del, i);
+                    let src: *const T = &v[i];
+                    let dst: *mut T = &mut v[i - del];
+                    unsafe {
+                        ptr::copy_nonoverlapping(src, dst, 1);
+                    }
                 }
             }
         }
-        if del > 0 {
-            self.truncate(len - del);
-        }
+        self.len = len - del;
     }
 
     /// Removes all but the first of consecutive elements in the vector that resolve to the same