From cea171b0aecfbf982c606ce3d9be09ba8405fbd6 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sun, 12 Oct 2014 01:17:11 +0200 Subject: [PATCH] improve clone_from_slice performance --- src/libcore/slice.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 5847a6177d7..6193e92cab5 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1126,13 +1126,16 @@ pub trait MutableCloneableSlice { } #[unstable = "trait is unstable"] -impl<'a, T:Clone> MutableCloneableSlice for &'a mut [T] { +impl<'a, T: Clone> MutableCloneableSlice for &'a mut [T] { #[inline] fn clone_from_slice(self, src: &[T]) -> uint { - for (a, b) in self.iter_mut().zip(src.iter()) { - a.clone_from(b); + let min = cmp::min(self.len(), src.len()); + let dst = self.slice_to_mut(min); + let src = src.slice_to(min); + for i in range(0, min) { + dst[i].clone_from(&src[i]); } - cmp::min(self.len(), src.len()) + min } }