-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
asyncio: slight optimizations for run_until_complete
and sleep_ms
#17699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
greezybacon
wants to merge
1
commit into
micropython:master
Choose a base branch
from
greezybacon:fix/improve-asyncio-core-loop-perf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+21
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
from time import ticks_ms as ticks, ticks_diff, ticks_add | ||
import sys, select | ||
from select import POLLIN, POLLOUT | ||
|
||
# Import TaskQueue and Task, preferring built-in C code over Python code | ||
try: | ||
|
@@ -54,7 +55,8 @@ def __next__(self): | |
# Use a SingletonGenerator to do it without allocating on the heap | ||
def sleep_ms(t, sgen=SingletonGenerator()): | ||
assert sgen.state is None | ||
sgen.state = ticks_add(ticks(), max(0, t)) | ||
now = ticks() | ||
sgen.state = ticks_add(now, t) if t > 0 else now | ||
return sgen | ||
|
||
|
||
|
@@ -66,6 +68,8 @@ def sleep(t): | |
################################################################################ | ||
# Queue and poller for stream IO | ||
|
||
nPOLLIN = ~POLLIN | ||
nPOLLOUT = ~POLLOUT | ||
|
||
class IOQueue: | ||
def __init__(self): | ||
|
@@ -77,13 +81,13 @@ def _enqueue(self, s, idx): | |
entry = [None, None, s] | ||
entry[idx] = cur_task | ||
self.map[id(s)] = entry | ||
self.poller.register(s, select.POLLIN if idx == 0 else select.POLLOUT) | ||
self.poller.register(s, POLLIN if idx == 0 else POLLOUT) | ||
else: | ||
sm = self.map[id(s)] | ||
assert sm[idx] is None | ||
assert sm[1 - idx] is not None | ||
sm[idx] = cur_task | ||
self.poller.modify(s, select.POLLIN | select.POLLOUT) | ||
self.poller.modify(s, POLLIN | POLLOUT) | ||
# Link task to this IOQueue so it can be removed if needed | ||
cur_task.data = self | ||
|
||
|
@@ -114,20 +118,20 @@ def wait_io_event(self, dt): | |
for s, ev in self.poller.ipoll(dt): | ||
sm = self.map[id(s)] | ||
# print('poll', s, sm, ev) | ||
if ev & ~select.POLLOUT and sm[0] is not None: | ||
if ev & nPOLLOUT and sm[0] is not None: | ||
# POLLIN or error | ||
_task_queue.push(sm[0]) | ||
sm[0] = None | ||
if ev & ~select.POLLIN and sm[1] is not None: | ||
if ev & nPOLLIN and sm[1] is not None: | ||
# POLLOUT or error | ||
_task_queue.push(sm[1]) | ||
sm[1] = None | ||
if sm[0] is None and sm[1] is None: | ||
self._dequeue(s) | ||
elif sm[0] is None: | ||
self.poller.modify(s, select.POLLOUT) | ||
self.poller.modify(s, POLLOUT) | ||
else: | ||
self.poller.modify(s, select.POLLIN) | ||
self.poller.modify(s, POLLIN) | ||
|
||
|
||
################################################################################ | ||
|
@@ -153,24 +157,29 @@ def run_until_complete(main_task=None): | |
global cur_task | ||
excs_all = (CancelledError, Exception) # To prevent heap allocation in loop | ||
excs_stop = (CancelledError, StopIteration) # To prevent heap allocation in loop | ||
queue_peek = _task_queue.peek | ||
queue_pop = _task_queue.pop | ||
wait_io_event = _io_queue.wait_io_event | ||
while True: | ||
# Wait until the head of _task_queue is ready to run | ||
dt = 1 | ||
while dt > 0: | ||
while dt: | ||
dt = -1 | ||
t = _task_queue.peek() | ||
t = queue_peek() | ||
if t: | ||
# A task waiting on _task_queue; "ph_key" is time to schedule task at | ||
dt = max(0, ticks_diff(t.ph_key, ticks())) | ||
dt = ticks_diff(t.ph_key, ticks()) | ||
if dt < 0: | ||
dt = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, does this change here actually make things faster? |
||
elif not _io_queue.map: | ||
# No tasks can be woken so finished running | ||
cur_task = None | ||
return | ||
# print('(poll {})'.format(dt), len(_io_queue.map)) | ||
_io_queue.wait_io_event(dt) | ||
wait_io_event(dt) | ||
|
||
# Get next task to run and continue it | ||
t = _task_queue.pop() | ||
t = queue_pop() | ||
cur_task = t | ||
try: | ||
# Continue running the coroutine, it's responsible for rescheduling itself | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this give a measurable speed improvement? Is it worth it for the cost in code size?
I measure this change here as +5 bytes to the bytecode. The most taken path will be when
ticks_add()
needs to be called, which goes from 12 opcodes previously to now 16 opcodes. It's usually the opcode overhead that's slow, rather than the actual call (eg out tomax
, which should be quick with two small int args). So I would guess that this change actually makes things a little slower.