Skip to content

Commit 745eb7a

Browse files
committed
Use split_at in slice's ToOwned::clone_into
It appears to codegen slightly more efficiently with `split_at` taking two slices at once, rather than slicing across different calls.
1 parent 2835ca6 commit 745eb7a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/liballoc/slice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,14 @@ impl<T: Clone> ToOwned for [T] {
735735
fn clone_into(&self, target: &mut Vec<T>) {
736736
// drop anything in target that will not be overwritten
737737
target.truncate(self.len());
738-
let len = target.len();
739-
740-
// reuse the contained values' allocations/resources.
741-
target.clone_from_slice(&self[..len]);
742738

743739
// target.len <= self.len due to the truncate above, so the
744-
// slice here is always in-bounds.
745-
target.extend_from_slice(&self[len..]);
740+
// slices here are always in-bounds.
741+
let (init, tail) = self.split_at(target.len());
742+
743+
// reuse the contained values' allocations/resources.
744+
target.clone_from_slice(init);
745+
target.extend_from_slice(tail);
746746
}
747747
}
748748

0 commit comments

Comments
 (0)