diff options
| author | Josh Stone <jistone@redhat.com> | 2020-03-20 15:28:26 -0700 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2020-04-06 15:50:59 -0700 |
| commit | e8339e820b0c4e7c9a26a3e3495b7655e60bd102 (patch) | |
| tree | b74d61092274bfd11ac9d2861fb6b8ce821a253e /src/liballoc | |
| parent | 6dee5f1126dfd5c9314ee5ae9d9eb010e35ef257 (diff) | |
| download | rust-e8339e820b0c4e7c9a26a3e3495b7655e60bd102.tar.gz rust-e8339e820b0c4e7c9a26a3e3495b7655e60bd102.zip | |
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.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/slice.rs | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index a3d9c78b7f5..cd750d25580 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -733,14 +733,14 @@ impl<T: Clone> ToOwned for [T] { fn clone_into(&self, target: &mut Vec<T>) { // drop anything in target that will not be overwritten target.truncate(self.len()); - let len = target.len(); - - // reuse the contained values' allocations/resources. - target.clone_from_slice(&self[..len]); // target.len <= self.len due to the truncate above, so the - // slice here is always in-bounds. - target.extend_from_slice(&self[len..]); + // slices here are always in-bounds. + let (init, tail) = self.split_at(target.len()); + + // reuse the contained values' allocations/resources. + target.clone_from_slice(init); + target.extend_from_slice(tail); } } |
