From cc7d8014d720ac800f868a0b7ec854f4a5036e3f Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 9 Nov 2021 21:43:20 -0800 Subject: 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. --- library/core/src/array/mod.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'library/core/src/array') 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 Copy for [T; N] {} impl 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 Clone for [T; N] { } } +#[cfg(not(bootstrap))] +trait SpecArrayClone: Clone { + fn clone(array: &[Self; N]) -> [Self; N]; +} + +#[cfg(not(bootstrap))] +impl SpecArrayClone for T { + default fn clone(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 SpecArrayClone for T { + fn clone(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. -- cgit 1.4.1-3-g733a5 From 5b115fcb6839b94a14fb77acdd738281b4bb3fae Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Wed, 10 Nov 2021 11:57:14 -0800 Subject: Moar #[inline] --- library/core/src/array/mod.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'library/core/src/array') diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index e8e0cbf84b3..09bb4519170 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -355,6 +355,7 @@ trait SpecArrayClone: Clone { #[cfg(not(bootstrap))] impl SpecArrayClone for T { + #[inline] default fn clone(array: &[T; N]) -> [T; N] { // SAFETY: we know for certain that this iterator will yield exactly `N` // items. @@ -364,6 +365,7 @@ impl SpecArrayClone for T { #[cfg(not(bootstrap))] impl SpecArrayClone for T { + #[inline] fn clone(array: &[T; N]) -> [T; N] { *array } -- cgit 1.4.1-3-g733a5