Skip to content
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

PERF: improve StringArray.isna #57733

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jorisvandenbossche
Copy link
Member

See #57431 (comment) for context.

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Speed-up is around 2x for this case (extracted from one of our ASVs):

dtype = "string"
N = 10**6
data = np.array([str(i) * 5 for i in range(N)], dtype=object)
na_value = pd.NA
ser = pd.Series(data, dtype=dtype)

%timeit ser.isna()
# 11.3 ms ± 47.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)    <-- main
# 5.01 ms ± 55.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)    <-- PR

Not entirely sure it is worth the extra code, but so it definitely gives a decent speedup for a common operation.

@jorisvandenbossche jorisvandenbossche added Performance Memory or execution speed performance Strings String extension data type and string data labels Mar 5, 2024
cnp.PyArray_ITER_NEXT(it)
if val is C_NA:
# Dereference pointer (set value)
(<uint8_t *>(cnp.PyArray_ITER_DATA(it2)))[0] = <uint8_t>1
Copy link
Member

Choose a reason for hiding this comment

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

You can just assign 1U - no need for the cast on the right hand side

@@ -220,6 +220,51 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr):
return result.view(np.bool_)


@cython.wraparound(False)
@cython.boundscheck(False)
cpdef ndarray[uint8_t] isna_string(ndarray arr):
Copy link
Member

Choose a reason for hiding this comment

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

Can we return a const uint8_t memory view instead of an ndarray? I still think generally better to use memoryviews over the long term to stay somewhat backend agnostic

Copy link
Member Author

Choose a reason for hiding this comment

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

I think for the return value we want a numpy array? (inside the function we could use more memoryviews)
I am actually only using this function from python, so can make this a def function and remove the return type

Copy link
Contributor

github-actions bot commented Apr 5, 2024

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance Stale Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants