bpo-37207: Use PEP 590 vectorcall to speed up dict() (GH-19280)

This commit is contained in:
Dong-hee Na 2020-04-02 09:55:43 +09:00 committed by GitHub
parent 224e1c34d6
commit e27916b1fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Speed up calls to ``dict()`` by using the :pep:`590` ``vectorcall`` calling
convention.

View File

@ -3342,6 +3342,38 @@ 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) {
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)
{
@ -3400,6 +3432,7 @@ PyTypeObject PyDict_Type = {
PyType_GenericAlloc, /* tp_alloc */
dict_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = dict_vectorcall,
};
PyObject *