Skip to content

Commit a76bb92

Browse files
committed
FIX: Factor out the inner loops in Zip apply_core_*
The innermost loop is the stretch of constant stride (for the contiguous case, this is the whole loop); with a lot of function arguments, this part is actually commmon to the three loops (contig, c, and f). Also make a small simplification of this inner loop; use a while loop to have less code to compile in this much instantiated function.
1 parent eb79293 commit a76bb92

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/zip/mod.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ where
723723
}
724724
}
725725

726-
fn apply_core_contiguous<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
726+
fn apply_core_contiguous<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
727727
where
728728
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
729729
P: ZippableTuple<Dim = D>,
@@ -732,15 +732,35 @@ where
732732
let size = self.dimension.size();
733733
let ptrs = self.parts.as_ptr();
734734
let inner_strides = self.parts.contiguous_stride();
735-
for i in 0..size {
736-
unsafe {
737-
let ptr_i = ptrs.stride_offset(inner_strides, i);
738-
acc = fold_while![function(acc, self.parts.as_ref(ptr_i))];
739-
}
735+
unsafe {
736+
self.inner(acc, ptrs, inner_strides, size, &mut function)
737+
}
738+
}
739+
740+
/// The innermost loop of the Zip apply methods
741+
///
742+
/// Run the fold while operation on a stretch of elements with constant strides
743+
///
744+
/// `ptr`: base pointer for the first element in this stretch
745+
/// `strides`: strides for the elements in this stretch
746+
/// `len`: number of elements
747+
/// `function`: closure
748+
unsafe fn inner<F, Acc>(&self, mut acc: Acc, ptr: P::Ptr, strides: P::Stride,
749+
len: usize, function: &mut F) -> FoldWhile<Acc>
750+
where
751+
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
752+
P: ZippableTuple
753+
{
754+
let mut i = 0;
755+
while i < len {
756+
let p = ptr.stride_offset(strides, i);
757+
acc = fold_while!(function(acc, self.parts.as_ref(p)));
758+
i += 1;
740759
}
741760
FoldWhile::Continue(acc)
742761
}
743762

763+
744764
fn apply_core_strided<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
745765
where
746766
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
@@ -773,10 +793,7 @@ where
773793
while let Some(index) = index_ {
774794
unsafe {
775795
let ptr = self.parts.uget_ptr(&index);
776-
for i in 0..inner_len {
777-
let p = ptr.stride_offset(inner_strides, i);
778-
acc = fold_while!(function(acc, self.parts.as_ref(p)));
779-
}
796+
acc = fold_while![self.inner(acc, ptr, inner_strides, inner_len, &mut function)];
780797
}
781798

782799
index_ = self.dimension.next_for(index);
@@ -800,10 +817,7 @@ where
800817
loop {
801818
unsafe {
802819
let ptr = self.parts.uget_ptr(&index);
803-
for i in 0..inner_len {
804-
let p = ptr.stride_offset(inner_strides, i);
805-
acc = fold_while!(function(acc, self.parts.as_ref(p)));
806-
}
820+
acc = fold_while![self.inner(acc, ptr, inner_strides, inner_len, &mut function)];
807821
}
808822

809823
if !self.dimension.next_for_f(&mut index) {

0 commit comments

Comments
 (0)