diff options
author | Dong-hee Na <donghee.na@python.org> | 2021-10-21 23:20:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-21 23:20:43 (GMT) |
commit | 83f202a802b0810e2f0900c6d298fd104390f2ba (patch) | |
tree | f00ba7b9bdc23937581e5cbb9efa33b7e4ee437a /Objects/enumobject.c | |
parent | 37fad7d3b7154c44b9902a2ab0db8641f1a0284b (diff) | |
download | cpython-83f202a802b0810e2f0900c6d298fd104390f2ba.zip cpython-83f202a802b0810e2f0900c6d298fd104390f2ba.tar.gz cpython-83f202a802b0810e2f0900c6d298fd104390f2ba.tar.bz2 |
bpo-43706: Use PEP 590 vectorcall to speed up enumerate() (GH-25154)
Diffstat (limited to 'Objects/enumobject.c')
-rw-r--r-- | Objects/enumobject.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 4513831..b78230d 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -81,6 +81,45 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) return (PyObject *)en; } +// TODO: Use AC when bpo-43447 is supported +static PyObject * +enumerate_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + assert(PyType_Check(type)); + PyTypeObject *tp = (PyTypeObject *)type; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + Py_ssize_t nkwargs = 0; + if (nargs == 0) { + PyErr_SetString(PyExc_TypeError, + "enumerate() missing required argument 'iterable'"); + return NULL; + } + if (kwnames != NULL) { + nkwargs = PyTuple_GET_SIZE(kwnames); + } + + if (nargs + nkwargs == 2) { + if (nkwargs == 1) { + PyObject *kw = PyTuple_GET_ITEM(kwnames, 0); + if (!_PyUnicode_EqualToASCIIString(kw, "start")) { + PyErr_Format(PyExc_TypeError, + "'%S' is an invalid keyword argument for enumerate()", kw); + return NULL; + } + } + return enum_new_impl(tp, args[0], args[1]); + } + + if (nargs == 1 && nkwargs == 0) { + return enum_new_impl(tp, args[0], NULL); + } + + PyErr_Format(PyExc_TypeError, + "enumerate() takes at most 2 arguments (%d given)", nargs + nkwargs); + return NULL; +} + static void enum_dealloc(enumobject *en) { @@ -261,6 +300,7 @@ PyTypeObject PyEnum_Type = { PyType_GenericAlloc, /* tp_alloc */ enum_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ + .tp_vectorcall = (vectorcallfunc)enumerate_vectorcall }; /* Reversed Object ***************************************************************/ |