Skip to content

Commit cf5863f

Browse files
rhettingermiss-islington
authored andcommitted
Optimize set.pop() to advance a pointer instead of indexing. (pythonGH-10429)
Gives approx 20% speed-up using clang depending on the number of elements in the set (the less dense the set, the more the speed-up). Uses the same entry++ logic used elsewhere in the setobject.c code.
1 parent b83942c commit cf5863f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Objects/setobject.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,7 @@ static PyObject *
701701
set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored))
702702
{
703703
/* Make sure the search finger is in bounds */
704-
Py_ssize_t i = so->finger & so->mask;
705-
setentry *entry;
704+
setentry *entry, *limit;
706705
PyObject *key;
707706

708707
assert (PyAnySet_Check(so));
@@ -711,16 +710,18 @@ set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored))
711710
return NULL;
712711
}
713712

714-
while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
715-
i++;
716-
if (i > so->mask)
717-
i = 0;
713+
entry = so->table + (so->finger & so->mask);
714+
limit = so->table + so->mask;
715+
while (entry->key == NULL || entry->key==dummy) {
716+
entry++;
717+
if (entry > limit)
718+
entry = so->table;
718719
}
719720
key = entry->key;
720721
entry->key = dummy;
721722
entry->hash = -1;
722723
so->used--;
723-
so->finger = i + 1; /* next place to start */
724+
so->finger = entry - so->table + 1; /* next place to start */
724725
return key;
725726
}
726727

0 commit comments

Comments
 (0)