-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-136599: Improve long_hash #136600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-136599: Improve long_hash #136600
Changes from 12 commits
146f5aa
a162da2
4f9fc76
07bce4b
32341de
194fb7a
a48860f
6d3754b
fec9fbe
08d7ba9
14a90f1
76c4f6a
f720557
55e5bd9
0aa56f0
c1a3184
b9a487d
b0fd0d8
c6e060d
9b6e628
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1693,5 +1693,22 @@ class MyInt(int): | |||||
# GH-117195 -- This shouldn't crash | ||||||
object.__sizeof__(1) | ||||||
|
||||||
def test_hash(self): | ||||||
# gh-136599 | ||||||
self.assertEqual(hash(-1), -2) | ||||||
self.assertEqual(hash(0), 0) | ||||||
self.assertEqual(hash(10), 10) | ||||||
|
||||||
self.assertEqual(hash(sys.hash_info.modulus - 2), sys.hash_info.modulus - 2) | ||||||
self.assertEqual(hash(sys.hash_info.modulus - 1), sys.hash_info.modulus - 1) | ||||||
self.assertEqual(hash(sys.hash_info.modulus), 0) | ||||||
self.assertEqual(hash(sys.hash_info.modulus + 1), 1) | ||||||
|
||||||
self.assertEqual(hash(-sys.hash_info.modulus - 2), -2) | ||||||
self.assertEqual(hash(-sys.hash_info.modulus - 1), -2) | ||||||
self.assertEqual(hash(-sys.hash_info.modulus), 0) | ||||||
self.assertEqual(hash(-sys.hash_info.modulus + 1), - (sys.hash_info.modulus - 1)) | ||||||
|
self.assertEqual(hash(-sys.hash_info.modulus + 1), - (sys.hash_info.modulus - 1)) | |
self.assertEqual(hash(-sys.hash_info.modulus + 1), -sys.hash_info.modulus + 1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve performance of :class:`int` hash calculations. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3676,7 +3676,20 @@ long_hash(PyObject *obj) | |||
} | ||||
i = _PyLong_DigitCount(v); | ||||
sign = _PyLong_NonCompactSign(v); | ||||
x = 0; | ||||
|
||||
// unroll first two digits | ||||
#if ( PyHASH_BITS > PyLong_SHIFT ) | ||||
|
#if ( PyHASH_BITS > PyLong_SHIFT ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case is indeed untested, but I added it because of a comment by Serhiy #136600 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case is indeed untested
FYI: #138336
I added it because of a comment by Serhiy
Then fine. Though, an assert might be option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace the test with a build assertion: Py_BUILD_ASSERT(PyHASH_BITS > PyLong_SHIFT);
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just use assert(i>=1)
here and then again in the second #if
below?
Otherwise LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that part could be in a separate pr, merged first.
The docs describe algorithm in details:
https://docs.python.org/3/library/stdtypes.html#hashing-of-numeric-types
Maybe we could test it against pure-Python implementation, using also hypothesis?