Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-37207: Use PEP 590 vectorcall to speed up dict()
  • Loading branch information
corona10 committed Apr 1, 2020
commit 8ae337b169ecd0175225b28aec4298ef0546a9e4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up calls to ``dict()`` by using the :pep:`590` ``vectorcall`` calling
convention.
35 changes: 35 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3342,6 +3342,40 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
return dict_update_common(self, args, kwds, "dict");
}

static PyObject *
dict_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
assert(PyType_Check(type));
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
return NULL;
}

PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
if (self == NULL) {
return NULL;
}
if (nargs == 1) {
if (dict_update_arg(self, args[0]) < 0) {
Py_DECREF(self);
return NULL;
}
args++;
}
if (kwnames == NULL) {
Comment thread
corona10 marked this conversation as resolved.
Outdated
return self;
}

for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
Py_DECREF(self);
return NULL;
}
}
return self;
}

static PyObject *
dict_iter(PyDictObject *dict)
{
Expand Down Expand Up @@ -3400,6 +3434,7 @@ PyTypeObject PyDict_Type = {
PyType_GenericAlloc, /* tp_alloc */
dict_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = dict_vectorcall,
};

PyObject *
Expand Down