From 8b13d425b31a4b533f35c1ae957046ccd8be0119 Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Thu, 9 Jan 2025 22:38:47 -0800 Subject: [PATCH 1/2] optimize `Array::concat` The resulting assembly is much nicer. As a bonus, `concat` no longer has panicking branches. Signed-off-by: Eric Lagergren --- src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6380b22..b45440c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -232,7 +232,18 @@ where U: Add, Sum: ArraySize, { - self.into_iter().chain(other).collect() + let mut c = Array::uninit(); + let mut i = 0; + for v in self { + c[i].write(v); + i += 1; + } + for v in other { + c[i].write(v); + i += 1; + } + // SAFETY: We wrote to every element of `c`. + unsafe { c.assume_init() } } /// Splits `self` at index `N` in two arrays. From 13153817295cf6d5e67021299d50df1910e2315e Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Thu, 9 Jan 2025 22:45:19 -0800 Subject: [PATCH 2/2] thanks, clippy Signed-off-by: Eric Lagergren --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index b45440c..4a1334c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,6 +226,7 @@ where /// Concatenates `self` with `other`. #[inline] + #[allow(clippy::arithmetic_side_effects, reason = "`i` will never overflow")] pub fn concat(self, other: Array) -> Array> where N: ArraySize,