Title: [C API] Replace PyTuple_Pack(1,2) with PyTuple_Make[Single,Pair] to optimize creation of tuples · Issue #140052 · python/cpython · GitHub
Open Graph Title: [C API] Replace PyTuple_Pack(1,2) with PyTuple_Make[Single,Pair] to optimize creation of tuples · Issue #140052 · python/cpython
X Title: [C API] Replace PyTuple_Pack(1,2) with PyTuple_Make[Single,Pair] to optimize creation of tuples · Issue #140052 · python/cpython
Description: Feature or enhancement Proposal: I ran benchmarks on pyperformance and found that tuples with one or two elements account for about 80% of the total. I checked the code and filled the following table with the number of occurrences of one...
Open Graph Description: Feature or enhancement Proposal: I ran benchmarks on pyperformance and found that tuples with one or two elements account for about 80% of the total. I checked the code and filled the following tab...
X Description: Feature or enhancement Proposal: I ran benchmarks on pyperformance and found that tuples with one or two elements account for about 80% of the total. I checked the code and filled the following tab...
Opengraph URL: https://github.com/python/cpython/issues/140052
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] Replace PyTuple_Pack(1,2) with PyTuple_Make[Single,Pair] to optimize creation of tuples","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nI ran benchmarks on `pyperformance` and found that tuples with one or two elements account for about 80% of the total.\n\n\u003cimg width=\"841\" height=\"525\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/996243de-ad0b-4843-9879-97e594070e6e\" /\u003e\n\nI checked the code and filled the following table with the number of occurrences of one- and two-element tuples:\n\u003cdetails\u003e\u003csummary\u003eTable with number of occurrences\u003c/summary\u003e\n\n| file | function | count |\n| ----------------------- | ----------------- | ----- |\n| `_asyncmodule.c` | `PyTuple_New(2)` | 2 |\n| `_collectionsmodule.c` | `PyTuple_Pack(1)` | 2 |\n| `_csv.c` | `PyTuple_Pack(1)` | 1 |\n| `_datetimemodule.c` | `PyTuple_Pack(2)` | 4 |\n| | `PyTuple_Pack(1)` | 3 |\n| `_elementtree.c` | `PyTuple_Pack(2)` | 4 |\n| `_functoolsmodule.c` | `PyTuple_New(2)` | 2 |\n| `_interpretersmodule.c` | `PyTuple_Pack(2)` | 1 |\n| `_json.c` | `PyTuple_New(2)` | 1 |\n| | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_Pack(1)` | 1 |\n| `_operator.c` | `PyTuple_Pack(2)` | 1 |\n| `_pickle.c` | `PyTuple_Pack(2)` | 3 |\n| | `PyTuple_New(2)` | 2 |\n| | `PyTuple_New(1)` | 2 |\n| `_ssl.c` | `PyTuple_New(2)` | 6 |\n| | `PyTuple_Pack(2)` | 1 |\n| `_threadmodule.c` | `PyTuple_New(2)` | 1 |\n| `_tkinter.c` | `PyTuple_Pack(1)` | |\n| `arraymodule.c` | `PyTuple_New(2)` | 2 |\n| `itertoolsmodule.c` | `PyTuple_Pack(2)` | 2 |\n| | `PyTuple_New(2)` | 1 |\n| `main.c` | `PyTuple_Pack(2)` | 1 |\n| `overlapped.c` | `PyTuple_New(2)` | 2 |\n| `posixmodule.c` | `PyTuple_Pack(2)` | 1 |\n| `pyexpat.c` | `PyTuple_New(1)` | 1 |\n| `selectmodule.c` | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_New(2)` | 1 |\n| `signal_module.c` | `PyTuple_New(2)` | 1 |\n| `socket_module.c` | `PyTuple_Pack(2)` | 3 |\n| `termios.c` | `PyTuple_New(2)` | 2 |\n| `_ctypes.c` | `PyTuple_Pack(2)` | 2 |\n| `stgdict.c` | `PyTuple_Pack(2)` | 1 |\n| `decimal.c` | `PyTuple_Pack(2)` | 7 |\n| | `PyTuple_Pack(1)` | 2 |\n| `microprotocol.c` | `PyTuple_Pack(2)` | 2 |\n| `_sre.c` | `PyTuple_New(2)` | 1 |\n| `datetime.c` | `PyTuple_Pack(1)` | 2 |\n| | `PyTuple_Pack(2)` | 1 |\n| `getargs.c` | `PyTuple_Pack(1)` | 1 |\n| `heaptype.c` | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_Pack(1)` | 2 |\n| | `PyTuple_New(2)` | 1 |\n| `vectorcall_limited.c` | `PyTuple_New(1)` | 2 |\n| `multibytecodec.c` | `PyTuple_New(2)` | 1 |\n| `codeobject.c` | `PyTuple_Pack(2)` | 7 |\n| `dictobject.c` | `PyTuple_Pack(2)` | 2 |\n| | `PyTuple_New(2)` | 4 |\n| `enumobject.c` | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_New(2)` | 2 |\n| `exceptions.c` | `PyTuple_Pack(2)` | 7 |\n| `floatobject.c` | `PyTuple_Pack(2)` | 1 |\n| `frameobject.c` | `PyTuple_Pack(2)` | 2 |\n| | `PyTuple_Pack(1)` | 1 |\n| `genericaliasobject.c` | `PyTuple_Pack(1)` | 2 |\n| `listobject.c` | `PyTuple_Pack(2)` | 1 |\n| `longobject.c` | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_New(2)` | 2 |\n| `odictobject.c` | `PyTuple_Pack(2)` | 2 |\n| | `PyTuple_New(2)` | 1 |\n| `setobject.c` | `PyTuple_Pack(1)` | 1 |\n| `typeobject.c` | `PyTuple_Pack(2)` | 2 |\n| | `PyTuple_Pack(1)` | 5 |\n| `typevarobject.c` | `PyTuple_Pack(2)` | 1 |\n| | `PyTuple_Pack(1)` | 2 |\n| `unicode_format.h` | `PyTuple_Pack(2)` | 2 |\n| `pegen_errors.c` | `PyTuple_Pack(2)` | 2 |\n| `_warnings.c` | `PyTuple_Pack(2)` | 1 |\n| `bltnmodule.c` | `PyTuple_Pack(2)` | 1 |\n| `ceval.c` | `PyTuple_Pack(1)` | 1 |\n| `_codegen.c` | `PyTuple_Pack(1)` | 2 |\n| `compile.c` | `PyTuple_Pack(2)` | 1 |\n| `crossinterp.c` | `PyTuple_Pack(1)` | 1 |\n| `errors.c` | `PyTuple_Pack(1)` | 1 |\n| `hamt.c` | `PyTuple_Pack(2)` | 1 |\n| `marshal.c` | `PyTuple_Pack(2)` | 1 |\n| `pylifecycle.c` | `PyTuple_Pack(2)` | 1 |\n| `Python-tokenize.c` | `PyTuple_Pack(2)` | 1 |\n| `sysmodule.c` | `PyTuple_Pack(1)` | 1 |\n| `tracemalloc.c` | `PyTuple_New(2)` | 1 |\n\u003c/details\u003e \n\nI came up with the idea of adding `PyTuple_MakeSingle` and `PyTuple_MakePair` for such cases to improve performance.\n\nAfterwards, @eendebakpt sent me a link with a previous attempt at this (many thanks!) - https://github.com/python/cpython/issues/118222.\n\nAnyway, I implemented these changes and ran benchmarks.\n\nIf we replace `PyTuple_Pack(1,...)` with `PyTuple_MakeSingle` and `PyTuple_Pack(2,...)` with `PyTuple_MakePair` then we get following results (ran on ubuntu 24.04 x64, compiled with lto):\n\u003cdetails\u003e\u003csummary\u003eGeometric mean - 1.00x faster\u003c/summary\u003e\n\n```\n+--------------------------+----------+------------------------+\n| Benchmark | main | opt |\n+==========================+==========+========================+\n| async_generators | 277 ms | 279 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| asyncio_websockets | 242 ms | 241 ms: 1.00x faster |\n+--------------------------+----------+------------------------+\n| chaos | 36.0 ms | 36.6 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| comprehensions | 10.3 us | 10.1 us: 1.02x faster |\n+--------------------------+----------+------------------------+\n| bench_mp_pool | 66.1 ms | 43.0 ms: 1.54x faster |\n+--------------------------+----------+------------------------+\n| coroutines | 15.0 ms | 14.5 ms: 1.03x faster |\n+--------------------------+----------+------------------------+\n| coverage | 54.9 ms | 56.1 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| crypto_pyaes | 45.3 ms | 46.1 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| deepcopy | 171 us | 168 us: 1.02x faster |\n+--------------------------+----------+------------------------+\n| deepcopy_reduce | 1.87 us | 1.84 us: 1.02x faster |\n+--------------------------+----------+------------------------+\n| deepcopy_memo | 16.5 us | 17.5 us: 1.06x slower |\n+--------------------------+----------+------------------------+\n| deltablue | 1.97 ms | 1.99 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| django_template | 23.1 ms | 23.4 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| docutils | 1.70 sec | 1.68 sec: 1.01x faster |\n+--------------------------+----------+------------------------+\n| fannkuch | 245 ms | 243 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| float | 42.4 ms | 43.4 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| gc_traversal | 2.93 ms | 2.82 ms: 1.04x faster |\n+--------------------------+----------+------------------------+\n| generators | 19.1 ms | 19.5 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| genshi_text | 14.2 ms | 14.4 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| genshi_xml | 32.8 ms | 33.1 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| go | 68.7 ms | 70.2 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| hexiom | 3.64 ms | 3.61 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| json_dumps | 6.34 ms | 6.26 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| json_loads | 15.7 us | 16.1 us: 1.02x slower |\n+--------------------------+----------+------------------------+\n| logging_silent | 63.6 ns | 59.4 ns: 1.07x faster |\n+--------------------------+----------+------------------------+\n| logging_simple | 3.49 us | 3.52 us: 1.01x slower |\n+--------------------------+----------+------------------------+\n| mako | 7.00 ms | 6.94 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| mdp | 788 ms | 780 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| meteor_contest | 68.0 ms | 68.2 ms: 1.00x slower |\n+--------------------------+----------+------------------------+\n| nbody | 55.4 ms | 55.0 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| pickle_dict | 18.5 us | 18.8 us: 1.02x slower |\n+--------------------------+----------+------------------------+\n| pickle_list | 2.93 us | 2.98 us: 1.02x slower |\n+--------------------------+----------+------------------------+\n| pickle_pure_python | 208 us | 205 us: 1.01x faster |\n+--------------------------+----------+------------------------+\n| pidigits | 143 ms | 143 ms: 1.00x slower |\n+--------------------------+----------+------------------------+\n| pprint_safe_repr | 489 ms | 496 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| pprint_pformat | 997 ms | 1.01 sec: 1.01x slower |\n+--------------------------+----------+------------------------+\n| pyflate | 259 ms | 260 ms: 1.00x slower |\n+--------------------------+----------+------------------------+\n| regex_compile | 82.7 ms | 83.5 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| regex_dna | 115 ms | 113 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| regex_v8 | 14.7 ms | 14.3 ms: 1.03x faster |\n+--------------------------+----------+------------------------+\n| richards | 27.3 ms | 27.1 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| richards_super | 31.2 ms | 31.1 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| scimark_fft | 174 ms | 178 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| scimark_lu | 71.5 ms | 69.3 ms: 1.03x faster |\n+--------------------------+----------+------------------------+\n| scimark_monte_carlo | 41.9 ms | 41.2 ms: 1.02x faster |\n+--------------------------+----------+------------------------+\n| scimark_sor | 70.4 ms | 72.0 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| scimark_sparse_mat_mult | 2.67 ms | 2.71 ms: 1.02x slower |\n+--------------------------+----------+------------------------+\n| spectral_norm | 59.2 ms | 58.8 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| sqlglot_normalize | 176 ms | 179 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| sqlglot_optimize | 34.0 ms | 34.1 ms: 1.00x slower |\n+--------------------------+----------+------------------------+\n| sqlglot_parse | 801 us | 792 us: 1.01x faster |\n+--------------------------+----------+------------------------+\n| sqlglot_transpile | 1.01 ms | 994 us: 1.01x faster |\n+--------------------------+----------+------------------------+\n| sympy_expand | 311 ms | 306 ms: 1.02x faster |\n+--------------------------+----------+------------------------+\n| sympy_sum | 92.8 ms | 92.4 ms: 1.00x faster |\n+--------------------------+----------+------------------------+\n| sympy_str | 177 ms | 176 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| telco | 112 ms | 111 ms: 1.00x faster |\n+--------------------------+----------+------------------------+\n| tomli_loads | 1.21 sec | 1.23 sec: 1.02x slower |\n+--------------------------+----------+------------------------+\n| typing_runtime_protocols | 107 us | 109 us: 1.01x slower |\n+--------------------------+----------+------------------------+\n| unpack_sequence | 25.2 ns | 26.3 ns: 1.04x slower |\n+--------------------------+----------+------------------------+\n| unpickle_list | 2.93 us | 3.01 us: 1.03x slower |\n+--------------------------+----------+------------------------+\n| unpickle_pure_python | 137 us | 138 us: 1.01x slower |\n+--------------------------+----------+------------------------+\n| xml_etree_iterparse | 62.9 ms | 62.0 ms: 1.01x faster |\n+--------------------------+----------+------------------------+\n| xml_etree_generate | 54.7 ms | 55.4 ms: 1.01x slower |\n+--------------------------+----------+------------------------+\n| xml_etree_process | 39.0 ms | 40.3 ms: 1.03x slower |\n+--------------------------+----------+------------------------+\n| Geometric mean | (ref) | 1.00x faster |\n+--------------------------+----------+------------------------+\n\nBenchmark hidden because not significant (19): 2to3, asyncio_tcp, asyncio_tcp_ssl, bench_thread_pool, dulwich_log, create_gc_cycles, html5lib, logging_format, nqueens, pathlib, pickle, python_startup, python_startup_no_site, raytrace, regex_effbot, sqlite_synth, sympy_integrate, unpickle, xml_etree_parse\n```\n\n\u003c/details\u003e \n\nI plan to implement `PyTuple_Make[Single,Pair]Steal` and also replace `PyTuple_New(1,2)`.\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_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-140079\n* gh-140132\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/sergey-miryanov","@type":"Person","name":"sergey-miryanov"},"datePublished":"2025-10-13T16:54:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/140052/cpython/issues/140052"}
| 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:f3475c3d-9008-fd18-f118-8aeed3e3874c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E30E:B7E00:3E655F:50035F:696B428A |
| html-safe-nonce | cc17baeda6333c972540fc855dd5722cc61190cf271afe9149118707a512fd82 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzBFOkI3RTAwOjNFNjU1Rjo1MDAzNUY6Njk2QjQyOEEiLCJ2aXNpdG9yX2lkIjoiMjUzNjI3MjU2MzM3MzAzMjA3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c1e4dd75154632cf6297869e04e19e9b08d9afa9a3cf390714eddbaf12399d61 |
| hovercard-subject-tag | issue:3510761200 |
| 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/140052/issue_layout |
| twitter:image | https://opengraph.githubassets.com/96b00bd4990377854bdbd0345658bd5db5e3e89baaf0a9997cd7045f58c1ac20/python/cpython/issues/140052 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/96b00bd4990377854bdbd0345658bd5db5e3e89baaf0a9997cd7045f58c1ac20/python/cpython/issues/140052 |
| og:image:alt | Feature or enhancement Proposal: I ran benchmarks on pyperformance and found that tuples with one or two elements account for about 80% of the total. I checked the code and filled the following tab... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sergey-miryanov |
| 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