mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-136599: Improve long_hash() (#136600)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
e46d403d59
commit
c9cf019cf5
2 changed files with 18 additions and 1 deletions
|
|
@ -0,0 +1 @@
|
|||
Improve performance of :class:`int` hash calculations.
|
||||
|
|
@ -3676,7 +3676,23 @@ 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);
|
||||
|
||||
#if PyHASH_BITS >= 2 * PyLong_SHIFT
|
||||
// unroll second digit
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue