summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-09-25 09:47:00 +0300
committerGitHub <noreply@github.com>2019-09-25 09:47:00 +0300
commitf163aeaa8c15b806b622c9cb10bc1d2a6e034e24 (patch)
tree064ac67fe9e980a8062bde811e7918ad6095f701
parentad7736faf5b82a24374c601a72599adf29951080 (diff)
downloadcpython-git-f163aeaa8c15b806b622c9cb10bc1d2a6e034e24.tar.gz
bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2019-09-22-13-56-18.bpo-38219.rFl7JD.rst2
-rw-r--r--Objects/dictobject.c21
2 files changed, 15 insertions, 8 deletions
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
index 0000000000..8d621cfb82
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-09-22-13-56-18.bpo-38219.rFl7JD.rst
@@ -0,0 +1,2 @@
+Optimized the :class:`dict` constructor and the :meth:`~dict.update` method
+for the case when the argument is a dict.
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 2f24105835..2f86946b98 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -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);
+ }
}
}