Hoist constant expressions (so->table and so->mask) out of the inner-loop.

This commit is contained in:
Raymond Hettinger 2015-12-13 18:45:01 -08:00
parent 9fdaff30db
commit 5088f6005f
1 changed files with 12 additions and 12 deletions

View File

@ -260,16 +260,13 @@ The caller is responsible for updating the key's reference count and
the setobject's fill and used fields.
*/
static void
set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
set_insert_clean(setentry *table, size_t mask, PyObject *key, Py_hash_t hash)
{
setentry *table = so->table;
setentry *entry;
size_t perturb = hash;
size_t mask = (size_t)so->mask;
size_t i = (size_t)hash & mask;
size_t j;
assert(so->fill == so->used);
while (1) {
entry = &table[i];
if (entry->key == NULL)
@ -285,8 +282,8 @@ set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
i = (i * 5 + 1 + perturb) & mask;
}
found_null:
entry->key = key;
entry->hash = hash;
entry->key = key;
}
/* ======== End logic for probing the hash table ========================== */
@ -304,6 +301,8 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
setentry *oldtable, *newtable, *entry;
Py_ssize_t oldfill = so->fill;
Py_ssize_t oldused = so->used;
Py_ssize_t oldmask = so->mask;
size_t newmask;
int is_oldtable_malloced;
setentry small_copy[PySet_MINSIZE];
@ -363,18 +362,17 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
/* Copy the data over; this is refcount-neutral for active entries;
dummy entries aren't copied over, of course */
newmask = (size_t)so->mask;
if (oldfill == oldused) {
for (entry = oldtable; oldused > 0; entry++) {
for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
if (entry->key != NULL) {
oldused--;
set_insert_clean(so, entry->key, entry->hash);
set_insert_clean(newtable, newmask, entry->key, entry->hash);
}
}
} else {
for (entry = oldtable; oldused > 0; entry++) {
for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
if (entry->key != NULL && entry->key != dummy) {
oldused--;
set_insert_clean(so, entry->key, entry->hash);
set_insert_clean(newtable, newmask, entry->key, entry->hash);
}
}
}
@ -676,13 +674,15 @@ set_merge(PySetObject *so, PyObject *otherset)
/* 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 = other->used;
so->used = other->used;
for (i = 0; i <= other->mask; i++, other_entry++) {
key = other_entry->key;
if (key != NULL && key != dummy) {
Py_INCREF(key);
set_insert_clean(so, key, other_entry->hash);
set_insert_clean(newtable, newmask, key, other_entry->hash);
}
}
return 0;