From 2a4c0185187dd40683697932c57af608062cb320 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Wed, 7 Feb 2018 12:35:52 -0700 Subject: [PATCH 1/3] Apply optimization from #44355 to retain --- src/liballoc/vec.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index b26979c7f6d8c..a906628dbc734 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -813,14 +813,19 @@ impl Vec { 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 From a67749ae87b1c873ed09fca2a204beff2fe5e7ea Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Thu, 8 Feb 2018 08:27:53 -0700 Subject: [PATCH 2/3] Swap `ptr::read` for `ptr::drop_in_place` --- src/liballoc/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index a906628dbc734..41ba8e12105bb 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -814,7 +814,7 @@ impl Vec { if !f(&v[i]) { del += 1; unsafe { - ptr::read(&v[i]); + ptr::drop_in_place(&mut v[i]); } } else if del > 0 { let src: *const T = &v[i]; From fbad3b2468f46c14d0fd1283aa4935b3d79f007b Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 13 Feb 2018 08:48:25 -0700 Subject: [PATCH 3/3] Switch to retain calling drain_filter. --- src/liballoc/vec.rs | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 41ba8e12105bb..5c7f8ef73217f 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -805,27 +805,7 @@ impl Vec { pub fn retain(&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; - unsafe { - ptr::drop_in_place(&mut v[i]); - } - } else if del > 0 { - let src: *const T = &v[i]; - let dst: *mut T = &mut v[i - del]; - unsafe { - ptr::copy_nonoverlapping(src, dst, 1); - } - } - } - } - self.len = len - del; + self.drain_filter(|x| !f(x)); } /// Removes all but the first of consecutive elements in the vector that resolve to the same