Skip to content

Commit

Permalink
optimize set(dict(...)) using set_insert_clean()
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryLHW committed Jul 14, 2024
1 parent 04130b2 commit 4fad4fd
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@ set_update_dict_lock_held(PySetObject *so, PyObject *other)
* that there will be no (or few) overlapping keys.
*/
Py_ssize_t dictsize = PyDict_GET_SIZE(other);
if (dictsize == 0)
return 0;
if ((so->fill + dictsize)*5 >= so->mask*3) {
if (set_table_resize(so, (so->used + dictsize)*2) != 0) {
return -1;
Expand All @@ -933,6 +935,19 @@ set_update_dict_lock_held(PySetObject *so, PyObject *other)
PyObject *key;
PyObject *value;
Py_hash_t hash;

/* If our table is empty, we can use set_insert_clean() */
if (so->fill == 0) {
setentry *newtable = so->table;
size_t newmask = (size_t)so->mask;
so->fill = dictsize;
FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, dictsize);
while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
set_insert_clean(newtable, newmask, Py_NewRef(key), hash);
}
return 0;
}

while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
if (set_add_entry(so, key, hash)) {
return -1;
Expand Down

0 comments on commit 4fad4fd

Please sign in to comment.