Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize set(), frozenset(), and set.update().
45 changes: 29 additions & 16 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,18 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
return 0;
}

/*
Resize at the start of an operation where the final maximum set size is known
*/
static int
set_presize(PySetObject *so, Py_ssize_t n)
{
if ((so->fill + n)*5 >= so->mask*3) {
return set_table_resize(so, (so->used + n)*2);
}
return 0;
}

static int
set_contains_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
{
Expand Down Expand Up @@ -842,13 +854,8 @@ set_merge_lock_held(PySetObject *so, PyObject *otherset)
if (other == so || other->used == 0)
/* a.update(a) or a.update(set()); nothing to do */
return 0;
/* Do one big resize at the start, rather than
* incrementally resizing as we insert new keys. Expect
* that there will be no (or few) overlapping keys.
*/
if ((so->fill + other->used)*5 >= so->mask*3) {
if (set_table_resize(so, (so->used + other->used)*2) != 0)
return -1;
if (set_presize(so, other->used) < 0) {
return -1;
}
so_entry = so->table;
other_entry = other->table;
Expand Down Expand Up @@ -1195,15 +1202,8 @@ set_update_dict_lock_held(PySetObject *so, PyObject *other)
}
#endif

/* Do one big resize at the start, rather than
* incrementally resizing as we insert new keys. Expect
* that there will be no (or few) overlapping keys.
*/
Py_ssize_t dictsize = PyDict_GET_SIZE(other);
if ((so->fill + dictsize)*5 >= so->mask*3) {
if (set_table_resize(so, (so->used + dictsize)*2) != 0) {
return -1;
}
if (set_presize(so, PyDict_GET_SIZE(other)) < 0) {
return -1;
}

Py_ssize_t pos = 0;
Expand All @@ -1228,6 +1228,19 @@ set_update_iterable_lock_held(PySetObject *so, PyObject *other)
return -1;
}

Py_ssize_t n = PyObject_LengthHint(other, 0);
if (n < 0) {
PyErr_Clear(); /* grow on demand instead */
}
else if (n == 0 || n >= PY_SSIZE_T_MAX/8 - so->fill) {
/* Either a length hint was not found or the returned value for `n`
could lead to an overflow */
}
else if (set_presize(so, n) < 0) {
Py_DECREF(it);
return -1;
}

PyObject *key;
while ((key = PyIter_Next(it)) != NULL) {
if (set_add_key(so, key)) {
Expand Down
Loading