From fb721e082697bd1e4ccea00bb53d6a29429a07b6 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 04:55:38 +0800 Subject: [PATCH 1/4] MethodType AC vectorcall --- Objects/classobject.c | 10 ++++++---- Objects/clinic/classobject.c.h | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) 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]*/ From a9531abb760bbcb42cd4cceb0811de0e19f8243a Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 06:28:27 +0800 Subject: [PATCH 2/4] blurb --- .../2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst 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. From f3f7de41bf66a0894738e88e2546fea646236838 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 15:06:39 +0800 Subject: [PATCH 3/4] keep original method_vectorcall untouched --- Objects/classobject.c | 19 ++++++++++--------- Objects/clinic/classobject.c.h | 17 +++++++++-------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Objects/classobject.c b/Objects/classobject.c index 243d50add01fd3b..dbfdc9262cf0202 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -44,8 +44,8 @@ PyMethod_Self(PyObject *im) static PyObject * -method_instance_vectorcall(PyObject *method, PyObject *const *args, - size_t nargsf, PyObject *kwnames) +method_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_instance_vectorcall; + im->vectorcall = method_vectorcall; _PyObject_GC_TRACK(im); return (PyObject *)im; } @@ -184,7 +184,7 @@ method_getattro(PyObject *obj, PyObject *name) /*[clinic input] @vectorcall @classmethod -method.__new__ as method_new +method.__new__ as method_type_new function: object instance: object / @@ -193,8 +193,9 @@ Create a bound instance method object. [clinic start generated code]*/ static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance) -/*[clinic end generated code: output=d33ef4ebf702e1f7 input=5e5e5a509ac9bc99]*/ +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance) +/*[clinic end generated code: output=e9877864a8bfdf70 input=70c1e0da5d6267f5]*/ { if (!PyCallable_Check(function)) { PyErr_SetString(PyExc_TypeError, @@ -324,7 +325,7 @@ PyTypeObject PyMethod_Type = { .tp_setattro = PyObject_GenericSetAttr, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_VECTORCALL, - .tp_doc = method_new__doc__, + .tp_doc = method_type_new__doc__, .tp_traverse = method_traverse, .tp_richcompare = method_richcompare, .tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist), @@ -332,8 +333,8 @@ PyTypeObject PyMethod_Type = { .tp_members = method_memberlist, .tp_getset = method_getset, .tp_descr_get = method_descr_get, - .tp_new = method_new, - .tp_vectorcall = method_vectorcall, + .tp_new = method_type_new, + .tp_vectorcall = method_type_vectorcall, }; /* ------------------------------------------------------------------------ diff --git a/Objects/clinic/classobject.c.h b/Objects/clinic/classobject.c.h index 241aecb4716335d..1b96cc2212a826f 100644 --- a/Objects/clinic/classobject.c.h +++ b/Objects/clinic/classobject.c.h @@ -21,17 +21,18 @@ method___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) return method___reduce___impl((PyMethodObject *)self); } -PyDoc_STRVAR(method_new__doc__, +PyDoc_STRVAR(method_type_new__doc__, "method(function, instance, /)\n" "--\n" "\n" "Create a bound instance method object."); static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance); +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance); static PyObject * -method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +method_type_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; PyTypeObject *base_tp = &PyMethod_Type; @@ -47,14 +48,14 @@ method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) } function = PyTuple_GET_ITEM(args, 0); instance = PyTuple_GET_ITEM(args, 1); - return_value = method_new_impl(type, function, instance); + return_value = method_type_new_impl(type, function, instance); exit: return return_value; } static PyObject * -method_vectorcall(PyObject *type, PyObject *const *args, +method_type_vectorcall(PyObject *type, PyObject *const *args, size_t nargsf, PyObject *kwnames) { PyObject *return_value = NULL; @@ -62,7 +63,7 @@ method_vectorcall(PyObject *type, PyObject *const *args, PyObject *function; PyObject *instance; - assert(_PyType_CAST(type)->tp_new == method_new); + assert(_PyType_CAST(type)->tp_new == method_type_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)); @@ -74,7 +75,7 @@ method_vectorcall(PyObject *type, PyObject *const *args, } function = args[0]; instance = args[1]; - return_value = method_new_impl(_PyType_CAST(type), function, instance); + return_value = method_type_new_impl(_PyType_CAST(type), function, instance); exit: return return_value; @@ -109,4 +110,4 @@ instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=991dca7bbf4cb8ac input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fd5c214ee6e55e88 input=a9049054013a1b77]*/ From d619edbb7e5c28e33389994418291be1eb8986d3 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 15:09:14 +0800 Subject: [PATCH 4/4] Revert "blurb" This reverts commit a9531abb760bbcb42cd4cceb0811de0e19f8243a. --- .../2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst 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 deleted file mode 100644 index 096099c3013b8bb..000000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-22-06-28-21.gh-issue-162350.jOQaMn.rst +++ /dev/null @@ -1,2 +0,0 @@ -Speed up calling :class:`types.MethodType` by adding a vectorcall -implementation of the constructor generated by Argument Clinic.