Skip to content

Commit e82cf86

Browse files
authored
bpo-29949: Fix set memory usage regression (GH-943)
Revert "Minor factoring: move redundant resize scaling logic into the resize function." This reverts commit 4897300.
1 parent cd815ed commit e82cf86

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29949: Fix memory usage regression of set and frozenset object.
14+
1315
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque
1416
when pass indices of wrong type.
1517

Objects/setobject.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
236236
entry->hash = hash;
237237
if ((size_t)so->fill*5 < mask*3)
238238
return 0;
239-
return set_table_resize(so, so->used);
239+
return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
240240

241241
found_active:
242242
Py_DECREF(key);
@@ -302,7 +302,6 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
302302
setentry small_copy[PySet_MINSIZE];
303303

304304
assert(minused >= 0);
305-
minused = (minused > 50000) ? minused * 2 : minused * 4;
306305

307306
/* Find the smallest table size > minused. */
308307
/* XXX speed-up with intrinsics */
@@ -643,8 +642,8 @@ set_merge(PySetObject *so, PyObject *otherset)
643642
* that there will be no (or few) overlapping keys.
644643
*/
645644
if ((so->fill + other->used)*5 >= so->mask*3) {
646-
if (set_table_resize(so, so->used + other->used) != 0)
647-
return -1;
645+
if (set_table_resize(so, (so->used + other->used)*2) != 0)
646+
return -1;
648647
}
649648
so_entry = so->table;
650649
other_entry = other->table;
@@ -987,7 +986,7 @@ set_update_internal(PySetObject *so, PyObject *other)
987986
if (dictsize < 0)
988987
return -1;
989988
if ((so->fill + dictsize)*5 >= so->mask*3) {
990-
if (set_table_resize(so, so->used + dictsize) != 0)
989+
if (set_table_resize(so, (so->used + dictsize)*2) != 0)
991990
return -1;
992991
}
993992
while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
@@ -1507,7 +1506,7 @@ set_difference_update_internal(PySetObject *so, PyObject *other)
15071506
/* If more than 1/4th are dummies, then resize them away. */
15081507
if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
15091508
return 0;
1510-
return set_table_resize(so, so->used);
1509+
return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
15111510
}
15121511

15131512
static PyObject *

0 commit comments

Comments
 (0)