Title: build `_asyncio` module as static module · Issue #136669 · python/cpython · GitHub
Open Graph Title: build `_asyncio` module as static module · Issue #136669 · python/cpython
X Title: build `_asyncio` module as static module · Issue #136669 · python/cpython
Description: Currently _asyncio is built as a shared library module which causes it to use slower functions calls for getting the thread state whereas if it is built as static module then it can read the current thread state by using faster segment r...
Open Graph Description: Currently _asyncio is built as a shared library module which causes it to use slower functions calls for getting the thread state whereas if it is built as static module then it can read the curren...
X Description: Currently _asyncio is built as a shared library module which causes it to use slower functions calls for getting the thread state whereas if it is built as static module then it can read the curren...
Opengraph URL: https://github.com/python/cpython/issues/136669
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"build `_asyncio` module as static module","articleBody":"Currently `_asyncio` is built as a shared library module which causes it to use slower functions calls for getting the thread state whereas if it is built as static module then it can read the current thread state by using faster segment registers directly. This affects both free-threading and normal builds however on free-threading, critical sections heavily use thread state so it has a larger impact.\n\nThe function calls to `_PyThreadState_GetCurrent` are completely eliminated after the change and it reads thread state directly via `fs` register. \n\nNormal build:\n\nBefore:\n\n```\n(gdb) disassemble _asyncio__get_running_loop\nDump of assembler code for function _asyncio__get_running_loop:\n 0x00007ffff73e1d80 \u003c+0>: push %rax\n 0x00007ffff73e1d81 \u003c+1>: call 0x7ffff73e1500 \u003c_PyThreadState_GetCurrent@plt>\n 0x00007ffff73e1d86 \u003c+6>: mov 0x358(%rax),%rax\n 0x00007ffff73e1d8d \u003c+13>: test %rax,%rax\n 0x00007ffff73e1d90 \u003c+16>: je 0x7ffff73e1da2 \u003c_asyncio__get_running_loop+34>\n 0x00007ffff73e1d92 \u003c+18>: mov (%rax),%ecx\n 0x00007ffff73e1d94 \u003c+20>: cmp $0xbfffffff,%ecx\n 0x00007ffff73e1d9a \u003c+26>: ja 0x7ffff73e1da0 \u003c_asyncio__get_running_loop+32>\n 0x00007ffff73e1d9c \u003c+28>: inc %ecx\n 0x00007ffff73e1d9e \u003c+30>: mov %ecx,(%rax)\n 0x00007ffff73e1da0 \u003c+32>: pop %rcx\n 0x00007ffff73e1da1 \u003c+33>: ret\n 0x00007ffff73e1da2 \u003c+34>: mov 0xb1ef(%rip),%rax # 0x7ffff73ecf98\n 0x00007ffff73e1da9 \u003c+41>: pop %rcx\n 0x00007ffff73e1daa \u003c+42>: ret\nEnd of assembler dump.\n(gdb) \n```\n\nAfter:\n\n```\n(gdb) disassemble _asyncio__get_running_loop\nDump of assembler code for function _asyncio__get_running_loop:\n 0x0000555555853c80 \u003c+0>: mov $0xfffffffffffffff0,%rax\n 0x0000555555853c87 \u003c+7>: mov %fs:(%rax),%rax\n 0x0000555555853c8b \u003c+11>: mov 0x358(%rax),%rax\n 0x0000555555853c92 \u003c+18>: test %rax,%rax\n 0x0000555555853c95 \u003c+21>: je 0x555555853ca6 \u003c_asyncio__get_running_loop+38>\n 0x0000555555853c97 \u003c+23>: mov (%rax),%ecx\n 0x0000555555853c99 \u003c+25>: cmp $0xbfffffff,%ecx\n 0x0000555555853c9f \u003c+31>: ja 0x555555853ca5 \u003c_asyncio__get_running_loop+37>\n 0x0000555555853ca1 \u003c+33>: inc %ecx\n 0x0000555555853ca3 \u003c+35>: mov %ecx,(%rax)\n 0x0000555555853ca5 \u003c+37>: ret\n 0x0000555555853ca6 \u003c+38>: lea 0x1f377b(%rip),%rax # 0x555555a47428 \u003c_Py_NoneStruct>\n 0x0000555555853cad \u003c+45>: ret\nEnd of assembler dump.\n```\n\nfree-threading:\n\nBefore:\n```\n(gdb) disassemble _asyncio_Future_done\nDump of assembler code for function _asyncio_Future_done:\n 0x00007ffff7412c30 \u003c+0>: push %r14\n 0x00007ffff7412c32 \u003c+2>: push %rbx\n 0x00007ffff7412c33 \u003c+3>: sub $0x18,%rsp\n 0x00007ffff7412c37 \u003c+7>: mov %rdi,%rbx\n 0x00007ffff7412c3a \u003c+10>: lea 0xa(%rdi),%r14\n 0x00007ffff7412c3e \u003c+14>: mov $0x1,%cl\n 0x00007ffff7412c40 \u003c+16>: xor %eax,%eax\n 0x00007ffff7412c42 \u003c+18>: lock cmpxchg %cl,0xa(%rdi)\n 0x00007ffff7412c47 \u003c+23>: jne 0x7ffff7412c74 \u003c_asyncio_Future_done+68>\n 0x00007ffff7412c49 \u003c+25>: call 0x7ffff740c540 \u003c_PyThreadState_GetCurrent@plt>\n 0x00007ffff7412c4e \u003c+30>: mov %r14,0x10(%rsp)\n 0x00007ffff7412c53 \u003c+35>: mov 0xb0(%rax),%rcx\n 0x00007ffff7412c5a \u003c+42>: mov %rcx,0x8(%rsp)\n 0x00007ffff7412c5f \u003c+47>: lea 0x8(%rsp),%rcx\n 0x00007ffff7412c64 \u003c+52>: mov %rcx,0xb0(%rax)\n 0x00007ffff7412c6b \u003c+59>: cmpq $0x0,0x20(%rbx)\n 0x00007ffff7412c70 \u003c+64>: jne 0x7ffff7412c88 \u003c_asyncio_Future_done+88>\n 0x00007ffff7412c72 \u003c+66>: jmp 0x7ffff7412c8e \u003c_asyncio_Future_done+94>\n 0x00007ffff7412c74 \u003c+68>: lea 0x8(%rsp),%rdi\n 0x00007ffff7412c79 \u003c+73>: mov %r14,%rsi\n 0x00007ffff7412c7c \u003c+76>: call 0x7ffff740c130 \u003c_PyCriticalSection_BeginSlow@plt>\n 0x00007ffff7412c81 \u003c+81>: cmpq $0x0,0x20(%rbx)\n 0x00007ffff7412c86 \u003c+86>: je 0x7ffff7412c8e \u003c_asyncio_Future_done+94>\n 0x00007ffff7412c88 \u003c+88>: cmpl $0x0,0x78(%rbx)\n 0x00007ffff7412c8c \u003c+92>: jne 0x7ffff7412ca1 \u003c_asyncio_Future_done+113>\n 0x00007ffff7412c8e \u003c+94>: mov 0x92f3(%rip),%rbx # 0x7ffff741bf88\n 0x00007ffff7412c95 \u003c+101>: mov 0x10(%rsp),%rdi\n 0x00007ffff7412c9a \u003c+106>: test %rdi,%rdi\n 0x00007ffff7412c9d \u003c+109>: jne 0x7ffff7412cb2 \u003c_asyncio_Future_done+130>\n 0x00007ffff7412c9f \u003c+111>: jmp 0x7ffff7412cdf \u003c_asyncio_Future_done+175>\n 0x00007ffff7412ca1 \u003c+113>: mov 0x92f8(%rip),%rbx # 0x7ffff741bfa0\n 0x00007ffff7412ca8 \u003c+120>: mov 0x10(%rsp),%rdi\n 0x00007ffff7412cad \u003c+125>: test %rdi,%rdi\n 0x00007ffff7412cb0 \u003c+128>: je 0x7ffff7412cdf \u003c_asyncio_Future_done+175>\n 0x00007ffff7412cb2 \u003c+130>: xor %ecx,%ecx\n 0x00007ffff7412cb4 \u003c+132>: mov $0x1,%al\n 0x00007ffff7412cb6 \u003c+134>: lock cmpxchg %cl,(%rdi)\n 0x00007ffff7412cba \u003c+138>: je 0x7ffff7412cc1 \u003c_asyncio_Future_done+145>\n 0x00007ffff7412cbc \u003c+140>: call 0x7ffff740c550 \u003cPyMutex_Unlock@plt>\n 0x00007ffff7412cc1 \u003c+145>: call 0x7ffff740c540 \u003c_PyThreadState_GetCurrent@plt>\n 0x00007ffff7412cc6 \u003c+150>: mov 0x8(%rsp),%rcx\n 0x00007ffff7412ccb \u003c+155>: mov %rcx,0xb0(%rax)\n 0x00007ffff7412cd2 \u003c+162>: test $0x1,%cl\n 0x00007ffff7412cd5 \u003c+165>: je 0x7ffff7412cdf \u003c_asyncio_Future_done+175>\n 0x00007ffff7412cd7 \u003c+167>: mov %rax,%rdi\n 0x00007ffff7412cda \u003c+170>: call 0x7ffff740c390 \u003c_PyCriticalSection_Resume@plt>\n 0x00007ffff7412cdf \u003c+175>: mov %rbx,%rax\n 0x00007ffff7412ce2 \u003c+178>: add $0x18,%rsp\n 0x00007ffff7412ce6 \u003c+182>: pop %rbx\n 0x00007ffff7412ce7 \u003c+183>: pop %r14\n 0x00007ffff7412ce9 \u003c+185>: ret\nEnd of assembler dump.\n```\n\nAfter:\n\n```\n(gdb) disassemble _asyncio_Future_done\nDump of assembler code for function _asyncio_Future_done:\n 0x0000555555892fc0 \u003c+0>: push %rbx\n 0x0000555555892fc1 \u003c+1>: sub $0x10,%rsp\n 0x0000555555892fc5 \u003c+5>: mov %rdi,%rbx\n 0x0000555555892fc8 \u003c+8>: lea 0xa(%rdi),%rsi\n 0x0000555555892fcc \u003c+12>: mov $0x1,%cl\n 0x0000555555892fce \u003c+14>: xor %eax,%eax\n 0x0000555555892fd0 \u003c+16>: lock cmpxchg %cl,0xa(%rdi)\n 0x0000555555892fd5 \u003c+21>: jne 0x555555893005 \u003c_asyncio_Future_done+69>\n 0x0000555555892fd7 \u003c+23>: mov $0xfffffffffffffff0,%rax\n 0x0000555555892fde \u003c+30>: mov %fs:(%rax),%rax\n 0x0000555555892fe2 \u003c+34>: mov %rsi,0x8(%rsp)\n 0x0000555555892fe7 \u003c+39>: mov 0xb0(%rax),%rcx\n 0x0000555555892fee \u003c+46>: mov %rcx,(%rsp)\n 0x0000555555892ff2 \u003c+50>: mov %rsp,%rcx\n 0x0000555555892ff5 \u003c+53>: mov %rcx,0xb0(%rax)\n 0x0000555555892ffc \u003c+60>: cmpq $0x0,0x20(%rbx)\n 0x0000555555893001 \u003c+65>: jne 0x555555893014 \u003c_asyncio_Future_done+84>\n 0x0000555555893003 \u003c+67>: jmp 0x55555589301a \u003c_asyncio_Future_done+90>\n 0x0000555555893005 \u003c+69>: mov %rsp,%rdi\n 0x0000555555893008 \u003c+72>: call 0x5555557a4970 \u003c_PyCriticalSection_BeginSlow>\n 0x000055555589300d \u003c+77>: cmpq $0x0,0x20(%rbx)\n 0x0000555555893012 \u003c+82>: je 0x55555589301a \u003c_asyncio_Future_done+90>\n 0x0000555555893014 \u003c+84>: cmpl $0x0,0x78(%rbx)\n 0x0000555555893018 \u003c+88>: jne 0x555555893034 \u003c_asyncio_Future_done+116>\n 0x000055555589301a \u003c+90>: lea 0x1e9f57(%rip),%rbx # 0x555555a7cf78 \u003c_Py_FalseStruct>\n 0x0000555555893021 \u003c+97>: mov 0x8(%rsp),%rdi\n 0x0000555555893026 \u003c+102>: test %rdi,%rdi\n 0x0000555555893029 \u003c+105>: jne 0x555555893045 \u003c_asyncio_Future_done+133>\n 0x000055555589302b \u003c+107>: mov %rbx,%rax\n 0x000055555589302e \u003c+110>: add $0x10,%rsp\n 0x0000555555893032 \u003c+114>: pop %rbx\n 0x0000555555893033 \u003c+115>: ret\n 0x0000555555893034 \u003c+116>: lea 0x1e9f0d(%rip),%rbx # 0x555555a7cf48 \u003c_Py_TrueStruct>\n 0x000055555589303b \u003c+123>: mov 0x8(%rsp),%rdi\n 0x0000555555893040 \u003c+128>: test %rdi,%rdi\n 0x0000555555893043 \u003c+131>: je 0x55555589302b \u003c_asyncio_Future_done+107>\n 0x0000555555893045 \u003c+133>: xor %ecx,%ecx\n 0x0000555555893047 \u003c+135>: mov $0x1,%al\n 0x0000555555893049 \u003c+137>: lock cmpxchg %cl,(%rdi)\n 0x000055555589304d \u003c+141>: je 0x555555893054 \u003c_asyncio_Future_done+148>\n 0x000055555589304f \u003c+143>: call 0x5555557e2ec0 \u003cPyMutex_Unlock>\n 0x0000555555893054 \u003c+148>: mov (%rsp),%rax\n 0x0000555555893058 \u003c+152>: mov $0xfffffffffffffff0,%rcx\n 0x000055555589305f \u003c+159>: mov %fs:(%rcx),%rdi\n 0x0000555555893063 \u003c+163>: mov %rax,0xb0(%rdi)\n 0x000055555589306a \u003c+170>: test $0x1,%al\n 0x000055555589306c \u003c+172>: je 0x55555589302b \u003c_asyncio_Future_done+107>\n 0x000055555589306e \u003c+174>: call 0x5555557a4ae0 \u003c_PyCriticalSection_Resume>\n 0x0000555555893073 \u003c+179>: mov %rbx,%rax\n 0x0000555555893076 \u003c+182>: add $0x10,%rsp\n 0x000055555589307a \u003c+186>: pop %rbx\n 0x000055555589307b \u003c+187>: ret\nEnd of assembler dump.\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-136670\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kumaraditya303","@type":"Person","name":"kumaraditya303"},"datePublished":"2025-07-15T08:01:00Z","url":"https://github.com/python/cpython/issues/136669"}
| 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:98f07ae7-fdf2-2362-fc91-89144b0fb9c3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC5A:AFBD9:1E2C2A3:27A726C:6B022F5A |
| html-safe-nonce | 4546905d336ab741af874da13e27f151504f8dc376c0641f349d20360f6a681b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzVBOkFGQkQ5OjFFMkMyQTM6MjdBNzI2Qzo2QjAyMkY1QSIsInZpc2l0b3JfaWQiOiI4MTI3NzEwMjM5MTY5NTg5MDgyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f258aa40d6b9261d0e0c204368a3bf8294698bd7c8ee4262f9e14adbc16ea432 |
| hovercard-subject-tag | issue:3231243225 |
| 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/136669/issue_layout |
| twitter:image | https://opengraph.githubassets.com/61656fa9d8413fb9102b23b1f43a7aa7d4d9f0a24a2478897da42e63542a1c11/python/cpython/issues/136669 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/61656fa9d8413fb9102b23b1f43a7aa7d4d9f0a24a2478897da42e63542a1c11/python/cpython/issues/136669 |
| og:image:alt | Currently _asyncio is built as a shared library module which causes it to use slower functions calls for getting the thread state whereas if it is built as static module then it can read the curren... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kumaraditya303 |
| 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