Skip to content

Commit dcf38e8

Browse files
jturner314bluss
andcommitted
FEAT: Performance improvement for &Array + scalar and scalar + &Array
* The new implementation avoids cloning the elements twice, and it avoids iterating over the elements twice. (The old implementation called `.to_owned()` followed by the arithmetic operation, while the new implementation clones the elements and performs the arithmetic operation in the same iteration.) On my machine, this change improves the performance for both contiguous and discontiguous arrays. (`scalar_add_1/2` go from ~530 ns/iter to ~380 ns/iter, and `scalar_add_strided_1/2` go from ~1540 ns/iter to ~1420 ns/iter.) (Other changes to impl applicability removed from this commit.) Co-authored-by: bluss <[email protected]>
1 parent 52ca234 commit dcf38e8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/impl_ops.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ impl<'a, A, S, D, B> $trt<B> for &'a ArrayBase<S, D>
159159
B: ScalarOperand,
160160
{
161161
type Output = Array<A, D>;
162-
fn $mth(self, x: B) -> Array<A, D> {
163-
self.to_owned().$mth(x)
162+
fn $mth(self, x: B) -> Self::Output {
163+
self.map(move |elt| elt.clone() $operator x.clone())
164164
}
165165
}
166166
);
@@ -210,11 +210,11 @@ impl<'a, S, D> $trt<&'a ArrayBase<S, D>> for $scalar
210210
D: Dimension,
211211
{
212212
type Output = Array<$scalar, D>;
213-
fn $mth(self, rhs: &ArrayBase<S, D>) -> Array<$scalar, D> {
213+
fn $mth(self, rhs: &ArrayBase<S, D>) -> Self::Output {
214214
if_commutative!($commutative {
215215
rhs.$mth(self)
216216
} or {
217-
self.$mth(rhs.to_owned())
217+
rhs.map(move |elt| self.clone() $operator elt.clone())
218218
})
219219
}
220220
}

0 commit comments

Comments
 (0)