Skip to content

Commit 2a4c018

Browse files
committed
Apply optimization from #44355 to retain
1 parent fee39ba commit 2a4c018

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/liballoc/vec.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,19 @@ impl<T> Vec<T> {
813813
for i in 0..len {
814814
if !f(&v[i]) {
815815
del += 1;
816+
unsafe {
817+
ptr::read(&v[i]);
818+
}
816819
} else if del > 0 {
817-
v.swap(i - del, i);
820+
let src: *const T = &v[i];
821+
let dst: *mut T = &mut v[i - del];
822+
unsafe {
823+
ptr::copy_nonoverlapping(src, dst, 1);
824+
}
818825
}
819826
}
820827
}
821-
if del > 0 {
822-
self.truncate(len - del);
823-
}
828+
self.len = len - del;
824829
}
825830

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

0 commit comments

Comments
 (0)