Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
3eaf70d8
Unverified
Commit
3eaf70d8
authored
Sep 7, 2022
by
Raymond Hettinger
Committed by
GitHub
Sep 7, 2022
Browse files
Options
Downloads
Patches
Plain Diff
GH-96465: Cache hashes for Fraction instances (GH-96483)
parent
0cd992c0
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Lib/fractions.py
+35
-30
35 additions, 30 deletions
Lib/fractions.py
Misc/NEWS.d/next/Library/2022-09-01-13-54-38.gh-issue-96465.0IJmrH.rst
+1
-0
1 addition, 0 deletions
...ext/Library/2022-09-01-13-54-38.gh-issue-96465.0IJmrH.rst
with
36 additions
and
30 deletions
Lib/fractions.py
+
35
−
30
View file @
3eaf70d8
...
...
@@ -4,6 +4,7 @@
"""
Fraction, infinite-precision, rational numbers.
"""
from
decimal
import
Decimal
import
functools
import
math
import
numbers
import
operator
...
...
@@ -20,6 +21,39 @@
# _PyHASH_MODULUS.
_PyHASH_INF
=
sys
.
hash_info
.
inf
@functools.lru_cache
(
maxsize
=
1
<<
14
)
def
_hash_algorithm
(
numerator
,
denominator
):
# To make sure that the hash of a Fraction agrees with the hash
# of a numerically equal integer, float or Decimal instance, we
# follow the rules for numeric hashes outlined in the
# documentation. (See library docs, 'Built-in Types').
try
:
dinv
=
pow
(
denominator
,
-
1
,
_PyHASH_MODULUS
)
except
ValueError
:
# ValueError means there is no modular inverse.
hash_
=
_PyHASH_INF
else
:
# The general algorithm now specifies that the absolute value of
# the hash is
# (|N| * dinv) % P
# where N is self._numerator and P is _PyHASH_MODULUS. That's
# optimized here in two ways: first, for a non-negative int i,
# hash(i) == i % P, but the int hash implementation doesn't need
# to divide, and is faster than doing % P explicitly. So we do
# hash(|N| * dinv)
# instead. Second, N is unbounded, so its product with dinv may
# be arbitrarily expensive to compute. The final answer is the
# same if we use the bounded |N| % P instead, which can again
# be done with an int hash() call. If 0 <= i < P, hash(i) == i,
# so this nested hash() call wastes a bit of time making a
# redundant copy when |N| < P, but can save an arbitrarily large
# amount of computation for large |N|.
hash_
=
hash
(
hash
(
abs
(
numerator
))
*
dinv
)
result
=
hash_
if
numerator
>=
0
else
-
hash_
return
-
2
if
result
==
-
1
else
result
_RATIONAL_FORMAT
=
re
.
compile
(
r
"""
\A\s* # optional whitespace at the start,
(?P<sign>[-+]?) # an optional sign, then
...
...
@@ -646,36 +680,7 @@ def __round__(self, ndigits=None):
def
__hash__
(
self
):
"""
hash(self)
"""
# To make sure that the hash of a Fraction agrees with the hash
# of a numerically equal integer, float or Decimal instance, we
# follow the rules for numeric hashes outlined in the
# documentation. (See library docs, 'Built-in Types').
try
:
dinv
=
pow
(
self
.
_denominator
,
-
1
,
_PyHASH_MODULUS
)
except
ValueError
:
# ValueError means there is no modular inverse.
hash_
=
_PyHASH_INF
else
:
# The general algorithm now specifies that the absolute value of
# the hash is
# (|N| * dinv) % P
# where N is self._numerator and P is _PyHASH_MODULUS. That's
# optimized here in two ways: first, for a non-negative int i,
# hash(i) == i % P, but the int hash implementation doesn't need
# to divide, and is faster than doing % P explicitly. So we do
# hash(|N| * dinv)
# instead. Second, N is unbounded, so its product with dinv may
# be arbitrarily expensive to compute. The final answer is the
# same if we use the bounded |N| % P instead, which can again
# be done with an int hash() call. If 0 <= i < P, hash(i) == i,
# so this nested hash() call wastes a bit of time making a
# redundant copy when |N| < P, but can save an arbitrarily large
# amount of computation for large |N|.
hash_
=
hash
(
hash
(
abs
(
self
.
_numerator
))
*
dinv
)
result
=
hash_
if
self
.
_numerator
>=
0
else
-
hash_
return
-
2
if
result
==
-
1
else
result
return
_hash_algorithm
(
self
.
_numerator
,
self
.
_denominator
)
def
__eq__
(
a
,
b
):
"""
a == b
"""
...
...
This diff is collapsed.
Click to expand it.
Misc/NEWS.d/next/Library/2022-09-01-13-54-38.gh-issue-96465.0IJmrH.rst
0 → 100644
+
1
−
0
View file @
3eaf70d8
Fraction hashes are now cached.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment