Title: Cache the hash value of fractions · Issue #96465 · python/cpython · GitHub
Open Graph Title: Cache the hash value of fractions · Issue #96465 · python/cpython
X Title: Cache the hash value of fractions · Issue #96465 · python/cpython
Description: In both 3.10 and 3.11, hashing a small fraction is 20x slower than for a decimal. For fractions with large denominators, it is slower still. We could sacrifice the space for one slot to cache the hash value: __slots__ = ('_numerator', '_...
Open Graph Description: In both 3.10 and 3.11, hashing a small fraction is 20x slower than for a decimal. For fractions with large denominators, it is slower still. We could sacrifice the space for one slot to cache the h...
X Description: In both 3.10 and 3.11, hashing a small fraction is 20x slower than for a decimal. For fractions with large denominators, it is slower still. We could sacrifice the space for one slot to cache the h...
Opengraph URL: https://github.com/python/cpython/issues/96465
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cache the hash value of fractions","articleBody":"In both 3.10 and 3.11, hashing a small fraction is 20x slower than for a decimal. For fractions with large denominators, it is slower still.\r\n\r\nWe could sacrifice the space for one slot to cache the hash value:\r\n\r\n __slots__ = ('_numerator', '_denominator', '_hash')\r\n\r\n def __hash__(self):\r\n try:\r\n return self._hash\r\n except AttributeError:\r\n self.hash = ... # current code goes here\r\n return self._hash \r\n\r\nAnother approach would be to have a single, size limited cache for the computation:\r\n\r\n```\r\ndef __hash__(self):\r\n\r\n @lru_cache(maxsize=1 \u003c\u003c 16)\r\n def current_code(numerator, denominator):\r\n try:\r\n dinv = pow(denominator, -1, _PyHASH_MODULUS)\r\n except ValueError:\r\n # ValueError means there is no modular inverse.\r\n hash_ = _PyHASH_INF\r\n else:\r\n hash_ = hash(hash(abs(numerator)) * dinv)\r\n result = hash_ if numerator \u003e= 0 else -hash_\r\n return -2 if result == -1 else result\r\n\r\n return current_code(self._numerator, self._denominator)\r\n\r\n```\r\n\r\nExample of code that would benefit:\r\n\r\n```\r\n@lru_cache(maxsize=100_000)\r\ndef pabs(n: Fraction | int) -\u003e Fraction:\r\n \"P-adic absolute value\"\r\n ...\r\n\r\nfor x, y in product(pool, repeat=2): # pool is a set of fractions used as test values\r\n assert (pabs(x)==0) == (x==0) # |x|=0 if and only if x=0\r\n assert pabs(x * y) == pabs(x) * pabs(y) # |xy| = |x||y|\r\n assert pabs(x + y) \u003c= pabs(x) + pabs(y) # |x + y| ≤ |x| + |y| (Triangle Inequality)\r\n assert pabs(x + y) \u003c= max(pabs(x), pabs(y)) # Strong triangle inequality\r\n assert pabs(x) == pabs(y) or pabs(x + y) == max(pabs(x), pabs(y)) # Lemma 2.1: If |x| ≠ |y|, |x+y| = max{|x|, |y|}\r\n```\r\n\r\n","author":{"url":"https://github.com/rhettinger","@type":"Person","name":"rhettinger"},"datePublished":"2022-08-31T18:04:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/96465/cpython/issues/96465"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:80c1781b-25ea-a3ca-ce1b-76806bcdbac8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA4C:247F51:965280:CA392C:69697DE8 |
| html-safe-nonce | d260711f07863c0066edeff29009a9934d693554e849e0932527680f64fd1d7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTRDOjI0N0Y1MTo5NjUyODA6Q0EzOTJDOjY5Njk3REU4IiwidmlzaXRvcl9pZCI6IjEzMDQ1NzUwMjc3NzU2MzQ5MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 46e3e85c9325bd187b67601fc0d68d36bf450c2bc5c8981d07f114c17760300d |
| hovercard-subject-tag | issue:1357679641 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/96465/issue_layout |
| twitter:image | https://opengraph.githubassets.com/893efe463f3c1c78fc13b649ecb6463c89e728549b9f482edc94a2cac073cd03/python/cpython/issues/96465 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/893efe463f3c1c78fc13b649ecb6463c89e728549b9f482edc94a2cac073cd03/python/cpython/issues/96465 |
| og:image:alt | In both 3.10 and 3.11, hashing a small fraction is 20x slower than for a decimal. For fractions with large denominators, it is slower still. We could sacrifice the space for one slot to cache the h... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhettinger |
| hostname | github.com |
| expected-hostname | github.com |
| None | f33e4b94c8824ab2b434d82a94139432fb5ebee9df4b75304140ad22508c4a77 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 48f380098b30acbb700b04f1724481ca10d574fc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width