-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
PERF: Skip probing __array_ufunc__
for NumPy builtin scalars
#21470
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
Conversation
We also already have EDIT: Of course, it might be interesting to see if using that function is much slower than the other one. |
I found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now, thanks! Just those two small nits to clean up the code a bit.
Some of the scalar benchmarks should notice this now, so I think it is OK benchmark wise. Although adding new small-array benchmarks would be cool anyway.
(Or maybe as Matti suggested there, see if we shouldn't just add small arrays to the shape matrix.)
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
…mpy into fast_check_attribute
__array_ufunc__
for NumPy builtin scalars
Thanks @eendebakpt lets put this in. |
The method
PyUFuncOverride_GetNonDefaultArrayUfunc
is expensive on numpy scalars because these objects do not have a__array_ufunc__
set and for a missing attribute lookup cpython generates an exception that is later cleared by numpy. This is a performance bottleneck, see #21455.An issue has been submitted to cpython (python/cpython#92216). But even if this is addressed in cpython, it will take untill python 3.12+ before this will be useable by numpy.
As an alternative solution, this PR adds a fast path to
PyUFuncOverride_GetNonDefaultArrayUfunc
to determine whether an object is a numpy scalar. Some remarks:PyDoubleArrType_Type
andPyIntArrType_Type
. Perhaps we can check all types easily? Or select a different subset of all numpy scalar types.__array_ufunc__
to the numpy scalars, but this is not backwards compatible (PERF: Improve performance of special attribute lookups #21423 (comment))Microbenchmark on
np.sqrt(np.float64(1.1))
:Full benchmark