From 60a858a372b14b73547baacf4a472eccfade1073 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 7 Dec 2021 20:17:17 -0600 Subject: ENH: Improve array function overhead by using vectorcall This moves dispatching for `__array_function__` into a C-wrapper. This helps speed for multiple reasons: * Avoids one additional dispatching function call to C * Avoids the use of `*args, **kwargs` which is slower. * For simple NumPy calls we can stay in the faster "vectorcall" world This speeds up things generally a little, but can speed things up a lot when keyword arguments are used on lightweight functions, for example:: np.can_cast(arr, dtype, casting="same_kind") is more than twice as fast with this. There is one alternative in principle to get best speed: We could inline the "relevant argument"/dispatcher extraction. That changes behavior in an acceptable but larger way (passes default arguments). Unless the C-entry point seems unwanted, this should be a decent step in the right direction even if we want to do that eventually, though. Closes gh-20790 Closes gh-18547 (although not quite sure why) --- numpy/core/_asarray.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'numpy/core/_asarray.py') diff --git a/numpy/core/_asarray.py b/numpy/core/_asarray.py index cbaab8c3f..a9abc5a88 100644 --- a/numpy/core/_asarray.py +++ b/numpy/core/_asarray.py @@ -24,10 +24,6 @@ POSSIBLE_FLAGS = { } -def _require_dispatcher(a, dtype=None, requirements=None, *, like=None): - return (like,) - - @set_array_function_like_doc @set_module('numpy') def require(a, dtype=None, requirements=None, *, like=None): @@ -100,10 +96,10 @@ def require(a, dtype=None, requirements=None, *, like=None): """ if like is not None: return _require_with_like( + like, a, dtype=dtype, requirements=requirements, - like=like, ) if not requirements: @@ -135,6 +131,4 @@ def require(a, dtype=None, requirements=None, *, like=None): return arr -_require_with_like = array_function_dispatch( - _require_dispatcher -)(require) +_require_with_like = array_function_dispatch()(require) -- cgit v1.2.1