René's URL Explorer Experiment


Title: gh-131757: allow lru_cache functions to execute concurrently by tom-pytel · Pull Request #131758 · python/cpython · GitHub

Open Graph Title: gh-131757: allow lru_cache functions to execute concurrently by tom-pytel · Pull Request #131758 · python/cpython

X Title: gh-131757: allow lru_cache functions to execute concurrently by tom-pytel · Pull Request #131758 · python/cpython

Description: This PR changes functools.lru_cache to only hold critical sections when it is performing operations on itself and not when it calls the wrapped function being cached. Example script timing, current code: $ ./python ../bench_lrucache.py Time: 31.0s This PR: $ ./python ../bench_lrucache.py Time: 2.4s Explanation: The script is 16 threads doing a long operation. In the current code they run sequentially because they are serialized by lru_cache. In this PR they are allowed to run concurrently. Script: import threading from functools import lru_cache from time import time @lru_cache def func(v): for i in range(100000000): pass return v threads = [threading.Thread(target=func, args=(i,)) for i in range(16)] t0 = time() for thread in threads: thread.start() for thread in threads: thread.join() print(f'Time: {time() - t0:0.1f}s') More detail: There are three caching functions that can be used, currently they all execute locked (and by extension the function being cached): uncached_lru_cache_wrapper - Used when lru_cache(maxsize=0). Just a passthrough call, this doesn't need a lock at all. infinite_lru_cache_wrapper - Used when lru_cache(maxsize=None). This can just rely on the possible lock when doing dict operations. bounded_lru_cache_wrapper - Used otherwise. This has been split up into a pre-call function and a post-call function which are both locked individually, with the critical section being released during the actual call to the cached function. This can be done because lru_cache is thread-safe and accomodates for the possibility of the cache dictionary changing during execution of the cached function. NOTE: This can be reduced to a single critical section if the locked function is only ever called in its owner thread, but not sure is necessary and wanted to keep things simple for this PR. Issue: gh-131757

Open Graph Description: This PR changes functools.lru_cache to only hold critical sections when it is performing operations on itself and not when it calls the wrapped function being cached. Example script timing, current...

X Description: This PR changes functools.lru_cache to only hold critical sections when it is performing operations on itself and not when it calls the wrapped function being cached. Example script timing, current...

