Title: `PyList_SetItem` is ~2.7x slower per item on the free-threaded build · Issue #158660 · python/cpython · GitHub
Open Graph Title: `PyList_SetItem` is ~2.7x slower per item on the free-threaded build · Issue #158660 · python/cpython
X Title: `PyList_SetItem` is ~2.7x slower per item on the free-threaded build · Issue #158660 · python/cpython
Description: Bug report Extensions built for the stable ABI can't use PyList_SET_ITEM, so they fill new lists with PyList_SetItem. On the free-threaded build each call costs about 2.7x what it does on the GIL-enabled build, while PyTuple_SetItem cost...
Open Graph Description: Bug report Extensions built for the stable ABI can't use PyList_SET_ITEM, so they fill new lists with PyList_SetItem. On the free-threaded build each call costs about 2.7x what it does on the GIL-e...
X Description: Bug report Extensions built for the stable ABI can't use PyList_SET_ITEM, so they fill new lists with PyList_SetItem. On the free-threaded build each call costs about 2.7x what it does on the G...
Opengraph URL: https://github.com/python/cpython/issues/158660
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`PyList_SetItem` is ~2.7x slower per item on the free-threaded build","articleBody":"### Bug report\n\nExtensions built for the stable ABI can't use `PyList_SET_ITEM`, so they fill new lists with `PyList_SetItem`. On the free-threaded build each call costs about 2.7x what it does on the GIL-enabled build, while `PyTuple_SetItem` costs about the same on both builds. Converting native sequences (pointer arrays, vectors) to lists is a hot path for bindings, so this becomes a visible slowdown when a project switches from abi3 to abi3t wheels. For example, in tokenizers a getter that returns three lists runs about 2x slower (see https://github.com/huggingface/tokenizers/issues/2488#issuecomment-5970712841).\n\n### Reproducer\n\n`fill.c`:\n\n```c\n#include \u003cPython.h>\n\nstatic PyObject *fill_list(PyObject *m, PyObject *arg) {\n Py_ssize_t n = PyLong_AsSsize_t(arg);\n PyObject *list = PyList_New(n);\n for (Py_ssize_t i = 0; i \u003c n; i++)\n PyList_SetItem(list, i, Py_NewRef(Py_None));\n return list;\n}\n\nstatic PyObject *fill_tuple(PyObject *m, PyObject *arg) {\n Py_ssize_t n = PyLong_AsSsize_t(arg);\n PyObject *tuple = PyTuple_New(n);\n for (Py_ssize_t i = 0; i \u003c n; i++)\n PyTuple_SetItem(tuple, i, Py_NewRef(Py_None));\n return tuple;\n}\n\nstatic PyMethodDef methods[] = {\n {\"fill_list\", fill_list, METH_O, NULL},\n {\"fill_tuple\", fill_tuple, METH_O, NULL},\n {NULL},\n};\nPyABIInfo_VAR(abi_info);\nstatic PySlot slots[] = {\n PySlot_DATA(Py_mod_abi, &abi_info),\n PySlot_DATA(Py_mod_name, \"fill\"),\n PySlot_STATIC_DATA(Py_mod_methods, methods),\n PySlot_DATA(Py_mod_gil, Py_MOD_GIL_NOT_USED),\n PySlot_END,\n};\nPyMODEXPORT_FUNC PyModExport_fill(void) { return slots; }\n```\n\n`bench.py`:\n\n```python\nimport sys, timeit, fill\n\nn = 1000\nprint(sys.version.split()[0], \"free-threaded\" if not sys._is_gil_enabled() else \"GIL\", fill.__file__.rsplit(\"/\", 1)[-1])\nfor name in (\"fill_list\", \"fill_tuple\"):\n f = getattr(fill, name)\n t = min(timeit.repeat(lambda: f(n), number=2000, repeat=9)) / 2000\n print(f\" {name}: {t / n * 1e9:.1f} ns/item\")\n```\n\nBuild one copy for abi3 and one for abi3t, then run them:\n\n```sh\nmkdir abi3 abi3t\ngcc -O2 -shared -fPIC -DPy_LIMITED_API=0x030f0000 -I\"$(python3.15 -c 'import sysconfig; print(sysconfig.get_path(\"include\"))')\" fill.c -o abi3/fill.abi3.so\ngcc -O2 -shared -fPIC -DPy_TARGET_ABI3T=0x030f0000 -I\"$(python3.15t -c 'import sysconfig; print(sysconfig.get_path(\"include\"))')\" fill.c -o abi3t/fill.abi3t.so\nPYTHONPATH=abi3 python3.15 bench.py\nPYTHONPATH=abi3t python3.15 bench.py\nPYTHONPATH=abi3t python3.15t bench.py\n```\n\n### Results\n\n3.15.0rc2, x86-64 Linux, gcc -O2:\n\n```\n3.15.0rc2 GIL fill.abi3.so\n fill_list: 7.2 ns/item\n fill_tuple: 7.6 ns/item\n3.15.0rc2 GIL fill.abi3t.so\n fill_list: 6.6 ns/item\n fill_tuple: 7.5 ns/item\n3.15.0rc2 free-threaded fill.abi3t.so\n fill_list: 19.8 ns/item\n fill_tuple: 9.6 ns/item\n```\n\n\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160127\n* gh-160156\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ngoldbaum","@type":"Person","name":"ngoldbaum"},"datePublished":"2026-10-03T15:59:25Z","url":"https://github.com/python/cpython/issues/158660"}
| 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:c5af9981-495a-85b5-2523-5e06cefce69a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A89E:17FB7:3C34DFB:4C38460:6B023AA1 |
| html-safe-nonce | 9b3a42b3d21bb1f5fdeca010daf9a9033fbef5367d5aa5eab3e2ff8d88c5df79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODlFOjE3RkI3OjNDMzRERkI6NEMzODQ2MDo2QjAyM0FBMSIsInZpc2l0b3JfaWQiOiI0MzgzMjIxMjQ3ODY2MTkwNDk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6409af08f2486321d23a3de6b424341fa085a01dc86506028ae958c019822b98 |
| hovercard-subject-tag | issue:5691213452 |
| 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/158660/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e69c4ba02502fa1676190f6047a9b7ec073ac2fc51e9d77ada61e1bf54113faa/python/cpython/issues/158660 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e69c4ba02502fa1676190f6047a9b7ec073ac2fc51e9d77ada61e1bf54113faa/python/cpython/issues/158660 |
| og:image:alt | Bug report Extensions built for the stable ABI can't use PyList_SET_ITEM, so they fill new lists with PyList_SetItem. On the free-threaded build each call costs about 2.7x what it does on the GIL-e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ngoldbaum |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width