Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 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.
16 changes: 15 additions & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3676,7 +3676,21 @@ 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);
--i;
x = v->long_value.ob_digit[i];
assert(x < PyHASH_MODULUS);
// unroll second digit
#if ( PyHASH_BITS > (2 * PyLong_SHIFT) )
assert(i>=1);
--i;
x <<= PyLong_SHIFT;
x += v->long_value.ob_digit[i];
assert(x < PyHASH_MODULUS);
#endif
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