Skip to content

Commit 9a9c11a

Browse files
authored
bpo-43287: Use PEP 590 vectorcall to speed up filter() (GH-24611)
1 parent 0a30f0e commit 9a9c11a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up calls to ``filter()`` by using the :pep:`590` ``vectorcall``
2+
calling convention. Patch by Dong-hee Na.

Python/bltinmodule.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,40 @@ filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
533533
Py_DECREF(it);
534534
return NULL;
535535
}
536-
Py_INCREF(func);
537-
lz->func = func;
536+
537+
lz->func = Py_NewRef(func);
538+
lz->it = it;
539+
540+
return (PyObject *)lz;
541+
}
542+
543+
static PyObject *
544+
filter_vectorcall(PyObject *type, PyObject * const*args,
545+
size_t nargsf, PyObject *kwnames)
546+
{
547+
PyTypeObject *tp = (PyTypeObject *)type;
548+
if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) {
549+
return NULL;
550+
}
551+
552+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
553+
if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) {
554+
return NULL;
555+
}
556+
557+
PyObject *it = PyObject_GetIter(args[1]);
558+
if (it == NULL) {
559+
return NULL;
560+
}
561+
562+
filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0);
563+
564+
if (lz == NULL) {
565+
Py_DECREF(it);
566+
return NULL;
567+
}
568+
569+
lz->func = Py_NewRef(args[0]);
538570
lz->it = it;
539571

540572
return (PyObject *)lz;
@@ -653,6 +685,7 @@ PyTypeObject PyFilter_Type = {
653685
PyType_GenericAlloc, /* tp_alloc */
654686
filter_new, /* tp_new */
655687
PyObject_GC_Del, /* tp_free */
688+
.tp_vectorcall = (vectorcallfunc)filter_vectorcall
656689
};
657690

658691

0 commit comments

Comments
 (0)