bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)

This commit is contained in:
Serhiy Storchaka 2019-09-25 09:47:00 +03:00 committed by GitHub
parent ad7736faf5
commit f163aeaa8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View File

@ -0,0 +1,2 @@
Optimized the :class:`dict` constructor and the :meth:`~dict.update` method
for the case when the argument is a dict.

View File

@ -2317,17 +2317,22 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
result = -1; result = -1;
} }
else if (arg != NULL) { else if (arg != NULL) {
_Py_IDENTIFIER(keys); if (PyDict_CheckExact(arg)) {
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); result = PyDict_Merge(self, arg, 1);
} }
else { 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);
}
} }
} }