Skip to content

Commit cffc305

Browse files
authored
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)) ```
1 parent 1cc5802 commit cffc305

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

‎Objects/classobject.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ method_getattro(PyObject *obj, PyObject *name)
182182
}
183183

184184
/*[clinic input]
185+
@vectorcall
185186
@classmethod
186-
method.__new__ as method_new
187+
method.__new__ as method_type_new
187188
function: object
188189
instance: object
189190
/
@@ -192,8 +193,9 @@ Create a bound instance method object.
192193
[clinic start generated code]*/
193194

194195
static PyObject *
195-
method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance)
196-
/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/
196+
method_type_new_impl(PyTypeObject *type, PyObject *function,
197+
PyObject *instance)
198+
/*[clinic end generated code: output=e9877864a8bfdf70 input=70c1e0da5d6267f5]*/
197199
{
198200
if (!PyCallable_Check(function)) {
199201
PyErr_SetString(PyExc_TypeError,
@@ -323,15 +325,16 @@ PyTypeObject PyMethod_Type = {
323325
.tp_setattro = PyObject_GenericSetAttr,
324326
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
325327
Py_TPFLAGS_HAVE_VECTORCALL,
326-
.tp_doc = method_new__doc__,
328+
.tp_doc = method_type_new__doc__,
327329
.tp_traverse = method_traverse,
328330
.tp_richcompare = method_richcompare,
329331
.tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist),
330332
.tp_methods = method_methods,
331333
.tp_members = method_memberlist,
332334
.tp_getset = method_getset,
333335
.tp_descr_get = method_descr_get,
334-
.tp_new = method_new,
336+
.tp_new = method_type_new,
337+
.tp_vectorcall = method_type_vectorcall,
335338
};
336339

337340
/* ------------------------------------------------------------------------

‎Objects/clinic/classobject.c.h‎

Lines changed: 33 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)