8000 Add fast case (no-op case) for into_dimensionality and into_dyn by bluss · Pull Request #906 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::mem::MaybeUninit;
use ndarray::ShapeBuilder;
use ndarray::{arr0, arr1, arr2, azip, s};
use ndarray::{Array, Array1, Array2, Axis, Ix, Zip};
use ndarray::{Ix1, Ix2, Ix3, Ix5, IxDyn};

use test::black_box;

Expand Down Expand Up @@ -941,3 +942,59 @@ fn sum_axis1(bench: &mut test::Bencher) {
let a = range_mat(MEAN_SUM_N, MEAN_SUM_N);
bench.iter(|| a.sum_axis(Axis(1)));
}

#[bench]
fn into_dimensionality_ix1_ok(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(Ix1(10));
let a = a.view();
bench.iter(|| a.into_dimensionality::<Ix1>());
}

#[bench]
fn into_dimensionality_ix3_ok(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(Ix3(10, 10, 10));
let a = a.view();
bench.iter(|| a.into_dimensionality::<Ix3>());
}

#[bench]
fn into_dimensionality_ix3_err(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(Ix3(10, 10, 10));
let a = a.view();
bench.iter(|| a.into_dimensionality::<Ix2>());
}

#[bench]
fn into_dimensionality_dyn_to_ix3(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(IxDyn(&[10, 10, 10]));
let a = a.view();
bench.iter(|| a.clone().into_dimensionality::<Ix3>());
}

#[bench]
fn into_dimensionality_dyn_to_dyn(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(IxDyn(&[10, 10, 10]));
let a = a.view();
bench.iter(|| a.clone().into_dimensionality::<IxDyn>());
}

#[bench]
fn into_dyn_ix3(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(Ix3(10, 10, 10));
let a = a.view();
bench.iter(|| a.into_dyn());
}

#[bench]
fn into_dyn_ix5(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(Ix5(2, 2, 2, 2, 2));
let a = a.view();
bench.iter(|| a.into_dyn());
}

#[bench]
fn into_dyn_dyn(bench: &mut test::Bencher) {
let a = Array::<f32, _>::zeros(IxDyn(&[10, 10, 10]));
let a = a.view();
bench.iter(|| a.clone().into_dyn());
}
5 changes: 5 additions & 0 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,11 @@ impl Dimension for IxDyn {
fn from_dimension<D2: Dimension>(d: &D2) -> Option<Self> {
Some(IxDyn(d.slice()))
}

fn into_dyn(self) -> IxDyn {
self
}

private_impl! {}
}

Expand Down
41 changes: 37 additions & 4 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem::{size_of, ManuallyDrop};
use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -1583,8 +1584,11 @@ where
}
}

/// Convert an array or array view to another with the same type, but
/// different dimensionality type. Errors if the dimensions don't agree.
/// Convert an array or array view to another with the same type, but different dimensionality
/// type. Errors if the dimensions don't agree (the number of axes must match).
///
/// Note that conversion to a dynamic dimensional array will never fail (and is equivalent to
/// the `into_dyn` method).
///
/// ```
/// use ndarray::{ArrayD, Ix2, IxDyn};
Expand All @@ -1600,15 +1604,29 @@ where
where
D2: Dimension,
{
if let Some(dim) = D2::from_dimension(&self.dim) {
if let Some(strides) = D2::from_dimension(&self.strides) {
if D::NDIM == D2::NDIM {
// safe because D == D2
unsafe {
let dim = unlimited_transmute::<D, D2>(self.dim);
let strides = unlimited_transmute::<D, D2>(self.strides);
return Ok(ArrayBase {
data: self.data,
ptr: self.ptr,
dim,
strides,
});
}
} else if D::NDIM == None || D2::NDIM == None { // one is dynamic dim
if let Some(dim) = D2::from_dimension(&self.dim) {
if let Some(strides) = D2::from_dimension(&self.strides) {
return Ok(ArrayBase {
data: self.data,
ptr: self.ptr,
dim,
strides,
});
}
}
}
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape))
}
Expand Down Expand Up @@ -2375,3 +2393,18 @@ where
});
}
}


/// Transmute from A to B.
///
/// Like transmute, but does not have the compile-time size check which blocks
/// using regular transmute in some cases.
///
/// **Panics** if the size of A and B are different.
#[inline]
unsafe fn unlimited_transmute<A, B>(data: A) -> B {
// safe when sizes are equal and caller guarantees that representations are equal
assert_eq!(size_of::<A>(), size_of::<B>());
let old_data = ManuallyDrop::new(data);
(&*old_data as *const A as *const B).read()
}
0