Skip to content

Commit 3f59442

Browse files
blussjturner314
andcommitted
append: Use standard order for non-growing axes in append
Imagine that the array-to-append has length 1 along the growing axis. With this memory layout, the resulting assignment when copying the array elements into `self`, can often be a contiguous traversal. Co-authored-by: Jim Turner <[email protected]>
1 parent c9dd195 commit 3f59442

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/impl_owned_array.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,14 @@ impl<A, D> Array<A, D>
327327
// The array will be created with 0 (C) or ndim-1 (F) as the biggest stride
328328
// axis. Rearrange the shape so that `growing_axis` is the biggest stride axis
329329
// afterwards.
330-
let prefer_f_layout = growing_axis == Axis(ndim - 1);
331-
if !prefer_f_layout {
332-
dim.slice_mut().swap(0, growing_axis.index());
333-
}
334-
let mut new_array = Self::uninit(dim.set_f(prefer_f_layout));
335-
if !prefer_f_layout {
336-
new_array.swap_axes(0, growing_axis.index());
330+
let mut new_array;
331+
if growing_axis == Axis(ndim - 1) {
332+
new_array = Self::uninit(dim.f());
333+
} else {
334+
dim.slice_mut()[..=growing_axis.index()].rotate_right(1);
335+
new_array = Self::uninit(dim);
336+
new_array.dim.slice_mut()[..=growing_axis.index()].rotate_left(1);
337+
new_array.strides.slice_mut()[..=growing_axis.index()].rotate_left(1);
337338
}
338339

339340
// self -> old_self.
@@ -467,11 +468,13 @@ impl<A, D> Array<A, D>
467468
// Axis n - 1 is outermost axis
468469
res_dim.fortran_strides()
469470
} else {
470-
// Default with modification
471-
res_dim.slice_mut().swap(0, axis.index());
471+
// standard axis order except for the growing axis;
472+
// anticipates that it's likely that `array` has standard order apart from the
473+
// growing axis.
474+
res_dim.slice_mut()[..=axis.index()].rotate_right(1);
472475
let mut strides = res_dim.default_strides();
473-
res_dim.slice_mut().swap(0, axis.index());
474-
strides.slice_mut().swap(0, axis.index());
476+
res_dim.slice_mut()[..=axis.index()].rotate_left(1);
477+
strides.slice_mut()[..=axis.index()].rotate_left(1);
475478
strides
476479
}
477480
} else if current_axis_len == 1 {

0 commit comments

Comments
 (0)