8000 PERF: Improve performance of special attribute lookups by eendebakpt · Pull Request #21423 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

PERF: Improve performance of special attribute lookups #21423

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 4 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
MAINT Refactor PyArray_LookupSpecial and PyArray_LookupSpecial_OnInst…
…ance

Refactor code so the unicode names are interned
  • Loading branch information
eendebakpt committed May 3, 2022
commit a619a7faccb22065282754d303797d7472f41cd0
2 changes: 1 addition & 1 deletion numpy/core/src/common/binop_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ binop_should_defer(PyObject *self, PyObject *other, int inplace)
* Classes with __array_ufunc__ are living in the future, and only need to
* check whether __array_ufunc__ equals None.
*/
attr = PyArray_LookupSpecial(other, "__array_ufunc__");
attr = PyArray_LookupSpecial(other, npy_um_str_array_ufunc);
if (attr != NULL) {
defer = !inplace && (attr == Py_None);
Py_DECREF(attr);
Expand Down
109 changes: 7 additions & 102 deletions numpy/core/src/common/get_attr_string.h
F438
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef NUMPY_CORE_SRC_COMMON_GET_ATTR_STRING_H_
#define NUMPY_CORE_SRC_COMMON_GET_ATTR_STRING_H_

#include <Python.h>
#include "ufunc_object.h"

static NPY_INLINE npy_bool
_is_basic_python_type(PyTypeObject *tp)
{
Expand Down Expand Up @@ -33,43 +36,6 @@ _is_basic_python_type(PyTypeObject *tp)
);
}

/*
* Stripped down version of PyObject_GetAttrString(obj, name) that does not
* raise PyExc_AttributeError.
*
* This allows it to avoid creating then discarding exception objects when
* performing lookups on objects without any attributes.
*
* Returns attribute value on success, NULL without an exception set if
* there is no such attribute, and NULL with an exception on failure.
*/
static NPY_INLINE PyObject *
maybe_get_attr(PyObject *obj, char const *name)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;

/* Attribute referenced by (char *)name */
if (tp->tp_getattr != NULL) {
res = (*tp->tp_getattr)(obj, (char *)name);
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
}
/* Attribute referenced by (PyObject *)name */
else if (tp->tp_getattro != NULL) {
PyObject *w = PyUnicode_InternFromString(name);
if (w == NULL) {
return (PyObject *)NULL;
}
res = (*tp->tp_getattro)(obj, w);
Py_DECREF(w);
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
}
return res;
}

/*
* Lookup a special method, following the python approach of looking up
Expand All @@ -81,78 +47,17 @@ maybe_get_attr(PyObject *obj, char const *name)
* In future, could be made more like _Py_LookupSpecial
*/
static NPY_INLINE PyObject *
PyArray_LookupSpecial(PyObject *obj, char const *name)
PyArray_LookupSpecial(PyObject *obj, PyObject *name_unicode)
{
PyTypeObject *tp = Py_TYPE(obj);

/* We do not need to check for special attributes on trivial types */
if (_is_basic_python_type(tp)) {
return NULL;
}
return maybe_get_attr((PyObject *)tp, name);
}

/*
* Stripped down version of PyObject_GetAttrString(obj, name) that does not
* raise PyExc_AttributeError.
*
* This allows it to avoid creating then discarding exception objects when
* performing lookups on objects without any attributes.
*
* Returns attribute value on success, NULL without an exception set if
* there is no such attribute, and NULL with an exception on failure.
*/
static NPY_INLINE PyObject *
_maybe_get_attr_unicode(PyObject *obj, char const *name, PyObject *name_unicode)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;

/* Attribute referenced by (char *)name */
if (tp->tp_getattr != NULL) {
res = (*tp->tp_getattr)(obj, (char *)name);
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
}
/* Attribute referenced by (PyObject *)name */
else if (tp->tp_getattro != NULL) {
res = (*tp->tp_getattro)(obj, name_unicode);
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
}
return res;
return PyObject_GetAttr((PyObject *)tp, name_unicode);
}

/*
* Lookup a special method "__array_ufunc__", following the python approach of looking up
* on the type object, rather than on the instance itself.
*
* Assumes that the special method is a numpy-specific one, so does not look
* at builtin types, nor does it look at a base ndarray.
*
* In future, could be made more like _Py_LookupSpecial
*/
static NPY_INLINE PyObject *
PyArray_Lookup_Array_UFunc(PyObject *obj)
{
static const char *name = "__array_ufunc__";
static PyObject *name_unicode = NULL;

/* On first entry, cache unicode version of __array_ufunc__ */
if (name_unicode == NULL) {
name_unicode = PyUnicode_InternFromString(name);
}

PyTypeObject *tp = Py_TYPE(obj);

/* We do not need to check for special attributes on trivial types */
if (_is_basic_python_type(tp)) {
return NULL;
}
return _maybe_get_attr_unicode((PyObject *)tp, name, name_unicode);
}

/*
* PyArray_LookupSpecial_OnInstance:
Expand All @@ -163,7 +68,7 @@ PyArray_Lookup_Array_UFunc(PyObject *obj)
* Kept for backwards compatibility. In future, we should deprecate this.
*/
static NPY_INLINE PyObject *
PyArray_LookupSpecial_OnInstance(PyObject *obj, char const *name)
PyArray_LookupSpecial_OnInstance(PyObject *obj, PyObject *name_unicode)
{
PyTypeObject *tp = Py_TYPE(obj);

Expand All @@ -172,7 +77,7 @@ PyArray_LookupSpecial_OnInstance(PyObject *obj, char const *name)
return NULL;
}

