Skip to content

ENH: Add fast array equal function for indexers #50592

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

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,11 @@ def get_reverse_indexer(
) -> npt.NDArray[np.intp]: ...
def is_bool_list(obj: list) -> bool: ...
def dtypes_all_equal(types: list[DtypeObj]) -> bool: ...
@overload
def array_equal_fast(
left: np.ndarray[np.int64], right: np.ndarray[np.int64]
) -> bool: ...
@overload
def array_equal_fast(
left: np.ndarray[np.int32], right: np.ndarray[np.int32]
) -> bool: ...
34 changes: 34 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ from numpy cimport (
complex128_t,
flatiter,
float64_t,
int32_t,
int64_t,
intp_t,
ndarray,
Expand Down Expand Up @@ -642,6 +643,39 @@ def array_equivalent_object(ndarray left, ndarray right) -> bool:
return True


ctypedef fused int6432_t:
int64_t
int32_t


@cython.wraparound(False)
@cython.boundscheck(False)
def array_equal_fast(
ndarray[int6432_t, ndim=1] left, ndarray[int6432_t, ndim=1] right,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check locally a little bit, and using a memoryview (int[:]) instead of ndarray doesn't seem to give a significant speed up here (I seemed to remember that those could sometimes be faster)

) -> bool:
"""
Perform an element by element comparison on 1-d integer arrays, meant for indexer
comparisons
"""
cdef:
Py_ssize_t i, n = left.size

if left.size != right.size:
return False

with nogil:
for i in range(n):

if left[i] != right[i]:
break
else:
i = i + 1

if i != n:
return False
return True


ctypedef fused ndarr_object:
ndarray[object, ndim=1]
ndarray[object, ndim=2]
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ def test_get_reverse_indexer(self):
expected = np.array([4, 2, 3, 6, 7], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("dtype", ["int64", "int32"])
def test_array_equal_fast(self, dtype):
# GH#50592
left = np.arange(1, 100, dtype=dtype)
right = np.arange(1, 100, dtype=dtype)
assert lib.array_equal_fast(left, right)

@pytest.mark.parametrize("dtype", ["int64", "int32"])
def test_array_equal_fast_not_equal(self, dtype):
# GH#50592
left = np.array([1, 2], dtype=dtype)
right = np.array([2, 2], dtype=dtype)
assert not lib.array_equal_fast(left, right)

@pytest.mark.parametrize("dtype", ["int64", "int32"])
def test_array_equal_fast_not_equal_shape(self, dtype):
# GH#50592
left = np.array([1, 2, 3], dtype=dtype)
right = np.array([2, 2], dtype=dtype)
assert not lib.array_equal_fast(left, right)


def test_cache_readonly_preserve_docstrings():
# GH18197
Expand Down