Title: PyType_GetModuleByDef family for binary functions performance · Issue #117578 · python/cpython · GitHub
Open Graph Title: PyType_GetModuleByDef family for binary functions performance · Issue #117578 · python/cpython
X Title: PyType_GetModuleByDef family for binary functions performance · Issue #117578 · python/cpython
Description: Feature or enhancement Proposal: When implementing an extension module with a module state enabled, binary tp slot functions (e.g. nb_add) need two PyType_GetModuleByDef() to be used like the following to compare the given types with a h...
Open Graph Description: Feature or enhancement Proposal: When implementing an extension module with a module state enabled, binary tp slot functions (e.g. nb_add) need two PyType_GetModuleByDef() to be used like the follo...
X Description: Feature or enhancement Proposal: When implementing an extension module with a module state enabled, binary tp slot functions (e.g. nb_add) need two PyType_GetModuleByDef() to be used like the follo...
Opengraph URL: https://github.com/python/cpython/issues/117578
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PyType_GetModuleByDef family for binary functions performance","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nWhen implementing an extension module with a module state enabled,\r\nbinary tp slot functions (e.g. `nb_add`) need two `PyType_GetModuleByDef()` to be used like the following to compare the given types with a heap-type object in the module state, which can be slow:\r\n\r\n```c\r\nstatic PyObject *\r\nfoo_add(PyObject *left, PyObject *right)\r\n{\r\n ...\r\n PyObject *module = PyType_GetModuleByDef(Py_TYPE(left), \u0026module_def);\r\n if (module == NULL) {\r\n PyErr_Clear();\r\n module = PyType_GetModuleByDef(Py_TYPE(right), \u0026module_def);\r\n }\r\n ...\r\n}\r\n```\r\n\r\n* 3.13.0 alpha5 `_decimal` (module state ver.)\r\n\r\n```py\r\nfrom timeit import timeit\r\nf = lambda s: timeit(s, 'import _decimal; d = _decimal.Decimal(1)')\r\na = f('d + 1')\r\nb = f('1 + d')\r\nprint('d + 1:', a)\r\nprint('1 + d:', b, b / a)\r\n```\r\n```\r\nd + 1: 0.11202071857405826\r\n1 + d: 0.49533294743326095 4.421797625818569\r\n```\r\nThe difference mainly comes from a `TypeError` emission from `PyType_GetModuleByDef()`, so it would be nice if we could have a new function which receives two types and emits an error after checking them.\r\n\r\ncc @encukou\r\n\r\n### Related issue:\r\n* #103092\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-117661\n* gh-123100\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/neonene","@type":"Person","name":"neonene"},"datePublished":"2024-04-06T09:30:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/117578/cpython/issues/117578"}
| 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:9ffd3ee4-c296-28c9-93f8-fdd93ce59f75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8FC:CD10D:1732812:1E072DA:696B2774 |
| html-safe-nonce | d16a311057de77a6781208f30a93d7c47d89ac2b0de8d97da47d8cb76cc738dd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEZDOkNEMTBEOjE3MzI4MTI6MUUwNzJEQTo2OTZCMjc3NCIsInZpc2l0b3JfaWQiOiI4MTA1MjMyNjc4MTIzMTUzMjY4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c9bde5c38b6cea353a83c9174a77cab6ae4ae6227a1f5ba89645a1e9409879a1 |
| hovercard-subject-tag | issue:2229162006 |
| 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/117578/issue_layout |
| twitter:image | https://opengraph.githubassets.com/790f6471b8c1856d315349719600b792d49077a5ac0943730ce534b360be2a4d/python/cpython/issues/117578 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/790f6471b8c1856d315349719600b792d49077a5ac0943730ce534b360be2a4d/python/cpython/issues/117578 |
| og:image:alt | Feature or enhancement Proposal: When implementing an extension module with a module state enabled, binary tp slot functions (e.g. nb_add) need two PyType_GetModuleByDef() to be used like the follo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | neonene |
| 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