return maybe_get_attr(obj, name);
return PyObject_GetAttr(obj, name_unicode);
}

#endif /* NUMPY_CORE_SRC_COMMON_GET_ATTR_STRING_H_ */
2 changes: 1 addition & 1 deletion numpy/core/src/common/ufunc_override.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj)
* Does the class define __array_ufunc__? (Note that LookupSpecial has fast
* return for basic python types, so no need to worry about those here)
*/
cls_array_ufunc = PyArray_Lookup_Array_UFunc(obj);
cls_array_ufunc = PyArray_LookupSpecial(obj, npy_um_str_array_ufunc);
if (cls_array_ufunc == NULL) {
if (PyErr_Occurred()) {
PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/arrayfunction_override.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ get_array_function(PyObject *obj)
return ndarray_array_function;
}

PyObject *array_function = PyArray_LookupSpecial(obj, "__array_function__");
PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str_array_function);
if (array_function == NULL && PyErr_Occurred()) {
PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
}
Expand Down
6 changes: 3 additions & 3 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ PyArray_FromStructInterface(PyObject *input)
PyObject *attr;
char endian = NPY_NATBYTE;

attr = PyArray_LookupSpecial_OnInstance(input, "__array_struct__");
attr = PyArray_LookupSpecial_OnInstance(input, npy_ma_str_array_struct);
if (attr == NULL) {
if (PyErr_Occurred()) {
return NULL;
Expand Down Expand Up @@ -2233,7 +2233,7 @@ PyArray_FromInterface(PyObject *origin)
npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS];
int dataflags = NPY_ARRAY_BEHAVED;

iface = PyArray_LookupSpecial_OnInstance(origin, "__array_interface__");
iface = PyArray_LookupSpecial_OnInstance(origin, npy_ma_str_array_interface);

if (iface == NULL) {
if (PyErr_Occurred()) {
Expand Down Expand Up @@ -2514,7 +2514,7 @@ PyArray_FromArrayAttr_int(
PyObject *new;
PyObject *array_meth;

array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__");
array_meth = PyArray_LookupSpecial_OnInstance(op, npy_ma_str_array);
if (array_meth == NULL) {
if (PyErr_Occurred()) {
return NULL;
Expand Down
27 changes: 26 additions & 1 deletion numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ PyArray_GetPriority(PyObject *obj, double default_)
return NPY_SCALAR_PRIORITY;
}

ret = PyArray_LookupSpecial_OnInstance(obj, "__array_priority__");
ret = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str_array_priority);
if (ret == NULL) {
if (PyErr_Occurred()) {
/* TODO[gh-14801]: propagate crashes during attribute access? */
Expand Down Expand Up @@ -4665,6 +4665,11 @@ set_flaginfo(PyObject *d)
return;
}

NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_function = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_struct = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_interface = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_priority = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_wrap = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_finalize = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_implementation = NULL;
Expand All @@ -4676,6 +4681,26 @@ NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_numpy = NULL;
static int
intern_strings(void)
{
npy_ma_str_array = PyUnicode_InternFromString("__array__");
if (npy_ma_str_array == NULL) {
return -1;
}
npy_ma_str_array_function = PyUnicode_InternFromString("__array_function__");
if (npy_ma_str_array_function == NULL) {
return -1;
}
npy_ma_str_array_struct = PyUnicode_InternFromString("__array_struct__");
if (npy_ma_str_array_struct == NULL) {
return -1;
}
npy_ma_str_array_priority = PyUnicode_InternFromString("__array_priority__");
if (npy_ma_str_array_priority == NULL) {
return -1;
}
npy_ma_str_array_interface = PyUnicode_InternFromString("__array_interface__");
if (npy_ma_str_array_interface == NULL) {
return -1;
}
npy_ma_str_array_wrap = PyUnicode_InternFromString("__array_wrap__");
if (npy_ma_str_array_wrap == NULL) {
return -1;
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_
#define NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_

NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_function;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_struct;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_priority;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_interface;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_wrap;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_finalize;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_implementation;
Expand Down
1 change: 1 addition & 0 deletions numpy/core/src/umath/ufunc_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NPY_NO_EXPORT const char*
ufunc_get_name_cstr(PyUFuncObject *ufunc);

/* strings from umathmodule.c that are interned on umath import */
NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_array_ufunc;
NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_array_prepare;
NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_array_wrap;
NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_pyvals_name;
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args)
*****************************************************************************
*/

NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_array_ufunc = NULL;
NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_array_prepare = NULL;
NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_array_wrap = NULL;
NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_pyvals_name = NULL;
Expand All @@ -221,6 +222,10 @@ NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_pyvals_name = NULL;
static int
intern_strings(void)
{
npy_um_str_array_ufunc = PyUnicode_InternFromString("__array_ufunc__");
if (npy_um_str_array_ufunc == NULL) {
return -1;
}
npy_um_str_array_prepare = PyUnicode_InternFromString("__array_prepare__");
if (npy_um_str_array_prepare == NULL) {
return -1;
Expand Down
0