Skip to content

Commit efde51a

Browse files
authored
bpo-29949: Fix set memory usage regression (GH-945)
Revert "Minor factoring: move redundant resize scaling logic into the resize function." This reverts commit 4897300. (cherry picked from commit e82cf86)
1 parent 7d5d13d commit efde51a

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.6.2 release candidate 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*3 < mask*2)
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);
@@ -304,7 +304,6 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
304304
setentry small_copy[PySet_MINSIZE];
305305

306306
assert(minused >= 0);
307-
minused = (minused > 50000) ? minused * 2 : minused * 4;
308307

309308
/* Find the smallest table size > minused. */
310309
/* XXX speed-up with intrinsics */
@@ -646,8 +645,8 @@ set_merge(PySetObject *so, PyObject *otherset)
646645
* that there will be no (or few) overlapping keys.
647646
*/
648647
if ((so->fill + other->used)*3 >= so->mask*2) {
649-
if (set_table_resize(so, so->used + other->used) != 0)
650-
return -1;
648+
if (set_table_resize(so, (so->used + other->used)*2) != 0)
649+
return -1;
651650
}
652651
so_entry = so->table;
653652
other_entry = other->table;
@@ -990,7 +989,7 @@ set_update_internal(PySetObject *so, PyObject *other)
990989
if (dictsize < 0)
991990
return -1;
992991
if ((so->fill + dictsize)*3 >= so->mask*2) {
993-
if (set_table_resize(so, so->used + dictsize) != 0)
992+
if (set_table_resize(so, (so->used + dictsize)*2) != 0)
994993
return -1;
995994
}
996995
while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
@@ -1511,7 +1510,7 @@ set_difference_update_internal(PySetObject *so, PyObject *other)
15111510
/* If more than 1/4th are dummies, then resize them away. */
15121511
if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
15131512
return 0;
1514-
return set_table_resize(so, so->used);
1513+
return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
15151514
}
15161515

15171516
static PyObject *

0 commit comments

Comments
 (0)