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
| heapq.heapify(new_scheduled) |

| heapq.heappush(self._scheduled, timer) |

| handle=heapq.heappop(self._scheduled) |

Avoiding running __lt__ can speed up processing call_ats by ~10%
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
Wrapping 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
6f80b4c
Example benchmark
fromasyncioimportTimerHandleimportheapqimporttimeitdefcallback():
"""This is the callback function that will be called when the timer expires."""classMockLoop:
defget_debug(self):
returnFalseloop=MockLoop()
defheap_tuple():
scheduled= []
when=1for_inrange(100):
when+=1handle=TimerHandle(when, callback, (), loop)
heapq.heappush(scheduled, (when, handle))
whilescheduled:
when, handle=heapq.heappop(scheduled)
defheap_handle():
scheduled= []
when=1for_inrange(100):
when+=1handle=TimerHandle(when, callback, (), loop)
heapq.heappush(scheduled, handle)
whilescheduled:
handle=heapq.heappop(scheduled)
print("wrap when, TimerHandle in tuple", timeit.timeit(heap_tuple))
print("bare TimerHandle", timeit.timeit(heap_handle))% python3 bench/timer_handle_heap.py
wrap when, TimerHandle in tuple 34.082984749999014
bare TimerHandle 49.678519583001616
Linked PRs
Feature or enhancement
Proposal:
The
heapqcalls in base_events.py represents quite a bit of asyncio scheduling overhead because they have to run__lt__quite often inTimerHandlecpython/Lib/asyncio/events.py
Line 128 in 0fd97e4
cpython/Lib/asyncio/base_events.py
Line 1968 in 0fd97e4
cpython/Lib/asyncio/base_events.py
Line 815 in 0fd97e4
cpython/Lib/asyncio/base_events.py
Line 1975 in 0fd97e4
Avoiding running
__lt__can speed up processingcall_ats by ~10%Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
Wrapping
TimerHandlein a tuple starting withwhenavoids the__lt__call. Thank you to whoever wrote theheapqdocs for help getting there https://docs.python.org/3/library/heapq.html#basic-examples6f80b4c
Example benchmark
Linked PRs