Skip to content

Commit e8339e8

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 6dee5f1 commit e8339e8

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
@@ -733,14 +733,14 @@ impl<T: Clone> ToOwned for [T] {
733733
fn clone_into(&self, target: &mut Vec<T>) {
734734
// drop anything in target that will not be overwritten
735735
target.truncate(self.len());
736-
let len = target.len();
737-
738-
// reuse the contained values' allocations/resources.
739-
target.clone_from_slice(&self[..len]);
740736

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

0 commit comments

Comments
 (0)