René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:80c1781b-25ea-a3ca-ce1b-76806bcdbac8
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idEA4C:247F51:965280:CA392C:69697DE8
html-safe-nonced260711f07863c0066edeff29009a9934d693554e849e0932527680f64fd1d7c
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTRDOjI0N0Y1MTo5NjUyODA6Q0EzOTJDOjY5Njk3REU4IiwidmlzaXRvcl9pZCI6IjEzMDQ1NzUwMjc3NzU2MzQ5MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac46e3e85c9325bd187b67601fc0d68d36bf450c2bc5c8981d07f114c17760300d
hovercard-subject-tagissue:1357679641
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/96465/issue_layout
twitter:imagehttps://opengraph.githubassets.com/893efe463f3c1c78fc13b649ecb6463c89e728549b9f482edc94a2cac073cd03/python/cpython/issues/96465
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/893efe463f3c1c78fc13b649ecb6463c89e728549b9f482edc94a2cac073cd03/python/cpython/issues/96465
og:image:altIn 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerhettinger
hostnamegithub.com
expected-hostnamegithub.com
Nonef33e4b94c8824ab2b434d82a94139432fb5ebee9df4b75304140ad22508c4a77
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release48f380098b30acbb700b04f1724481ca10d574fc
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/96465#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F96465
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F96465
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/96465
Reloadhttps://github.com/python/cpython/issues/96465
Reloadhttps://github.com/python/cpython/issues/96465
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/96465
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/96465
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/96465
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/96465
Cache the hash value of fractionshttps://github.com/python/cpython/issues/96465#top
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/rhettinger
https://github.com/rhettinger
rhettingerhttps://github.com/rhettinger
on Aug 31, 2022https://github.com/python/cpython/issues/96465#issue-1357679641
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.