Title: Use per-thread reference count for heap type objects in the free-threaded build · Issue #122417 · python/cpython · GitHub
Open Graph Title: Use per-thread reference count for heap type objects in the free-threaded build · Issue #122417 · python/cpython
X Title: Use per-thread reference count for heap type objects in the free-threaded build · Issue #122417 · python/cpython
Description: Feature or enhancement Overview As described in PEP 703, heap type objects use a mix of reference counting techniques in the free-threaded build. They use deferred reference counting (see #117376) in combination with with per-thread refe...
Open Graph Description: Feature or enhancement Overview As described in PEP 703, heap type objects use a mix of reference counting techniques in the free-threaded build. They use deferred reference counting (see #117376) ...
X Description: Feature or enhancement Overview As described in PEP 703, heap type objects use a mix of reference counting techniques in the free-threaded build. They use deferred reference counting (see #117376) ...
Opengraph URL: https://github.com/python/cpython/issues/122417
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use per-thread reference count for heap type objects in the free-threaded build","articleBody":"# Feature or enhancement\n\n### Overview\n\nAs described in [PEP 703](https://peps.python.org/pep-0703/#reference-counting-type-objects), heap type objects use a mix of reference counting techniques in the free-threaded build. They use deferred reference counting (see #117376) in combination with with per-thread reference counting. Deferred reference counting alone is not sufficient to address the multi-threaded scaling bottlenecks with heap types because most references to heap types are from object instances, not references on the interpreter stack.\n\nTo address this, heap type reference counts are partially stored in a distributed manner in per-thread arrays. Every thread stores an array of local reference counts for each heap type object. A heap type's true reference count is the sum of:\n\n* The PyObject reference count (i.e., `ob_ref_local` and `ob_ref_shared`)\n* The deferred references on the stack (like with other objects that use deferred reference counting)\n* The per-thread reference counts stored in each `_PyThreadStateImpl`\n\n### Mechanics\n\nTwo new internal functions for reference counting types:\n\n* `void _Py_INCREF_TYPE(PyTypeObject *type)`\n* `void _Py_DECREF_TYPE(PyTypeObject *type)`\n\nThese are like `Py_INCREF` and `Py_DECREF`, but modify the per-thread reference counts when possible. If necessary, they fallback to `Py_INCREF/DECREF`. In the default build, these just call `Py_INCREF/DECREF`. Note that it's always *safe* to call `Py_INCREF` instead of `_Py_INCREF_TYPE`; it's just that `_Py_INCREF_TYPE` avoids scalability bottlenecks in some cases.\n\nThe free-threaded GC will scan the per-thread refcount arrays when computing an object's reference count in addition to scanning each thread's stack for deferred reference counting.\n\nWe'll also need some code to manage the lifetime and resizing of the per-thread refcount arrays.\n\n### Internal Usages\n\n[`_PyObject_Init`](https://github.com/python/cpython/blob/490e0ad83ac72c5688dfbbab4eac61ccfd7be5fd/Include/internal/pycore_object.h#L294) should call `_Py_INCREF_TYPE(typeobj)` instead of `Py_INCREF(typeobj)`. (The PEP says `PyType_GenericAlloc`, but `_PyObject_Init` is now the right place to do it.)\n\n`subtype_dealloc` should call `_Py_DECREF_TYPE(type)` instead of `Py_DECREF(type)`. (Same as described in the PEP).\n\nAdditionally, with the conversion of some static types to heap types, we now use the following `tp_dealloc` pattern in more places:\n\n```c\nPyTypeObject *tp = Py_TYPE(self);\ntp->tp_free(self);\nPy_DECREF(tp);\n```\n\nWe will eventually want to change internal usages to the following to avoid reference count contention on `tp`:\n\n```c\nPyTypeObject *tp = Py_TYPE(self);\ntp->tp_free(self);\n_Py_DECREF_TYPE(tp);\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-122418\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2024-07-29T19:00:22Z","url":"https://github.com/python/cpython/issues/122417"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:4528f98a-9a33-385f-4826-0ab596ea015a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9CD6:1F927B:1EF0AA9:28B6A48:6B022F68 |
| html-safe-nonce | c90d48ee67308a1b175c9c3666a75559873b08f5b099ab63801ad4cd6bf74254 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Q0Q2OjFGOTI3QjoxRUYwQUE5OjI4QjZBNDg6NkIwMjJGNjgiLCJ2aXNpdG9yX2lkIjoiMjk3MTQ5NjczMDY3MzE2NDEzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 96052bcafba565dfd75754cadee69621f32d40cad14a733bd4486cce2d6bbfd4 |
| hovercard-subject-tag | issue:2436128687 |
| 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/122417/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e9a3634a425ac00c9e3a0a6a58992e74b534584ecd8c66f0cbad67ab18b1f86d/python/cpython/issues/122417 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e9a3634a425ac00c9e3a0a6a58992e74b534584ecd8c66f0cbad67ab18b1f86d/python/cpython/issues/122417 |
| og:image:alt | Feature or enhancement Overview As described in PEP 703, heap type objects use a mix of reference counting techniques in the free-threaded build. They use deferred reference counting (see #117376) ... |
| 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 | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width