Title: Avoid creating argument tuples when calling slot wrappers · Issue #162258 · python/cpython · GitHub
Open Graph Title: Avoid creating argument tuples when calling slot wrappers · Issue #162258 · python/cpython
X Title: Avoid creating argument tuples when calling slot wrappers · Issue #162258 · python/cpython
Description: Feature or enhancement Python wrapper_descriptor objects often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples which often takes longer than the opera...
Open Graph Description: Feature or enhancement Python wrapper_descriptor objects often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples...
X Description: Feature or enhancement Python wrapper_descriptor objects often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples...
Opengraph URL: https://github.com/python/cpython/issues/162258
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Avoid creating argument tuples when calling slot wrappers","articleBody":"### Feature or enhancement\n\n\nPython [`wrapper_descriptor` objects](https://github.com/python/cpython/blob/441c010fef90ab2860ae8142cbfd1fd52f53ba75/Include/cpython/descrobject.h#L54-L58) often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples which often takes longer than the operation.\n\nThis is particularly visible with frozen dataclasses. The decorator generates an `__init__` that calls `object.__setattr__(self, name, value)` once per field.\n\n```python3\nfrom dataclasses import dataclass\n\n@dataclass(frozen=True)\nclass frozen_triple:\n a: int\n b: int\n c: int\n\nfrozen_triple(1, 2, 3)\n```\nOn `main` on my machine the `frozen_triple` instance takes ~156ns to construct. With my prototype which avoids the tuples it takes ~110ns (1.42x faster). This work generalizes gh-156250 which focused on a single slot.\n\nI propose removing the unnecessary tuples across all `wrapper_descriptor` objects by using what the interpreter already knows about exact ones with no new public API. Wrappers that take keywords, `__init__`, and `__call__`, keep one tuple.\n\n1. In `_PyObject_MakeTpCall` special case `PyWrapperDescr_Type` and call its slot wrapper directly. Change the slot wrapper core implementation to be vectorcall shaped and add a small `tp_call` wrapper around it for compatibility.\n\n This avoids building an args tuple containing `self` that is then sliced.\n\n prototype: https://github.com/python/cpython/commit/ba2f6455b258cb91a19fba4c732c20d89bac9488\n2. Expand the `slotdefs` array private `pytype_slotdef` entries to contain two parallel members. The existing `wrapperfunc`, which takes a tuple, and a new variant which takes vectorcall args. If the vectorcall version is non-NULL, call it. This allows for gradual migration of slots. Custom slot wrapper descriptors, such as Sage and Cython use, keep working as before.\n\n This makes the second tuple construction optional. Until descriptor wrappers move it is still constructed when necessary.\n3. Migrate slot definitions to provide both members. The argument tuple used to keep `self` and the arguments alive during the call. In the prototype I move that responsibility to the shared call as it is relatively low overhead. Having each wrapper hold only what it needs is a possible refinement.\n\nStep 2 & 3 prototype: https://github.com/cmaloney/cpython/commit/193f25c328a\n\n### Performance\n\n| Statement | main | step 1 only | all three steps |\n|---|:---:|:---:|:---:|\n| `frozen_triple(1, 2, 3)` | 156 ns | 132 ns (1.19x faster) | 110 ns (1.42x faster) |\n| frozen dataclass, 8 fields | 356 ns | 285 ns (1.25x faster) | 222 ns (1.60x faster) |\n| `object.__setattr__(o, 'x', 1)` | 39.6 ns | 30.4 ns (1.30x faster) | 25.0 ns (1.59x faster) |\n| `int.__add__(1, 2)` | 27.1 ns | 21.5 ns (1.26x faster) | 17.6 ns (1.55x faster) |\n| `c['x'] = 1` (`collections.Counter`) | 36.4 ns | 29.4 ns (1.24x faster) | 23.2 ns (1.57x faster) |\n\nLinux x86-64, clang PGO+LTO build, pyperf.\n\nPrototype: https://github.com/python/cpython/compare/main...cmaloney:cpython:shared/exp-descrwrappper-tuple?diff=split&w\n\n### Alternatives Considered\n\n - Add a member to `PyWrapperDescrObject` which implements `vectorcall`. Adds a pointer to every `PyWrapperDescrObject` object. This works out fairly similarly performance wise but with more tradeoffs. It can be added later if needed for external project integration.\n\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n* [PEP 590](https://peps.python.org/pep-0590/) lists `wrapper_descriptor` and `method-wrapper` among the classes that \"will use the vectorcall protocol (not all of these will be changed in the initial implementation)\".\n* gh-75724 and GH-3685 (2017): \"Optimize wrapper descriptors using FASTCALL\". Closed by its author: \"these specific descriptors are rare, so the added complexity is not worth it\".\n* gh-73445 (2017): `tp_fastcall`, which included a fast wrapper in `struct wrapperbase`. Closed.\n* gh-78577 and GH-14863 (2019): inherited methods are slow in heap subclasses; the draft PR \"vectorcall for wrapper_descriptor and method-wrapper\" changed the `wrapperfunc` signature and was closed unreviewed for lack of time. The issue is still open.\n* gh-81155 (GH-13185 and GH-13781): vectorcall for `method_descriptor`, the closest precedent.\n* gh-81088: a crash from borrowed references during argument handling, the bug class the reference holding guards against.\n* gh-156250 and GH-156258: the same cost for a single slot `Counter.__setitem__`.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162369\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/cmaloney","@type":"Person","name":"cmaloney"},"datePublished":"2026-11-19T08:31:01Z","url":"https://github.com/python/cpython/issues/162258"}
| 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:6ab2f38b-592e-48c1-7bf5-c51a2ead2969 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A206:2E124E:34A6F3C:434C387:6B01EC38 |
| html-safe-nonce | 029eb0484c373370dadbee5308e9aab3b070a173d65cbd3a9dfc19b86f0be5a7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjA2OjJFMTI0RTozNEE2RjNDOjQzNEMzODc6NkIwMUVDMzgiLCJ2aXNpdG9yX2lkIjoiNTMyNzUwODE2MDkyNzQ0NzYxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f22f109293104445ae35b98124a6fffe2be1e723a1e577098b8ef87af90ed672 |
| hovercard-subject-tag | issue:6485578427 |
| 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/162258/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2b99a8b905efbf8fdc5c5a333c5734b883908a58bf618d06327c0939b19106f/python/cpython/issues/162258 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2b99a8b905efbf8fdc5c5a333c5734b883908a58bf618d06327c0939b19106f/python/cpython/issues/162258 |
| og:image:alt | Feature or enhancement Python wrapper_descriptor objects often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cmaloney |
| 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