-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Use PyObject_GetOptionalAttr
#27119
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 should be using |
0be8647
to
1b99112
Compare
PyObject_GetOptionalAttr
PyObject_GetOptionalAttr
This uses `PyObject_GetOptionalAttr` which is backported in capi compat. This function significantly speeds up many attribute lookups and thus dispatching protocols.
1b99112
to
fbd9a3b
Compare
Doesn't affect many inputs (but not few either, since a lot of objects have an
|
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.
Just one minor comment, otherwise this looks like a really nice cleanup. Didn't spot any bugs in the C refactorings.
@@ -46,22 +47,19 @@ _is_basic_python_type(PyTypeObject *tp) | |||
* | |||
* In future, could be made more like _Py_LookupSpecial |
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.
seems like you're moving the API away from _PyObject_LookupSpecial
, which returns a PyObject *
. Maybe delete this comment if it no longer applies?
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.
Good point, actually _PyObject_LookupSpecial
is the old name for PyObject_GetOptionalAttr
. But I didn't time if it makes sense to just remove the exact type-check fast path or not. So going to keep this (with the adaptation for now).
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.
(More importantly: You need Python 3.12 to get the super-fast behavior for type(...)
, so we actually can't fully replace it yet even if it might be fast enough for most cases at least.)
I'd like to backport the |
Nope that should be fine |
Now that
PyObject_GetOptionalAttr
is public in 3.13 and semi-public via the capi-backports... Let's use it!This is a massive speed up for code paths that run into this. For full throttle gains, you need 3.12, because only in 3.12 were type lookups improved (I didn't test it with it).
For example:
will go from 800+ns to <200ns on my setup. Things like
np.result_type(dtype, dtype)
(dtypes hit slow lookups for__array_function__
) should be at 2-3 times faster on Python 3.12 (untested as I didn't feel like bumping my Python version).Just to give another example, a Python object that implements a no-op
__array__
(i.e. returning an already wrapped array) goes from 575ns to 175ns.Draft, because I had to base it on gh-27117 to avoid the conflict.