diff options
author | Dong-hee Na <donghee.na@python.org> | 2021-03-10 16:39:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-10 16:39:52 (GMT) |
commit | 9a9c11ad41d7887f3d6440e0f0e8966156d959b5 (patch) | |
tree | 8eb4b11a62f0a31290d65544252640cdb9c50231 /Python/bltinmodule.c | |
parent | 0a30f0e93495355900fc7add10cc3bddd648bac5 (diff) | |
download | cpython-9a9c11ad41d7887f3d6440e0f0e8966156d959b5.zip cpython-9a9c11ad41d7887f3d6440e0f0e8966156d959b5.tar.gz cpython-9a9c11ad41d7887f3d6440e0f0e8966156d959b5.tar.bz2 |
bpo-43287: Use PEP 590 vectorcall to speed up filter() (GH-24611)
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index dec2984..52591c4 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -533,8 +533,40 @@ filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(it); return NULL; } - Py_INCREF(func); - lz->func = func; + + lz->func = Py_NewRef(func); + lz->it = it; + + return (PyObject *)lz; +} + +static PyObject * +filter_vectorcall(PyObject *type, PyObject * const*args, + size_t nargsf, PyObject *kwnames) +{ + PyTypeObject *tp = (PyTypeObject *)type; + if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) { + return NULL; + } + + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) { + return NULL; + } + + PyObject *it = PyObject_GetIter(args[1]); + if (it == NULL) { + return NULL; + } + + filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0); + + if (lz == NULL) { + Py_DECREF(it); + return NULL; + } + + lz->func = Py_NewRef(args[0]); lz->it = it; return (PyObject *)lz; @@ -653,6 +685,7 @@ PyTypeObject PyFilter_Type = { PyType_GenericAlloc, /* tp_alloc */ filter_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ + .tp_vectorcall = (vectorcallfunc)filter_vectorcall }; |