summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2020-04-02 00:55:43 (GMT)
committerGitHub <noreply@github.com>2020-04-02 00:55:43 (GMT)
commite27916b1fc0364e3627438df48550c16f0b80b82 (patch)
tree17f3242f1e2f1e273b179b15f59e3e801a27ede3
parent224e1c34d677ef42fe665ac008a000d4dcec1398 (diff)
downloadcpython-e27916b1fc0364e3627438df48550c16f0b80b82.zip
cpython-e27916b1fc0364e3627438df48550c16f0b80b82.tar.gz
cpython-e27916b1fc0364e3627438df48550c16f0b80b82.tar.bz2
bpo-37207: Use PEP 590 vectorcall to speed up dict() (GH-19280)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-04-02-00-25-19.bpo-37207.ZTPmKJ.rst2
-rw-r--r--Objects/dictobject.c33
2 files changed, 35 insertions, 0 deletions
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 0000000..cb5e9ff
--- /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 2ca32b5..e5f7005 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -3343,6 +3343,38 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
}
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)
{
return dictiter_new(dict, &PyDictIterKey_Type);
@@ -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 *