Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of :class:`int` hash calculations.
17 changes: 16 additions & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3676,7 +3676,22 @@ long_hash(PyObject *obj)
}
i = _PyLong_DigitCount(v);
sign = _PyLong_NonCompactSign(v);
x = 0;

// unroll first digit
Py_BUILD_ASSERT(PyHASH_BITS > PyLong_SHIFT);
assert(i>=1);
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
--i;
x = v->long_value.ob_digit[i];
assert(x < PyHASH_MODULUS);

#if ( PyHASH_BITS > (2 * PyLong_SHIFT) )
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
// unroll second digit
assert(i>=1);
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
--i;
x <<= PyLong_SHIFT;
x += v->long_value.ob_digit[i];
assert(x < PyHASH_MODULUS);
#endif
Comment thread
vstinner marked this conversation as resolved.
while (--i >= 0) {
/* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
want to compute x * 2**PyLong_SHIFT + v->long_value.ob_digit[i] modulo
Expand Down
Loading