Skip to content

Commit ccf071d

Browse files
committed
zip: Add Zip::and_unchecked and use to skip redundant dimension check
This speeds up the benches/append.rs benchmarks by 5-10%.
1 parent 5aebc8d commit ccf071d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/impl_owned_array.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,10 @@ impl<A, D> Array<A, D>
540540
data: &mut self.data,
541541
};
542542

543-
Zip::from(tail_view).and(array)
543+
544+
// Safety: tail_view is constructed to have the same shape as array
545+
Zip::from(tail_view)
546+
.and_unchecked(array)
544547
.debug_assert_c_order()
545548
.for_each(|to, from| {
546549
to.write(from.clone());

src/zip/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,26 @@ macro_rules! map_impl {
679679
self.build_and(part)
680680
}
681681

682+
/// Include the producer `p` in the Zip.
683+
///
684+
/// ## Safety
685+
///
686+
/// The caller must ensure that the producer's shape is equal to the Zip's shape.
687+
/// Uses assertions when debug assertions are enabled.
688+
#[allow(unused)]
689+
pub(crate) unsafe fn and_unchecked<P>(self, p: P) -> Zip<($($p,)* P::Output, ), D>
690+
where P: IntoNdProducer<Dim=D>,
691+
{
692+
#[cfg(debug_assertions)]
693+
{
694+
self.and(p)
695+
}
696+
#[cfg(not(debug_assertions))]
697+
{
698+
self.build_and(p.into_producer())
699+
}
700+
}
701+
682702
/// Include the producer `p` in the Zip, broadcasting if needed.
683703
///
684704
/// If their shapes disagree, `rhs` is broadcast to the shape of `self`.

0 commit comments

Comments
 (0)