about summary refs log tree commit diff
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2021-11-09 21:43:20 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2021-11-09 21:43:20 -0800
commitcc7d8014d720ac800f868a0b7ec854f4a5036e3f (patch)
tree8b08bdfe9c86dd3520b882d2616d9da9db3d4f65
parent8b09ba6a5d5c644fe0f1c27c7f9c80b334241707 (diff)
downloadrust-cc7d8014d720ac800f868a0b7ec854f4a5036e3f.tar.gz
rust-cc7d8014d720ac800f868a0b7ec854f4a5036e3f.zip
Specialize array cloning for Copy types
Because after PR 86041, the optimizer no longer load-merges at the LLVM IR level, which might be part of the perf loss.  (I'll run perf and see if this makes a difference.)

Also I added a codegen test so this hopefully won't regress in future -- it passes on stable and with my change here, but not on the 2021-11-09 nightly.
-rw-r--r--library/core/src/array/mod.rs25
-rw-r--r--src/test/codegen/array-clone.rs15
2 files changed, 37 insertions, 3 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index 089f1f36390..e8e0cbf84b3 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -339,9 +339,7 @@ impl<T: Copy, const N: usize> Copy for [T; N] {}
 impl<T: Clone, const N: usize> Clone for [T; N] {
     #[inline]
     fn clone(&self) -> Self {
-        // SAFETY: we know for certain that this iterator will yield exactly `N`
-        // items.
-        unsafe { collect_into_array_unchecked(&mut self.iter().cloned()) }
+        SpecArrayClone::clone(self)
     }
 
     #[inline]
@@ -350,6 +348,27 @@ impl<T: Clone, const N: usize> Clone for [T; N] {
     }
 }
 
+#[cfg(not(bootstrap))]
+trait SpecArrayClone: Clone {
+    fn clone<const N: usize>(array: &[Self; N]) -> [Self; N];
+}
+
+#[cfg(not(bootstrap))]
+impl<T: Clone> SpecArrayClone for T {
+    default fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
+        // SAFETY: we know for certain that this iterator will yield exactly `N`
+        // items.
+        unsafe { collect_into_array_unchecked(&mut array.iter().cloned()) }
+    }
+}
+
+#[cfg(not(bootstrap))]
+impl<T: Copy> SpecArrayClone for T {
+    fn clone<const N: usize>(array: &[T; N]) -> [T; N] {
+        *array
+    }
+}
+
 // The Default impls cannot be done with const generics because `[T; 0]` doesn't
 // require Default to be implemented, and having different impl blocks for
 // different numbers isn't supported yet.
diff --git a/src/test/codegen/array-clone.rs b/src/test/codegen/array-clone.rs
new file mode 100644
index 00000000000..0d42963bcd2
--- /dev/null
+++ b/src/test/codegen/array-clone.rs
@@ -0,0 +1,15 @@
+// compile-flags: -O
+
+#![crate_type = "lib"]
+
+// CHECK-LABEL: @array_clone
+#[no_mangle]
+pub fn array_clone(a: &[u8; 2]) -> [u8; 2] {
+    // CHECK-NOT: getelementptr
+    // CHECK-NOT: load i8
+    // CHECK-NOT: zext
+    // CHECK-NOT: shl
+    // CHECK: load i16
+    // CHECK-NEXT: ret i16
+    a.clone()
+}