https://github.com/python/cpython/commit/cffc305e809fc67abc8baae942dc3fd1dea7055a
commit: cffc305e809fc67abc8baae942dc3fd1dea7055a
branch: main
author: Tan Long <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-22T00:12:27-08:00
summary:

gh-162350: Add AC vectorcall for method.__new__ to speed up types.MethodType 
(#162408)

Add `@vectorcall` to Argument Clinic to generate `method.__new__`. Rename 
`method_new` as `method_type_new` to avoid the AC generated verctorcall name 
conflicting with existing `method_vectorcall`. Didn't add 
`Py_TPFLAGS_IMMUTABLETYPE` as this flag 
[automatically](https://docs.python.org/3/c-api/typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE)
 applies to static types.

Performance before:
```
Constructor microbenchmark (best of 7, 1000000 iterations each)
MethodType(f, obj)                   0.1073 s     107.3 ns/op
types.MethodType(f, obj)             0.1142 s     114.2 ns/op
MethodType(C.m, obj)                 0.1224 s     122.4 ns/op
call bound method m()                0.0944 s      94.4 ns/op
```

Performance after:
```
Constructor microbenchmark (best of 7, 1000000 iterations each)
MethodType(f, obj)                   0.0805 s      80.5 ns/op  (24.97% faster)
types.MethodType(f, obj)             0.0862 s      86.2 ns/op  (24.51% faster)
MethodType(C.m, obj)                 0.0927 s      92.7 ns/op  (24.26% faster)
call bound method m()                0.0946 s      94.6 ns/op  (-0.21% faster)
```

```python
import timeit

SETUP = """
import sys
import types
from types import MethodType

def f(self):
    pass

class C:
    def m(self):
        pass

obj = C()
m = MethodType(f, obj)
"""

CASES = [
    ("MethodType(f, obj)", "MethodType(f, obj)"),
    ("types.MethodType(f, obj)", "types.MethodType(f, obj)"),
    ("MethodType(C.m, obj)", "MethodType(C.m, obj)"),
    ("call bound method m()", "m()"),
]

NUMBER = 1_000_000
REPEAT = 7

print("Constructor microbenchmark (best of %d, %d iterations each)" % (REPEAT, 
NUMBER))
for name, stmt in CASES:
    times = timeit.repeat(stmt=stmt, setup=SETUP, number=NUMBER, repeat=REPEAT)
    best = min(times)
    print("%-34s %8.4f s   %7.1f ns/op" % (name, best, best / NUMBER * 1e9))
```

files:
M Objects/classobject.c
M Objects/clinic/classobject.c.h

diff --git a/Objects/classobject.c b/Objects/classobject.c
index 238f1c1dad7d866..dbfdc9262cf0202 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -182,8 +182,9 @@ 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
     /
@@ -192,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=4e32facc3c3108ae]*/
+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,
@@ -323,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),
@@ -331,7 +333,8 @@ PyTypeObject PyMethod_Type = {
     .tp_members = method_memberlist,
     .tp_getset = method_getset,
     .tp_descr_get = method_descr_get,
-    .tp_new = method_new,
+    .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 5934f1c2a41669f..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,7 +48,34 @@ 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_type_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_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));
+    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_type_new_impl(_PyType_CAST(type), function, 
instance);
 
 exit:
     return return_value;
@@ -82,4 +110,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=fd5c214ee6e55e88 input=a9049054013a1b77]*/

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to