From 7ea6c46a6d70506e92630f0186f2c6b0386147a0 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sat, 14 Dec 2019 12:38:45 -0500 Subject: [PATCH] Restore original implementation of Vec::retain This PR reverts #48065, which aimed to optimize `Vec::retain` by making use of `Vec::drain_filter`. Unfortunately at that time, `drain_filter` was unsound. The soundness hole in `Vec::drain_filter` was fixed in #61224 by guaranteeing that cleanup logic runs via a nested `Drop`, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code. Fixes #65970 --- src/liballoc/vec.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 6e165ccb91998..e5abfdd52022a 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1075,7 +1075,22 @@ impl Vec { pub fn retain(&mut self, mut f: F) where F: FnMut(&T) -> bool { - self.drain_filter(|x| !f(x)); + 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); + } } /// Removes all but the first of consecutive elements in the vector that resolve to the same