Title: Reduce asyncio heapq overhead · Issue #122881 · python/cpython · GitHub
Open Graph Title: Reduce asyncio heapq overhead · Issue #122881 · python/cpython
X Title: Reduce asyncio heapq overhead · Issue #122881 · python/cpython
Description: Feature or enhancement Proposal: The heapq calls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run __lt__ quite often in TimerHandle cpython/Lib/asyncio/events.py Line 128 in 0fd97e4 def __l...
Open Graph Description: Feature or enhancement Proposal: The heapq calls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run __lt__ quite often in TimerHandle cpython/Lib/async...
X Description: Feature or enhancement Proposal: The heapq calls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run __lt__ quite often in TimerHandle cpython/Lib/async...
Opengraph URL: https://github.com/python/cpython/issues/122881
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reduce asyncio heapq overhead","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nThe `heapq` calls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run `__lt__` quite often in `TimerHandle` https://github.com/python/cpython/blob/0fd97e46c75bb3060485b796ca597b13af7e6bec/Lib/asyncio/events.py#L128\r\n\r\nhttps://github.com/python/cpython/blob/0fd97e46c75bb3060485b796ca597b13af7e6bec/Lib/asyncio/base_events.py#L1968\r\n\r\n\r\nhttps://github.com/python/cpython/blob/0fd97e46c75bb3060485b796ca597b13af7e6bec/Lib/asyncio/base_events.py#L815\r\n\r\n\r\n\r\nhttps://github.com/python/cpython/blob/0fd97e46c75bb3060485b796ca597b13af7e6bec/Lib/asyncio/base_events.py#L1975\r\n\r\n\r\n\r\nAvoiding running `__lt__` can speed up processing `call_at`s by ~10%\r\n\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nThis is a minor feature, which does not need previous discussion elsewhere\r\n\r\n### Links to previous discussion of this feature:\r\n\r\nWrapping `TimerHandle` in a tuple starting with `when` avoids the `__lt__` call. Thank you to whoever wrote the `heapq` docs for help getting there https://docs.python.org/3/library/heapq.html#basic-examples \r\n6f80b4c8b7be\r\n\r\n\r\nExample benchmark\r\n```python\r\nfrom asyncio import TimerHandle\r\nimport heapq\r\nimport timeit\r\n\r\n\r\ndef callback():\r\n \"\"\"This is the callback function that will be called when the timer expires.\"\"\"\r\n\r\n\r\nclass MockLoop:\r\n def get_debug(self):\r\n return False\r\n\r\n\r\nloop = MockLoop()\r\n\r\n\r\ndef heap_tuple():\r\n scheduled = []\r\n when = 1\r\n\r\n for _ in range(100):\r\n when += 1\r\n handle = TimerHandle(when, callback, (), loop)\r\n heapq.heappush(scheduled, (when, handle))\r\n\r\n while scheduled:\r\n when, handle = heapq.heappop(scheduled)\r\n\r\n\r\ndef heap_handle():\r\n scheduled = []\r\n when = 1\r\n\r\n for _ in range(100):\r\n when += 1\r\n handle = TimerHandle(when, callback, (), loop)\r\n heapq.heappush(scheduled, handle)\r\n\r\n while scheduled:\r\n handle = heapq.heappop(scheduled)\r\n\r\n\r\nprint(\"wrap when, TimerHandle in tuple\", timeit.timeit(heap_tuple))\r\nprint(\"bare TimerHandle\", timeit.timeit(heap_handle))\r\n```\r\n\r\n```\r\n% python3 bench/timer_handle_heap.py\r\nwrap when, TimerHandle in tuple 34.082984749999014\r\nbare TimerHandle 49.678519583001616\r\n```\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-122882\r\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/bdraco","@type":"Person","name":"bdraco"},"datePublished":"2024-08-10T12:58:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/122881/cpython/issues/122881"}
| 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:f55a4218-8605-2b8e-f4d6-221df60ddbdb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ACD2:E3F0D:200AF19:29B0B07:696B473A |
| html-safe-nonce | bd68fad55957289280360d415ecfd18e767f8dae16c2cbd1ece1089eb2f04cab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0QyOkUzRjBEOjIwMEFGMTk6MjlCMEIwNzo2OTZCNDczQSIsInZpc2l0b3JfaWQiOiIyMDMxOTM5NzgyNDQxNjQ5OTc4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 02f23efd0e43479f8af9189fc19d6b85d2dbaf419ffe224a318167cc986bbfbd |
| hovercard-subject-tag | issue:2459074533 |
| 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/122881/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2b42594401b9779a6592e845d481480fae061497153d1c872b08d8f0e3a2bdde/python/cpython/issues/122881 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2b42594401b9779a6592e845d481480fae061497153d1c872b08d8f0e3a2bdde/python/cpython/issues/122881 |
| og:image:alt | Feature or enhancement Proposal: The heapq calls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run __lt__ quite often in TimerHandle cpython/Lib/async... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bdraco |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width