Title: Use tagged pointers on the stack in the default build. · Issue #127705 · python/cpython · GitHub
Open Graph Title: Use tagged pointers on the stack in the default build. · Issue #127705 · python/cpython
X Title: Use tagged pointers on the stack in the default build. · Issue #127705 · python/cpython
Description: Currently all references to objects in frameobjects use _PyStackRef instead of PyObject *. This is necessary for the free-threaded build to support deferred references. For the default build _PyStackRef is just an alias for PyObject *. W...
Open Graph Description: Currently all references to objects in frameobjects use _PyStackRef instead of PyObject *. This is necessary for the free-threaded build to support deferred references. For the default build _PySta...
X Description: Currently all references to objects in frameobjects use _PyStackRef instead of PyObject *. This is necessary for the free-threaded build to support deferred references. For the default build _PySta...
Opengraph URL: https://github.com/python/cpython/issues/127705
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use tagged pointers on the stack in the default build.","articleBody":"Currently all references to objects in frameobjects use `_PyStackRef` instead of `PyObject *`.\r\nThis is necessary for the free-threaded build to support deferred references.\r\n\r\nFor the default build `_PyStackRef` is just an alias for `PyObject *`.\r\nWe should change `_PyStackRef` to use proper tagged pointers in the default build for two important reasons:\r\n\r\n* It will reduce the maintenance burden of using tagged pointers if they were the same in both builds\r\n* It offers a lot of optimization potential. The overhead of reference counting operations is large, and tagged pointers will allow us to reduce that overhead considerably.\r\n\r\nMy initial implementation is [0.8% slower](https://github.com/faster-cpython/benchmarking-public/tree/main/results/bm-20241206-3.14.0a2+-0c20416), although I'd like to get that closer to 0 before merging anything. There is some speedup in the GC due to streamlined immortality checks, and some slowdown due to increased overhead of turning new `PyObject *` references into `_PyStackRef`s.\r\n\r\nThis small slowdown will allow us a large speedup (maybe more than 5%) as we can do the following:\r\n* Reduce the overhead of refcount operations by using tagged references for the majority of `LOAD_` instructions in the interpreter.\r\n* Completely eliminate many decref operations by tracking which references are tagged in the JIT.\r\n\r\nThe tagging scheme:\r\n Tag | Meaning\r\n--- | --- \r\n00 | Normal pointers\r\n01 | Pointers with embedded reference count\r\n10 | Unused\r\n11 | Pointer to immortal object\u003csup\u003e1\u003c/sup\u003e (including NULL)\r\n\r\nThis tagging scheme is chosen as it provides the best performance for the most common operations:\r\n* PyStackRef_DUP: Can check to see if the object's reference count needs updating with a single check and no memory read: `ptr \u0026 1`\r\n* PyStackRef_CLOSE: As for PyStackRef_DUP, only a single bit check is needed\r\n* PyStackRef_XCLOSE: Since `NULL` is treated as immortal and tagged, this is the same as PyStackRef_CLOSE.\r\n\r\nMaintaining the invariant that tag `11` is used for all immortal objects is a bit expensive, but can be mitigated by pushing the conversion from `PyObject *` to `_PyStackRef` down to a point where it is known whether an object is newly created or not.\r\nFor newly created objects `PyStackRef_FromPyObjectStealMortal` can be used which performs no immortality check.\r\n\r\n --------------\r\n\r\n1. Actually, any object that was immortal when the reference was created. References to objects that are made immortal after the reference is created would have the low bits set to `00`, or `01`. This is OK as immortal refcounts have a huge margin of error and the number of possible references to one of these immortal objects is very small.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-127875\n* gh-128121\n* gh-130785\n* gh-131072\n* gh-131140\n* gh-131198\n* gh-131365\n* gh-131500\n* gh-131508\n* gh-136178\n* gh-136206\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2024-12-06T18:31:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/127705/cpython/issues/127705"}
| 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:34d8a1d9-57ae-2e66-38c6-805a7ab8b5c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 80A4:1D81B0:13AC001:1B07350:696AA00E |
| html-safe-nonce | a2ff0608f2d9ea1fa7f46f5176eda3cc38376e85f09208b72288b8df268d0b07 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MEE0OjFEODFCMDoxM0FDMDAxOjFCMDczNTA6Njk2QUEwMEUiLCJ2aXNpdG9yX2lkIjoiNzU0Mjg5NTI5NDQxMjM5ODYwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1125411e1c470f4c86c25f99879e2a2ed5167f44aefa13ba6f6632c0cee81224 |
| hovercard-subject-tag | issue:2723682975 |
| 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/127705/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7bc7eb720a9228ef3c61745e9dbd9bcf0eda1c3b9862951d250fc2436befec15/python/cpython/issues/127705 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7bc7eb720a9228ef3c61745e9dbd9bcf0eda1c3b9862951d250fc2436befec15/python/cpython/issues/127705 |
| og:image:alt | Currently all references to objects in frameobjects use _PyStackRef instead of PyObject *. This is necessary for the free-threaded build to support deferred references. For the default build _PySta... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markshannon |
| hostname | github.com |
| expected-hostname | github.com |
| None | a51f97dbb9326f71c08ecb61577457d543c602124d1a2672871258ef37ac5261 |
| 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 | 4bd0eac606c70914085176ef312ebdcd97a8cdf1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width