mirror of
https://github.com/python/cpython.git
synced 2025-09-01 22:47:59 +00:00
Optimize set.pop() to advance a pointer instead of indexing. (GH-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.
This commit is contained in:
parent
b83942c755
commit
cf5863faab
1 changed files with 8 additions and 7 deletions
|
@ -701,8 +701,7 @@ static PyObject *
|
||||||
set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored))
|
set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored))
|
||||||
{
|
{
|
||||||
/* Make sure the search finger is in bounds */
|
/* Make sure the search finger is in bounds */
|
||||||
Py_ssize_t i = so->finger & so->mask;
|
setentry *entry, *limit;
|
||||||
setentry *entry;
|
|
||||||
PyObject *key;
|
PyObject *key;
|
||||||
|
|
||||||
assert (PyAnySet_Check(so));
|
assert (PyAnySet_Check(so));
|
||||||
|
@ -711,16 +710,18 @@ set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored))
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
|
entry = so->table + (so->finger & so->mask);
|
||||||
i++;
|
limit = so->table + so->mask;
|
||||||
if (i > so->mask)
|
while (entry->key == NULL || entry->key==dummy) {
|
||||||
i = 0;
|
entry++;
|
||||||
|
if (entry > limit)
|
||||||
|
entry = so->table;
|
||||||
}
|
}
|
||||||
key = entry->key;
|
key = entry->key;
|
||||||
entry->key = dummy;
|
entry->key = dummy;
|
||||||
entry->hash = -1;
|
entry->hash = -1;
|
||||||
so->used--;
|
so->used--;
|
||||||
so->finger = i + 1; /* next place to start */
|
so->finger = entry - so->table + 1; /* next place to start */
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue