Title: Make `dict` objects thread-safe in `--disable-gil` builds · Issue #112075 · python/cpython · GitHub
Open Graph Title: Make `dict` objects thread-safe in `--disable-gil` builds · Issue #112075 · python/cpython
X Title: Make `dict` objects thread-safe in `--disable-gil` builds · Issue #112075 · python/cpython
Description: We are going to need a variety of techniques to make dictionaries thread-safe in --disable-gil builds. I expect this to be implemented across multiple PRs. For context, here is the change from the nogil-3.12 fork, but things might be don...
Open Graph Description: We are going to need a variety of techniques to make dictionaries thread-safe in --disable-gil builds. I expect this to be implemented across multiple PRs. For context, here is the change from the ...
X Description: We are going to need a variety of techniques to make dictionaries thread-safe in --disable-gil builds. I expect this to be implemented across multiple PRs. For context, here is the change from the ...
Opengraph URL: https://github.com/python/cpython/issues/112075
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make `dict` objects thread-safe in `--disable-gil` builds","articleBody":"We are going to need a variety of techniques to make dictionaries thread-safe in `--disable-gil` builds. I expect this to be implemented across multiple PRs.\r\n\r\nFor context, here is the change from the `nogil-3.12` fork, but things might be done a bit differently in CPython 3.13: https://github.com/colesbury/nogil-3.12/commit/d896dfc8db\r\n\r\n1) Most operations should acquire the dictionary object lock using the critical section API.\r\n2) Dictionary global version counter should use atomic increments \r\n3) `PyDictKeysObject` needs special handling (see below)\r\n4) `PyDictOrValues` needs special handling (see below)\r\n5) Accessing a single element should optimistically avoid locking for performance\r\n\r\nIn general, we want to avoid making changes which hurt the performance of the default build. The \"critical sections\" are no-ops in the default build, but we will need to take care with other changes. Some may be behind `#ifdef` guards.\r\n\r\n### `PyDictKeysObject`\r\n\r\nNote that `PyDictKeysObject` is *NOT* a `PyObject` subclass.\r\n\r\nWe need a mutex in `PyDictKeysObject` because multiple threads may concurrently insert keys into shared `PyDictKeysObject`. The mutex is only used for modifications to \"shared\" keys; non-shared keys rely on the locking for the dict object.\r\n\r\nIn `--disable-gil` builds, we want to avoid refcounting shared `PyDictKeysObject` for performance and thread-safety reasons. Instead shared keys objects should be freed during cyclic GC. (Non-shared keys don't need reference counting because they \"owned\" by a single dict object.) We may want to consider making this change for both the default build and `--disable-gil` builds to make maintenance easier if there is not a negative perf impact.\r\n\r\n### `PyDictOrValues`\r\n\r\n`PyDictOrValues` is the \"managed\" dict in some PyObject pre-headers. We need some locking/atomic operations to handle the transition between `PyDictValues*` and storing a `PyDictObject*`\r\n\r\n### Optimistically Avoid Locking\r\n\r\nSee https://peps.python.org/pep-0703/#optimistically-avoiding-locking.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-112247\n* gh-114256\n* gh-114508\n* gh-114512\n* gh-114543\n* gh-114568\n* gh-114629\n* gh-114741\n* gh-114742\n* gh-114768\n* gh-114778\n* gh-114931\n* gh-115108\n* gh-115109\n* gh-115786\n* gh-115855\n* gh-116087\n* gh-116336\n* gh-116344\n* gh-116620\n* gh-117528\n* gh-117529\n* gh-117824\n* gh-118499\n* gh-118676\n* gh-119288\n* gh-121545\n* gh-129336\n* gh-139737\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2023-11-14T19:14:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/112075/cpython/issues/112075"}
| 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:774133a5-3367-080d-9c98-35f96c4cd4e0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9AC:11169F:2A1201F:37115C3:696B2CE6 |
| html-safe-nonce | bb082510e4bee99d51aac734ad74d5964c28a394dd2c6d1977dbd2988fe8e2c0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUFDOjExMTY5RjoyQTEyMDFGOjM3MTE1QzM6Njk2QjJDRTYiLCJ2aXNpdG9yX2lkIjoiODU4MzIzOTQyNjk0ODkzNDg4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e695b94c907d529b99cd420fb6c362343255e3e656b931f30069ec7c436a5d81 |
| hovercard-subject-tag | issue:1993401425 |
| 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/112075/issue_layout |
| twitter:image | https://opengraph.githubassets.com/04d44f684674effc931e688aed788cb5c73dbb60074440fda1adcab25539c232/python/cpython/issues/112075 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/04d44f684674effc931e688aed788cb5c73dbb60074440fda1adcab25539c232/python/cpython/issues/112075 |
| og:image:alt | We are going to need a variety of techniques to make dictionaries thread-safe in --disable-gil builds. I expect this to be implemented across multiple PRs. For context, here is the change from the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colesbury |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width