From 0df98222bf428dd076ba2f06608402fa07e27c35 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Fri, 20 Nov 2026 22:35:34 -0800 Subject: [PATCH] gh-162258: Use vectorcall for slot wrappers Add a `vectorcall` member to `PyWrapperDescrObject` so slots which don't require a args tuple or kwargs dict can avoid constructing them. Wrappers which take a keywords dict (`__init__` and `__call__`) keep calling through `tp_call`, as `METH_VARARGS` functions do: with vectorcall, `PyObject_Call()` with `**kwargs` would unpack the dict into an array only for the wrapper to build a new dict. Removing the `PyWrapperFlag_KEYWORDS` check in `PyDescr_NewWrapper()` shows it: `dict.__init__({}, **kw)` goes from 70.6 ns to 99.0 ns. The object grows by one pointer (`sys.getsizeof(object.__init__)`: 72 -> 80 bytes) but stays in the same allocator size class: on the default build, `sys.getallocatedblocks()` and max RSS are unchanged at startup (about 1,000 slot wrappers) and after importing asyncio, json, dataclasses, collections, decimal, typing, re, email and http.client. Results and error messages are unchanged. | Benchmark | main | PR | |---------------------------------|:-------:|:---------------------:| | `c['x'] = 1` | 35.5 ns | 26.6 ns: 1.33x faster | | `int.__add__(1, 2)` | 26.9 ns | 21.5 ns: 1.25x faster | | `object.__setattr__(o, 'x', 1)` | 37.2 ns | 30.8 ns: 1.21x faster | | `frozen_triple(1, 2, 3)` | 156 ns | 132 ns: 1.18x faster | | `dict.__init__({}, **kw)` | 68.9 ns | 70.9 ns: 1.03x slower | `dict_subclass()`, `list_subclass()` and `pair(1, 2)` were not significantly different. `dict_subclass()`, `list_subclass()` and `dict.__init__({}, **kw)` call `__init__`, which takes the same path as before. Across builds of the same code they moved by up to 7 ns either way. Under callgrind, `dict.__init__({}, **kw)` costs about 20 more instructions per call in the call path (the `self` reference and type check); `perf stat` pinned to one core shows no cycle difference for the subclass rows beyond run-to-run variance. Linux x86-64, clang PGO+LTO builds, pyperf: ```python import pyperf runner = pyperf.Runner() setup = """ from collections import Counter from dataclasses import dataclass @dataclass(frozen=True) class frozen_triple: a: int b: int c: int @dataclass class pair: a: int b: int class C: pass class list_subclass(list): def __init__(self): super().__init__() class dict_subclass(dict): def __init__(self): super().__init__(a=1) o = C() c = Counter() kw = {'x': 1} """ for stmt in [ "c['x'] = 1", "object.__setattr__(o, 'x', 1)", "int.__add__(1, 2)", "frozen_triple(1, 2, 3)", "dict_subclass()", "list_subclass()", "dict.__init__({}, **kw)", "pair(1, 2)", ]: runner.timeit(stmt, stmt, setup) ``` Co-Authored-By: Claude Opus 5.5 --- Include/cpython/descrobject.h | 1 + Lib/test/test_descr.py | 19 ++++++++++++ Lib/test/test_sys.py | 2 +- Objects/descrobject.c | 55 ++++++++++++++++++++++++++++------- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/Include/cpython/descrobject.h b/Include/cpython/descrobject.h index bbad8b59c225ab..70e87092ddbb91 100644 --- a/Include/cpython/descrobject.h +++ b/Include/cpython/descrobject.h @@ -55,6 +55,7 @@ typedef struct { PyDescr_COMMON; struct wrapperbase *d_base; void *d_wrapped; /* This can be any function pointer */ + vectorcallfunc vectorcall; } PyWrapperDescrObject; PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *, diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 2f5953ba096d43..cce859a6a2a609 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1,5 +1,6 @@ import builtins import copyreg +import functools import gc import itertools import math @@ -3791,6 +3792,8 @@ def f(a): return a bytearray.__init__(ba, 'abc\xbd\u20ac', encoding='latin1', errors='replace') self.assertEqual(ba, b'abc\xbd?') + with self.assertRaisesRegex(TypeError, 'takes no keyword arguments'): + int.__add__(1, 2, x=1) @support.skip_if_huge_c_stack() @support.skip_wasi_stack_overflow() @@ -5201,6 +5204,22 @@ class Foo: self.assertRaises(RecursionError, str, foo) self.assertRaises(RecursionError, repr, foo) + def test_slot_wrapper_holds_self(self): + # Make sure wrapper takes a strong reference to itself when called. + # cmp_to_key() does not own the objects passed to it. Custom list type + # so we can take a weakref. + class L(list): + pass + class Evil: + def __eq__(evil, other): + k.obj = None + self.assertIsNotNone(wr()) + return True + K = functools.cmp_to_key(list.__eq__) + k = K(L([Evil()])) + wr = weakref.ref(k.obj) + k < K([1]) + def test_mixing_slot_wrappers(self): class X(dict): __setattr__ = dict.__setitem__ diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 3b65bf5128cdbf..873cee40b12d0b 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1772,7 +1772,7 @@ def inner(): import collections check(collections.defaultdict.default_factory, size('3PP')) # wrapper_descriptor (descriptor object) - check(int.__add__, size('3P2P')) + check(int.__add__, size('3P3P')) # method-wrapper (descriptor object) check({}.__iter__, size('2P')) # empty dict diff --git a/Objects/descrobject.c b/Objects/descrobject.c index c4e525797d19ae..ae174af5992a1f 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -535,16 +535,14 @@ wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self, } static PyObject * -wrapperdescr_call(PyObject *_descr, PyObject *args, PyObject *kwds) +wrapperdescr_impl(PyObject *_descr, PyObject *const *args, + Py_ssize_t nargs, PyObject *kwds) { PyWrapperDescrObject *descr = (PyWrapperDescrObject *)_descr; - Py_ssize_t argc; PyObject *self, *result; /* Make sure that the first argument is acceptable as 'self' */ - assert(PyTuple_Check(args)); - argc = PyTuple_GET_SIZE(args); - if (argc < 1) { + if (nargs < 1) { PyErr_Format(PyExc_TypeError, "descriptor '%V' of '%.100s' " "object needs an argument", @@ -552,7 +550,7 @@ wrapperdescr_call(PyObject *_descr, PyObject *args, PyObject *kwds) PyDescr_TYPE(descr)->tp_name); return NULL; } - self = PyTuple_GET_ITEM(args, 0); + self = args[0]; if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self), (PyObject *)PyDescr_TYPE(descr))) { PyErr_Format(PyExc_TypeError, @@ -565,12 +563,41 @@ wrapperdescr_call(PyObject *_descr, PyObject *args, PyObject *kwds) return NULL; } - args = PyTuple_GetSlice(args, 1, argc); - if (args == NULL) { + PyObject *argstuple = PyTuple_FromArray(args + 1, nargs - 1); + if (argstuple == NULL) { return NULL; } - result = wrapperdescr_raw_call(descr, self, args, kwds); - Py_DECREF(args); + /* Turn vectorcall borrowed reference into a strong reference */ + Py_INCREF(self); + result = wrapperdescr_raw_call(descr, self, argstuple, kwds); + Py_DECREF(self); + Py_DECREF(argstuple); + return result; +} + +static PyObject * +wrapperdescr_call(PyObject *descr, PyObject *args, PyObject *kwds) +{ + return wrapperdescr_impl(descr, _PyTuple_ITEMS(args), + PyTuple_GET_SIZE(args), kwds); +} + +static PyObject * +wrapperdescr_vectorcall(PyObject *descr, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyThreadState *tstate = _PyThreadState_GET(); + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) > 0) { + /* These wrappers take no keywords: let tp_call raise */ + return _PyObject_MakeTpCall(tstate, descr, args, nargs, kwnames); + } + PyObject *result = NULL; + if (!_Py_EnterRecursiveCallTstate(tstate, + " while calling a Python object")) { + result = wrapperdescr_impl(descr, args, nargs, NULL); + _Py_LeaveRecursiveCallTstate(tstate); + } return result; } @@ -881,7 +908,7 @@ PyTypeObject PyWrapperDescr_Type = { sizeof(PyWrapperDescrObject), 0, descr_dealloc, /* tp_dealloc */ - 0, /* tp_vectorcall_offset */ + offsetof(PyWrapperDescrObject, vectorcall), /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_as_async */ @@ -896,6 +923,7 @@ PyTypeObject PyWrapperDescr_Type = { 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */ 0, /* tp_doc */ descr_traverse, /* tp_traverse */ @@ -1030,6 +1058,11 @@ PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped) if (descr != NULL) { descr->d_base = base; descr->d_wrapped = wrapped; + /* Like METH_VARARGS functions, wrappers taking a keywords dict + (__init__, __call__) are faster through tp_call */ + if (!(base->flags & PyWrapperFlag_KEYWORDS)) { + descr->vectorcall = wrapperdescr_vectorcall; + } } return (PyObject *)descr; }