Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f163aea

Browse files
bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)
1 parent ad7736f commit f163aea

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Optimized the :class:`dict` constructor and the :meth:`~dict.update` method
2+
for the case when the argument is a dict.

Objects/dictobject.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,17 +2317,22 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
23172317
result = -1;
23182318
}
23192319
else if (arg != NULL) {
2320-
_Py_IDENTIFIER(keys);
2321-
PyObject *func;
2322-
if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2323-
result = -1;
2324-
}
2325-
else if (func != NULL) {
2326-
Py_DECREF(func);
2320+
if (PyDict_CheckExact(arg)) {
23272321
result = PyDict_Merge(self, arg, 1);
23282322
}
23292323
else {
2330-
result = PyDict_MergeFromSeq2(self, arg, 1);
2324+
_Py_IDENTIFIER(keys);
2325+
PyObject *func;
2326+
if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2327+
result = -1;
2328+
}
2329+
else if (func != NULL) {
2330+
Py_DECREF(func);
2331+
result = PyDict_Merge(self, arg, 1);
2332+
}
2333+
else {
2334+
result = PyDict_MergeFromSeq2(self, arg, 1);
2335+
}
23312336
}
23322337
}
23332338

0 commit comments

Comments
 (0)