Skip to content

Commit fb8d706

Browse files
committed
Do not call into PyArray_EquivTypes if the descriptors are identical.
1 parent 7f5e79f commit fb8d706

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/dtype.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ impl PyArrayDescr {
117117

118118
/// Returns true if two type descriptors are equivalent.
119119
pub fn is_equiv_to(&self, other: &Self) -> bool {
120+
let self_ptr = self.as_dtype_ptr();
121+
let other_ptr = other.as_dtype_ptr();
122+
120123
unsafe {
121-
PY_ARRAY_API.PyArray_EquivTypes(self.py(), self.as_dtype_ptr(), other.as_dtype_ptr())
122-
!= 0
124+
self_ptr == other_ptr
125+
|| PY_ARRAY_API.PyArray_EquivTypes(self.py(), self_ptr, other_ptr) != 0
123126
}
124127
}
125128

@@ -413,7 +416,7 @@ fn npy_int_type_lookup<T, T0, T1, T2>(npy_types: [NPY_TYPES; 3]) -> NPY_TYPES {
413416

414417
fn npy_int_type<T: Bounded + Zero + Sized + PartialEq>() -> NPY_TYPES {
415418
let is_unsigned = T::min_value() == T::zero();
416-
let bit_width = size_of::<T>() << 3;
419+
let bit_width = 8 * size_of::<T>();
417420

418421
match (is_unsigned, bit_width) {
419422
(false, 8) => NPY_TYPES::NPY_BYTE,
@@ -449,6 +452,7 @@ macro_rules! impl_element_scalar {
449452
$(#[$meta])*
450453
unsafe impl Element for $ty {
451454
const IS_COPY: bool = true;
455+
452456
fn get_dtype(py: Python) -> &PyArrayDescr {
453457
PyArrayDescr::from_npy_type(py, $npy_type)
454458
}

0 commit comments

Comments
 (0)