From d95758bfcaa436e5cc99b4ab96b3ca47d6b4a11f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sat, 7 May 2022 20:15:17 +0200 Subject: [PATCH 1/4] PERF Fast check on numpy scalars in PyUFuncOverride_GetNonDefaultArrayUfunc --- numpy/core/src/common/ufunc_override.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/numpy/core/src/common/ufunc_override.c b/numpy/core/src/common/ufunc_override.c index 2c3dc5cb34a9..0eee69f4d561 100644 --- a/numpy/core/src/common/ufunc_override.c +++ b/numpy/core/src/common/ufunc_override.c @@ -6,6 +6,8 @@ #include "npy_import.h" #include "ufunc_override.h" +#define PyObject_CheckExact_Type(op, checktype) (((PyObject*)(op))->ob_type == &checktype) + /* * Check whether an object has __array_ufunc__ defined on its class and it * is not the default, i.e., the object is not an ndarray, and its @@ -30,6 +32,11 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) if (PyArray_CheckExact(obj)) { return NULL; } + /* Fast return for most common numpy scalar types */ + if (PyObject_CheckExact_Type(obj, PyDoubleArrType_Type) || PyObject_CheckExact_Type(obj, PyIntArrType_Type)) { + return NULL; + } + /* * Does the class define __array_ufunc__? (Note that LookupSpecial has fast * return for basic python types, so no need to worry about those here) From 35542e2d43ec3aab104b6b79371b003758c3bd39 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 8 May 2022 21:54:25 +0200 Subject: [PATCH 2/4] check on all numpy scalar types --- numpy/core/src/common/get_attr_string.h | 2 +- numpy/core/src/common/ufunc_override.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/numpy/core/src/common/get_attr_string.h b/numpy/core/src/common/get_attr_string.h index a3e5d5ec835c..90eca5ee65c9 100644 --- a/numpy/core/src/common/get_attr_string.h +++ b/numpy/core/src/common/get_attr_string.h @@ -42,7 +42,7 @@ _is_basic_python_type(PyTypeObject *tp) * 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. + * at builtin types. It does check base ndarray and numpy scalar types. * * In future, could be made more like _Py_LookupSpecial */ diff --git a/numpy/core/src/common/ufunc_override.c b/numpy/core/src/common/ufunc_override.c index 0eee69f4d561..1b7390cdb6ca 100644 --- a/numpy/core/src/common/ufunc_override.c +++ b/numpy/core/src/common/ufunc_override.c @@ -5,6 +5,7 @@ #include "get_attr_string.h" #include "npy_import.h" #include "ufunc_override.h" +#include "scalartypes.h" #define PyObject_CheckExact_Type(op, checktype) (((PyObject*)(op))->ob_type == &checktype) @@ -32,8 +33,9 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) if (PyArray_CheckExact(obj)) { return NULL; } - /* Fast return for most common numpy scalar types */ - if (PyObject_CheckExact_Type(obj, PyDoubleArrType_Type) || PyObject_CheckExact_Type(obj, PyIntArrType_Type)) { + /* Fast return for numpy scalar types */ + if (is_anyscalar_exact(obj) ) + { return NULL; } From 17e729afc3edbe71390272ee32b9a58c0cb5c49c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 9 May 2022 11:31:45 +0200 Subject: [PATCH 3/4] Update numpy/core/src/common/ufunc_override.c Co-authored-by: Sebastian Berg --- numpy/core/src/common/ufunc_override.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/numpy/core/src/common/ufunc_override.c b/numpy/core/src/common/ufunc_override.c index 1b7390cdb6ca..45cbe1a195f2 100644 --- a/numpy/core/src/common/ufunc_override.c +++ b/numpy/core/src/common/ufunc_override.c @@ -34,8 +34,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) return NULL; } /* Fast return for numpy scalar types */ - if (is_anyscalar_exact(obj) ) - { + if (is_anyscalar_exact(obj)) { return NULL; } From 5e1ec9f6f5c0c2c60c7745de4610597d92204007 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 9 May 2022 11:32:55 +0200 Subject: [PATCH 4/4] address review comments --- numpy/core/src/common/ufunc_override.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/numpy/core/src/common/ufunc_override.c b/numpy/core/src/common/ufunc_override.c index 1b7390cdb6ca..bcfd74688b1a 100644 --- a/numpy/core/src/common/ufunc_override.c +++ b/numpy/core/src/common/ufunc_override.c @@ -7,8 +7,6 @@ #include "ufunc_override.h" #include "scalartypes.h" -#define PyObject_CheckExact_Type(op, checktype) (((PyObject*)(op))->ob_type == &checktype) - /* * Check whether an object has __array_ufunc__ defined on its class and it * is not the default, i.e., the object is not an ndarray, and its