Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up calls to ``list()`` by using the PEP 590 ``vectorcall``
Comment thread
encukou marked this conversation as resolved.
Resolved
calling convention. Patch by Mark Shannon.
30 changes: 30 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,35 @@ list___init___impl(PyListObject *self, PyObject *iterable)
return 0;
}

static PyObject *
list_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #18980 is merged.
We can replace the logic into

if (!_PyArg_NoKwnames("list", kwnames)) {
    return NULL;
}

PyErr_Format(PyExc_TypeError, "list() takes no keyword arguments");
return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (nargs > 1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace it into

if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
    return NULL;
}

PyErr_Format(PyExc_TypeError, "list() expected at most 1 argument, got %zd", nargs);
return NULL;
}

assert(PyType_Check(type));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this assert into the top of the function body since this parameter is the 1st parameter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why that's necessary.

PyObject *list = PyType_GenericAlloc((PyTypeObject *)type, 0);
if (list == NULL) {
return NULL;
}
if (nargs) {
if (list___init___impl((PyListObject *)list, args[0])) {
Py_DECREF(list);
return NULL;
}
}
return list;
}


/*[clinic input]
list.__sizeof__

Expand Down Expand Up @@ -3032,6 +3061,7 @@ PyTypeObject PyList_Type = {
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = list_vectorcall,
};

/*********************** List Iterator **************************/
Expand Down