You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Classic time vs. space trade-off. How common is hashing Fractions? (The example seems a bit special.) If we decide to do this, I'd prefer the version using an LRU cache, so that someone creating a large number of Fractions they aren't hashing doesn't have to pay the price. Did you think about the cache size much or is 65k just your go-to cache size value these days?
FWIW the Decimal patch for Python was rejected. The C version seems all right, so the problem would only affect the tiny minority of people who can't use the C Decimal version. So that's not comparable here.
Activity
serhiy-storchaka commentedon Aug 31, 2022
See also #58683. The similar patch for Decimal was rejected.
gvanrossum commentedon Sep 1, 2022
Classic time vs. space trade-off. How common is hashing Fractions? (The example seems a bit special.) If we decide to do this, I'd prefer the version using an LRU cache, so that someone creating a large number of Fractions they aren't hashing doesn't have to pay the price. Did you think about the cache size much or is 65k just your go-to cache size value these days?
FWIW the Decimal patch for Python was rejected. The C version seems all right, so the problem would only affect the tiny minority of people who can't use the C Decimal version. So that's not comparable here.
pythonGH-96465: Cache hashes for Fraction instances
matthiasgoergens commentedon Sep 6, 2022
@rhettinger Do you have have benchmark data?
rhettinger commentedon Sep 7, 2022
Baseline
% ~/cpython/python.exe -m timeit -s 'from fractions import Fraction as F' -s 'f = F(375, 34)' 'hash(f)'
500000 loops, best of 5: 479 nsec per loop
With caching
% git checkout fraction_hash_cache
Switched to branch 'fraction_hash_cache'
% ~/cpython/python.exe -m timeit -s 'from fractions import Fraction as F' -s 'f = F(375, 34)' 'hash(f)'
2000000 loops, best of 5: 119 nsec per loop
GH-96465: Cache hashes for Fraction instances (GH-96483)
pythongh-96465: Clear fractions hash lru_cache under refleak testing
gh-96465: Clear fractions hash lru_cache under refleak testing (GH-96689
pythongh-96465: Clear fractions hash lru_cache under refleak testing (p…
[3.11] gh-108822: Backport libregrtest changes from the main branch (#…