diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst new file mode 100644 index 000000000000000..096099c3013b8bb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst @@ -0,0 +1,2 @@ +Speed up calling :class:`types.MethodType` by adding a vectorcall +implementation of the constructor generated by Argument Clinic. diff --git a/Objects/classobject.c b/Objects/classobject.c index 238f1c1dad7d866..243d50add01fd3b 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -44,8 +44,8 @@ PyMethod_Self(PyObject *im) static PyObject * -method_vectorcall(PyObject *method, PyObject *const *args, - size_t nargsf, PyObject *kwnames) +method_instance_vectorcall(PyObject *method, PyObject *const *args, + size_t nargsf, PyObject *kwnames) { assert(Py_IS_TYPE(method, &PyMethod_Type)); @@ -78,7 +78,7 @@ PyMethod_New(PyObject *func, PyObject *self) im->im_weakreflist = NULL; im->im_func = Py_NewRef(func); im->im_self = Py_NewRef(self); - im->vectorcall = method_vectorcall; + im->vectorcall = method_instance_vectorcall; _PyObject_GC_TRACK(im); return (PyObject *)im; } @@ -182,6 +182,7 @@ method_getattro(PyObject *obj, PyObject *name) } /*[clinic input] +@vectorcall @classmethod method.__new__ as method_new function: object @@ -193,7 +194,7 @@ Create a bound instance method object. static PyObject * method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance) -/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/ +/*[clinic end generated code: output=d33ef4ebf702e1f7 input=5e5e5a509ac9bc99]*/ { if (!PyCallable_Check(function)) { PyErr_SetString(PyExc_TypeError, @@ -332,6 +333,7 @@ PyTypeObject PyMethod_Type = { .tp_getset = method_getset, .tp_descr_get = method_descr_get, .tp_new = method_new, + .tp_vectorcall = method_vectorcall, }; /* ------------------------------------------------------------------------ diff --git a/Objects/clinic/classobject.c.h b/Objects/clinic/classobject.c.h index 5934f1c2a41669f..241aecb4716335d 100644 --- a/Objects/clinic/classobject.c.h +++ b/Objects/clinic/classobject.c.h @@ -53,6 +53,33 @@ method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +method_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *function; + PyObject *instance; + + assert(_PyType_CAST(type)->tp_new == method_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("method", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("method", nargs, 2, 2)) { + goto exit; + } + function = args[0]; + instance = args[1]; + return_value = method_new_impl(_PyType_CAST(type), function, instance); + +exit: + return return_value; +} + PyDoc_STRVAR(instancemethod_new__doc__, "instancemethod(function, /)\n" "--\n" @@ -82,4 +109,4 @@ instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=ab546abf90aac94e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=991dca7bbf4cb8ac input=a9049054013a1b77]*/