Skip to content

Commit 82c62ab

Browse files
eendebakptclaude
andauthored
gh-158863: Drop redundant incref/decref pair in dict_dict_merge() (#158864)
Drop redundant incref/decref pair in dict_dict_merge() On the override path insertdict() receives its own references to key and value via Py_NewRef(), and nothing in the loop body uses them afterwards, so the outer Py_INCREF/Py_DECREF pair was pure overhead. Keep it only on the non-override path, where _PyDict_Contains_KnownHash() can run arbitrary __eq__ code and *dupkey needs a live reference. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 9028df3 commit 82c62ab

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎Objects/dictobject.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,12 +4269,12 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **
42694269

42704270
while (_PyDict_Next((PyObject*)other, &pos, &key, &value, &hash)) {
42714271
int err = 0;
4272-
Py_INCREF(key);
4273-
Py_INCREF(value);
42744272
if (override == 1) {
42754273
err = insertdict(mp, Py_NewRef(key), hash, Py_NewRef(value));
42764274
}
42774275
else {
4276+
Py_INCREF(key);
4277+
Py_INCREF(value);
42784278
err = _PyDict_Contains_KnownHash((PyObject *)mp, key, hash);
42794279
if (err == 0) {
42804280
err = insertdict(mp, Py_NewRef(key), hash, Py_NewRef(value));
@@ -4287,9 +4287,9 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **
42874287
}
42884288
err = 0;
42894289
}
4290+
Py_DECREF(value);
4291+
Py_DECREF(key);
42904292
}
4291-
Py_DECREF(value);
4292-
Py_DECREF(key);
42934293
if (err != 0)
42944294
return -1;
42954295

0 commit comments

Comments
 (0)