mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
bpo-46481: Implement vectorcall for weakref.ref.__call__ method. (GH-30820)
This commit is contained in:
parent
1f715d5bd3
commit
76dc047a0e
4 changed files with 35 additions and 52 deletions
|
@ -29,6 +29,7 @@ struct _PyWeakReference {
|
||||||
*/
|
*/
|
||||||
PyWeakReference *wr_prev;
|
PyWeakReference *wr_prev;
|
||||||
PyWeakReference *wr_next;
|
PyWeakReference *wr_next;
|
||||||
|
vectorcallfunc vectorcall;
|
||||||
};
|
};
|
||||||
|
|
||||||
PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
|
PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
|
||||||
|
|
|
@ -1538,11 +1538,11 @@ class SizeofTest(unittest.TestCase):
|
||||||
# TODO: add check that forces layout of unicodefields
|
# TODO: add check that forces layout of unicodefields
|
||||||
# weakref
|
# weakref
|
||||||
import weakref
|
import weakref
|
||||||
check(weakref.ref(int), size('2Pn2P'))
|
check(weakref.ref(int), size('2Pn3P'))
|
||||||
# weakproxy
|
# weakproxy
|
||||||
# XXX
|
# XXX
|
||||||
# weakcallableproxy
|
# weakcallableproxy
|
||||||
check(weakref.proxy(int), size('2Pn2P'))
|
check(weakref.proxy(int), size('2Pn3P'))
|
||||||
|
|
||||||
def check_slots(self, obj, base, extra):
|
def check_slots(self, obj, base, extra):
|
||||||
expected = sys.getsizeof(base) + struct.calcsize(extra)
|
expected = sys.getsizeof(base) + struct.calcsize(extra)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Speed up calls to :meth:`weakref.ref.__call__` by using the :pep:`590`
|
||||||
|
``vectorcall`` calling convention. Patch by Dong-hee Na.
|
|
@ -19,6 +19,7 @@ _PyWeakref_GetWeakrefCount(PyWeakReference *head)
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *weakref_vectorcall(PyWeakReference *self, PyObject *const *args, size_t nargsf, PyObject *kwnames);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
|
init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
|
||||||
|
@ -27,8 +28,8 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
|
||||||
self->wr_object = ob;
|
self->wr_object = ob;
|
||||||
self->wr_prev = NULL;
|
self->wr_prev = NULL;
|
||||||
self->wr_next = NULL;
|
self->wr_next = NULL;
|
||||||
Py_XINCREF(callback);
|
self->wr_callback = Py_XNewRef(callback);
|
||||||
self->wr_callback = callback;
|
self->vectorcall = (vectorcallfunc)weakref_vectorcall;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyWeakReference *
|
static PyWeakReference *
|
||||||
|
@ -128,19 +129,19 @@ gc_clear(PyWeakReference *self)
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
|
weakref_vectorcall(PyWeakReference *self, PyObject *const *args,
|
||||||
|
size_t nargsf, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {NULL};
|
if (!_PyArg_NoKwnames("weakref", kwnames)) {
|
||||||
|
|
||||||
if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) {
|
|
||||||
PyObject *object = PyWeakref_GET_OBJECT(self);
|
|
||||||
Py_INCREF(object);
|
|
||||||
return (object);
|
|
||||||
}
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
||||||
|
if (!_PyArg_CheckPositional("weakref", nargs, 0, 0)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return Py_NewRef(PyWeakref_GET_OBJECT(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Py_hash_t
|
static Py_hash_t
|
||||||
weakref_hash(PyWeakReference *self)
|
weakref_hash(PyWeakReference *self)
|
||||||
{
|
{
|
||||||
|
@ -371,45 +372,24 @@ static PyMethodDef weakref_methods[] = {
|
||||||
PyTypeObject
|
PyTypeObject
|
||||||
_PyWeakref_RefType = {
|
_PyWeakref_RefType = {
|
||||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||||
"weakref",
|
.tp_name = "weakref",
|
||||||
sizeof(PyWeakReference),
|
.tp_basicsize = sizeof(PyWeakReference),
|
||||||
0,
|
.tp_dealloc = weakref_dealloc,
|
||||||
weakref_dealloc, /*tp_dealloc*/
|
.tp_vectorcall_offset = offsetof(PyWeakReference, vectorcall),
|
||||||
0, /*tp_vectorcall_offset*/
|
.tp_call = PyVectorcall_Call,
|
||||||
0, /*tp_getattr*/
|
.tp_repr = (reprfunc)weakref_repr,
|
||||||
0, /*tp_setattr*/
|
.tp_hash = (hashfunc)weakref_hash,
|
||||||
0, /*tp_as_async*/
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
||||||
(reprfunc)weakref_repr, /*tp_repr*/
|
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_BASETYPE,
|
||||||
0, /*tp_as_number*/
|
.tp_traverse = (traverseproc)gc_traverse,
|
||||||
0, /*tp_as_sequence*/
|
.tp_clear = (inquiry)gc_clear,
|
||||||
0, /*tp_as_mapping*/
|
.tp_richcompare = (richcmpfunc)weakref_richcompare,
|
||||||
(hashfunc)weakref_hash, /*tp_hash*/
|
.tp_methods = weakref_methods,
|
||||||
(ternaryfunc)weakref_call, /*tp_call*/
|
.tp_members = weakref_members,
|
||||||
0, /*tp_str*/
|
.tp_init = weakref___init__,
|
||||||
0, /*tp_getattro*/
|
.tp_alloc = PyType_GenericAlloc,
|
||||||
0, /*tp_setattro*/
|
.tp_new = weakref___new__,
|
||||||
0, /*tp_as_buffer*/
|
.tp_free = PyObject_GC_Del,
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
|
|
||||||
| Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
|
||||||
0, /*tp_doc*/
|
|
||||||
(traverseproc)gc_traverse, /*tp_traverse*/
|
|
||||||
(inquiry)gc_clear, /*tp_clear*/
|
|
||||||
(richcmpfunc)weakref_richcompare, /*tp_richcompare*/
|
|
||||||
0, /*tp_weaklistoffset*/
|
|
||||||
0, /*tp_iter*/
|
|
||||||
0, /*tp_iternext*/
|
|
||||||
weakref_methods, /*tp_methods*/
|
|
||||||
weakref_members, /*tp_members*/
|
|
||||||
0, /*tp_getset*/
|
|
||||||
0, /*tp_base*/
|
|
||||||
0, /*tp_dict*/
|
|
||||||
0, /*tp_descr_get*/
|
|
||||||
0, /*tp_descr_set*/
|
|
||||||
0, /*tp_dictoffset*/
|
|
||||||
weakref___init__, /*tp_init*/
|
|
||||||
PyType_GenericAlloc, /*tp_alloc*/
|
|
||||||
weakref___new__, /*tp_new*/
|
|
||||||
PyObject_GC_Del, /*tp_free*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue