]> granicus.if.org Git - python/commitdiff
bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 25 Sep 2019 06:47:00 +0000 (09:47 +0300)
committerGitHub <noreply@github.com>
Wed, 25 Sep 2019 06:47:00 +0000 (09:47 +0300)
Misc/NEWS.d/next/Core and Builtins/2019-09-22-13-56-18.bpo-38219.rFl7JD.rst [new file with mode: 0644]
Objects/dictobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-09-22-13-56-18.bpo-38219.rFl7JD.rst b/Misc/NEWS.d/next/Core and Builtins/2019-09-22-13-56-18.bpo-38219.rFl7JD.rst
new file mode 100644 (file)
index 0000000..8d621cf
--- /dev/null
@@ -0,0 +1,2 @@
+Optimized the :class:`dict` constructor and the :meth:`~dict.update` method
+for the case when the argument is a dict.
index 2f24105835120c99646be2fd4e81c8637317ba3f..2f86946b985f551b3b512df45eb8acc24ea5fce6 100644 (file)
@@ -2317,17 +2317,22 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
         result = -1;
     }
     else if (arg != NULL) {
-        _Py_IDENTIFIER(keys);
-        PyObject *func;
-        if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
-            result = -1;
-        }
-        else if (func != NULL) {
-            Py_DECREF(func);
+        if (PyDict_CheckExact(arg)) {
             result = PyDict_Merge(self, arg, 1);
         }
         else {
-            result = PyDict_MergeFromSeq2(self, arg, 1);
+            _Py_IDENTIFIER(keys);
+            PyObject *func;
+            if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
+                result = -1;
+            }
+            else if (func != NULL) {
+                Py_DECREF(func);
+                result = PyDict_Merge(self, arg, 1);
+            }
+            else {
+                result = PyDict_MergeFromSeq2(self, arg, 1);
+            }
         }
     }