Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of .fold() #574

Merged
merged 2 commits into from Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/dimension/axes.rs
Expand Up @@ -75,6 +75,15 @@ impl<'a, D> Iterator for Axes<'a, D>
}
}

fn fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, AxisDescription) -> B,
{
(self.start..self.end)
.map(move |i| AxisDescription(Axis(i), self.dim[i], self.strides[i] as isize))
.fold(init, f)
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.end - self.start;
(len, Some(len))
Expand Down
26 changes: 19 additions & 7 deletions src/impl_methods.rs
Expand Up @@ -1854,13 +1854,25 @@ where
} else {
let mut v = self.view();
// put the narrowest axis at the last position
if v.ndim() > 1 {
let last = v.ndim() - 1;
let narrow_axis = v.axes()
.filter(|ax| ax.len() > 1)
.min_by_key(|ax| ax.stride().abs())
.map_or(last, |ax| ax.axis().index());
v.swap_axes(last, narrow_axis);
match v.ndim() {
0 | 1 => {}
2 => {
if self.len_of(Axis(1)) <= 1
|| self.len_of(Axis(0)) > 1
&& self.stride_of(Axis(0)).abs() < self.stride_of(Axis(1)).abs()
{
v.swap_axes(0, 1);
}
}
n => {
let last = n - 1;
let narrow_axis = v
.axes()
.filter(|ax| ax.len() > 1)
.min_by_key(|ax| ax.stride().abs())
.map_or(last, |ax| ax.axis().index());
v.swap_axes(last, narrow_axis);
}
}
v.into_elements_base().fold(init, f)
}
Expand Down