Skip to content

Commit f649b5a

Browse files
committed
clone only when other is just size.
1 parent 09bdf83 commit f649b5a

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Objects/dictobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,14 +2520,13 @@ dict_merge(PyObject *a, PyObject *b, int override)
25202520
* skips the unnecessary test.
25212521
*/
25222522
override = 1;
2523+
PyDictKeysObject *okeys = other->ma_keys;
25232524

2524-
// If other is clean combined dict, just clone it.
2525+
// If other is clean, combined, and just allocated, just clone it.
25252526
if (other->ma_values == NULL &&
2526-
other->ma_used == other->ma_keys->dk_nentries &&
2527-
other->ma_keys->dk_size <= other->ma_used * 3 + PyDict_MINSIZE) {
2528-
/* Note: ma_used * 3 must not overflow, because sizeof(dict) is
2529-
* much larger than ma_used * 3.
2530-
*/
2527+
other->ma_used == okeys->dk_nentries &&
2528+
(okeys->dk_size == PyDict_MINSIZE ||
2529+
USABLE_FRACTION(okeys->dk_size/2) < other->ma_used)) {
25312530
PyDictKeysObject *keys = clone_combined_dict_keys(other);
25322531
if (keys == NULL) {
25332532
return -1;

0 commit comments

Comments
 (0)