Skip to content

PERF improve performance of is_lexsorted #47459

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 3 commits into from
Jun 24, 2022

Conversation

MarcoGorelli
Copy link
Member

If result is False, there's no need to keep the outer loop going, right?

Timing result, with failure taken from test_is_lexsorted:

%load_ext cython

%%cython -a

cimport cython
from cython cimport Py_ssize_t
from numpy cimport int64_t, import_array, ndarray, PyArray_DATA
import numpy as np

from libc.stdlib cimport free, malloc
from libc.stdio cimport printf
import_array()


@cython.wraparound(False)
@cython.boundscheck(False)
def main_is_lexsorted(list_of_arrays: list) -> bint:
    cdef:
        Py_ssize_t i
        Py_ssize_t n, nlevels
        int64_t k, cur, pre
        ndarray arr
        bint result = True

    nlevels = len(list_of_arrays)
    n = len(list_of_arrays[0])

    cdef int64_t **vecs = <int64_t**>malloc(nlevels * sizeof(int64_t*))
    for i in range(nlevels):
        arr = list_of_arrays[i]
        assert arr.dtype.name == 'int64'
        vecs[i] = <int64_t*>PyArray_DATA(arr)

    # Assume uniqueness??
    with nogil:
        for i in range(1, n):
            for k in range(nlevels):
                cur = vecs[k][i]
                pre = vecs[k][i -1]
                if cur == pre:
                    continue
                elif cur > pre:
                    break
                else:
                    result = False
                    break
    free(vecs)
    return result

@cython.wraparound(False)
@cython.boundscheck(False)
def branch_is_lexsorted(list_of_arrays: list) -> bint:
    cdef:
        Py_ssize_t i
        Py_ssize_t n, nlevels
        int64_t k, cur, pre
        ndarray arr
        bint result = True

    nlevels = len(list_of_arrays)
    n = len(list_of_arrays[0])

    cdef int64_t **vecs = <int64_t**>malloc(nlevels * sizeof(int64_t*))
    for i in range(nlevels):
        arr = list_of_arrays[i]
        assert arr.dtype.name == 'int64'
        vecs[i] = <int64_t*>PyArray_DATA(arr)

    # Assume uniqueness??
    with nogil:
        for i in range(1, n):
            for k in range(nlevels):
                cur = vecs[k][i]
                pre = vecs[k][i -1]
                if cur == pre:
                    continue
                elif cur > pre:
                    break
                else:
                    result = False
                    break
            if not result:
                break
    free(vecs)
    return result

%%timeit
main_is_lexsorted(failure)
# 16.6 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
 
%%timeit
branch_is_lexsorted(failure)
# 15.8 µs ± 113 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

More extreme example:

failure = [np.ones((100_000,), dtype='int64')]
failure[0][1] = 0 

%%timeit
main_is_lexsorted(failure)
# 556 µs ± 26.8 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

%%timeit
branch_is_lexsorted(failure)
# 8.07 µs ± 72.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

@MarcoGorelli MarcoGorelli added the Performance Memory or execution speed performance label Jun 22, 2022
Comment on lines 181 to +184
result = False
break
if not result:
break
Copy link
Member

Choose a reason for hiding this comment

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

This makes sense to me. But probably even better instead of the double break, to simply return when we know it's False. And we don't even need the result variable, since the return at the end of the function should always be return True.

Suggested change
result = False
break
if not result:
break
free(vecs)
return False

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like this isn't allowed

Error compiling Cython file:
------------------------------------------------------------
...
                else:
                    result = False
                    break
            if not result:
                free(vecs)
                return False
               ^
------------------------------------------------------------
pandas/_libs/algos.pyx:184:16: Returning Python object not allowed without gil

Copy link
Member

Choose a reason for hiding this comment

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

Good to know, I was wondering if there was a Cython limitation. Thanks for trying it.

@datapythonista datapythonista added the Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff label Jun 22, 2022
@MarcoGorelli MarcoGorelli marked this pull request as draft June 23, 2022 15:09
@MarcoGorelli MarcoGorelli marked this pull request as ready for review June 23, 2022 15:11
@mroeschke mroeschke added this to the 1.5 milestone Jun 23, 2022
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM. Off to @datapythonista

@jreback
Copy link
Contributor

jreback commented Jun 24, 2022

this diff is showing a lot of changes

@jreback jreback merged commit 70adab9 into pandas-dev:main Jun 24, 2022
@MarcoGorelli MarcoGorelli mentioned this pull request Jul 10, 2022
5 tasks
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants