From 8ae337b169ecd0175225b28aec4298ef0546a9e4 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 2 Apr 2020 00:25:23 +0900 Subject: [PATCH 1/2] bpo-37207: Use PEP 590 vectorcall to speed up dict() --- .../2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst | 2 ++ Objects/dictobject.c | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst new file mode 100644 index 00000000000000..cb5e9ff5b8f8e8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst @@ -0,0 +1,2 @@ +Speed up calls to ``dict()`` by using the :pep:`590` ``vectorcall`` calling +convention. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 2ca32b5a9d2a3a..157e5304f69879 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -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) { + 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) { @@ -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 * From 35259b4740f59707e7cafd72e37f45d206073c16 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 2 Apr 2020 01:07:45 +0900 Subject: [PATCH 2/2] bpo-37207: Apply Victor's code review --- Objects/dictobject.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 157e5304f69879..e5f7005d49f23a 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3363,14 +3363,12 @@ dict_vectorcall(PyObject *type, PyObject * const*args, } args++; } - if (kwnames == NULL) { - 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; + 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;