Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c629d4c

Browse files
committed
Replace outdated optimization with clearer code that compiles better.
Letting the compiler decide how to optimize the multiply by five gives it the freedom to make better choices for the best technique for a given target machine. For example, GCC on x86_64 produces a little bit better code: Old-way (3 steps with a data dependency between each step): shrq $5, %r13 leaq 1(%rbx,%r13), %rax leaq (%rax,%rbx,4), %rbx New-way (3 steps with no dependency between the first two steps which can be run in parallel): leaq (%rbx,%rbx,4), %rax # i*5 shrq $5, %r13 # perturb >>= PERTURB_SHIFT leaq 1(%r13,%rax), %rbx # 1 + perturb + i*5
1 parent 9e3d27b commit c629d4c

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

Objects/setobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash)
118118
/* In the loop, key == dummy is by far (factor of 100s) the
119119
least likely outcome, so test for that last. */
120120
for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121-
i = (i << 2) + i + perturb + 1;
121+
i = i * 5 + perturb + 1;
122122
entry = &table[i & mask];
123123
if (entry->key == NULL) {
124124
if (freeslot != NULL)
@@ -189,7 +189,7 @@ set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash)
189189
/* In the loop, key == dummy is by far (factor of 100s) the
190190
least likely outcome, so test for that last. */
191191
for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192-
i = (i << 2) + i + perturb + 1;
192+
i = i * 5 + perturb + 1;
193193
entry = &table[i & mask];
194194
if (entry->key == NULL)
195195
return freeslot == NULL ? entry : freeslot;
@@ -258,7 +258,7 @@ set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash)
258258
i = (size_t)hash & mask;
259259
entry = &table[i];
260260
for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
261-
i = (i << 2) + i + perturb + 1;
261+
i = i * 5 + perturb + 1;
262262
entry = &table[i & mask];
263263
}
264264
so->fill++;

0 commit comments

Comments
 (0)