Opengraph URL: https://github.com/python/cpython/pull/131758

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:fad96aa6-6b90-93e3-366c-60ee71d85b8a
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idC080:1F87:F642D5:1330D1A:6B02461E
html-safe-nonce6169d695e39e9dad229ad0d167cb8cabb07522b32fca747a8cf00c8f3cb06cfe
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDgwOjFGODc6RjY0MkQ1OjEzMzBEMUE6NkIwMjQ2MUUiLCJ2aXNpdG9yX2lkIjoiNDUzNDI3OTcxMDEzNzUzMzk4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacf64c3829bac9491acfef5119da72d6e2a9a2c1152a8ba1713011ca6d97a2def9
hovercard-subject-tagpull_request:2419026157
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/131758/files
twitter:imagehttps://avatars.githubusercontent.com/u/51921719?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/51921719?s=400&v=4
og:image:altThis PR changes functools.lru_cache to only hold critical sections when it is performing operations on itself and not when it calls the wrapped function being cached. Example script timing, current...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/131758/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F131758%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F131758%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/131758/files
Reloadhttps://github.com/python/cpython/pull/131758/files
Reloadhttps://github.com/python/cpython/pull/131758/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
vstinnerhttps://github.com/vstinner
python:mainhttps://github.com/python/cpython/tree/main
tom-pytel:fix-issue-131757https://github.com/tom-pytel/cpython/tree/fix-issue-131757
Conversation 63 https://github.com/python/cpython/pull/131758
Commits 19 https://github.com/python/cpython/pull/131758/commits
Checks 0 https://github.com/python/cpython/pull/131758/checks
Files changed https://github.com/python/cpython/pull/131758/files
Please reload this pagehttps://github.com/python/cpython/pull/131758/files
gh-131757: allow lru_cache functions to execute concurrently https://github.com/python/cpython/pull/131758/files#top
Show all changes 19 commits https://github.com/python/cpython/pull/131758/files
d60c12c gh-131757: allow lru_cache functions to execute concurrently tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/d60c12cf18bbebe5e98c938dd1ba0adee2644aa9
2dfcea3 📜🤖 Added by blurb_it. blurb-it[bot] Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/2dfcea3dbe2cc44e862dcf42146218fcdc2391f1
c700d1e fix smelly symbols tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/c700d1ed6a7c28c6c13c10e40b2fb1a70586c84f
24d4f7a fix smelly symbols for real this time tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/24d4f7a8beecf544cdadbbc3ffcc45e377de9b19
7ead4ee requested changes tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/7ead4eec0aff7bc20611a47e8ca541ca1fee8489
68d14ea remove unnecessary atomics tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/68d14eaee615ae8927d4cb82d65ed31535952005
028f18b add FT_ATOMIC_ADD_SSIZE just in this file tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/028f18b69b4cb4bcf989df6cb6fc8ac3520937dc
6c74b26 _PyDict_GetItemRef_KnownHash_LockHeld() tom-pytel Mar 26, 2025 https://github.com/python/cpython/pull/131758/commits/6c74b262cd6484093f4f3e6aa1344cf4a9a60430
2910429 requested changes tom-pytel Mar 27, 2025 https://github.com/python/cpython/pull/131758/commits/2910429b6c3bf64be211b2851be8b386d29c0429
1282325 stupid news tom-pytel Mar 27, 2025 https://github.com/python/cpython/pull/131758/commits/12823257ae1152954f1c76ae0903c336a0764113
34f7fe7 requested changes tom-pytel Mar 27, 2025 https://github.com/python/cpython/pull/131758/commits/34f7fe7d3e46506b4d670b8712573b9c20c3999b
60a475d FT_ATOMIC_ADD_SSIZE into pyatomic_ft_wrappers.h tom-pytel Mar 28, 2025 https://github.com/python/cpython/pull/131758/commits/60a475dbe481b8bc125b0a21714c48beb76cf5df
4200582 stupid warnings tom-pytel Mar 28, 2025 https://github.com/python/cpython/pull/131758/commits/4200582082eee25a442d7ba2ac000d6be0607035
aed9b45 return type tom-pytel Mar 28, 2025 https://github.com/python/cpython/pull/131758/commits/aed9b452fb0383c5bc37673468f0f4d01eff7873
47e47d5 FT_ATOMIC_ADD_SSIZE no return value tom-pytel Mar 28, 2025 https://github.com/python/cpython/pull/131758/commits/47e47d58640b9b53e1ed46db453cd29732180a78
416defe Merge branch 'main' into fix-issue-131757 tom-pytel Apr 4, 2025 https://github.com/python/cpython/pull/131758/commits/416defecb2c37f2f48be085c898abc78301507fb
2e7bcf4 Merge branch 'main' into fix-issue-131757 tom-pytel Apr 14, 2025 https://github.com/python/cpython/pull/131758/commits/2e7bcf4700e90bc20d0b8bb97fc8d8e7db7e51d5
11eab6d bounded_lru_cache_get_lock_held atomic writes so tsan doesn't complain tom-pytel Apr 14, 2025 https://github.com/python/cpython/pull/131758/commits/11eab6d046633128f46e550465e42f301958dd67
5d1bf31 fix indentation tom-pytel Apr 14, 2025 https://github.com/python/cpython/pull/131758/commits/5d1bf312dd62baf1a91ef8eed7b80fc86f50380f
Clear filters https://github.com/python/cpython/pull/131758/files
Please reload this pagehttps://github.com/python/cpython/pull/131758/files
Please reload this pagehttps://github.com/python/cpython/pull/131758/files
pycore_pyatomic_ft_wrappers.h https://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
2025-03-26-10-56-22.gh-issue-131757.pFRdmN.rst https://github.com/python/cpython/pull/131758/files#diff-2f91e11f64d96206e4f5eb95dbe0f09845a7f3915b2ddc0dc0b1010e29ee5d77
_functoolsmodule.c https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
Include/internal/pycore_pyatomic_ft_wrappers.hhttps://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
View file https://github.com/tom-pytel/cpython/blob/5d1bf312dd62baf1a91ef8eed7b80fc86f50380f/Include/internal/pycore_pyatomic_ft_wrappers.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/131758/{{ revealButtonHref }}
https://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
https://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
https://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
https://github.com/python/cpython/pull/131758/files#diff-ce601684210b2d7f8f955bb70eb6836e92f54d7d0ec6cd87e6d9da2b974ab7f7
Misc/NEWS.d/next/Library/2025-03-26-10-56-22.gh-issue-131757.pFRdmN.rsthttps://github.com/python/cpython/pull/131758/files#diff-2f91e11f64d96206e4f5eb95dbe0f09845a7f3915b2ddc0dc0b1010e29ee5d77
View file https://github.com/tom-pytel/cpython/blob/5d1bf312dd62baf1a91ef8eed7b80fc86f50380f/Misc/NEWS.d/next/Library/2025-03-26-10-56-22.gh-issue-131757.pFRdmN.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/131758/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L475
Modules/_functoolsmodule.chttps://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
View file https://github.com/tom-pytel/cpython/blob/5d1bf312dd62baf1a91ef8eed7b80fc86f50380f/Modules/_functoolsmodule.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/131758/{{ revealButtonHref }}
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
Please reload this pagehttps://github.com/python/cpython/pull/131758/files
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
https://github.com/python/cpython/pull/131758/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
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.