Skip to content

Commit caaff94

Browse files
committed
extmod/uasyncio: Rename and merge TaskQueue push/pop methods.
These are internal names and can be safely renamed without affecting user code. push_sorted() and push_head() are merged into a single push() method, which is already how the C version is implemented. pop_head() is simply renamed to pop(). The changes are: - q.push_sorted(task, t) -> q.push(task, t) - q.push_head(task) -> q.push(task) - q.pop_head() -> q.pop() The shorter names and removal of push_head() leads to a code size reduction of between 40 and 64 bytes on bare-metal targets. Signed-off-by: Damien George <damien@micropython.org>
1 parent 28e7e15 commit caaff94

6 files changed

Lines changed: 31 additions & 35 deletions

File tree

extmod/moduasyncio.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ STATIC mp_obj_t task_queue_peek(mp_obj_t self_in) {
103103
}
104104
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_queue_peek_obj, task_queue_peek);
105105

106-
STATIC mp_obj_t task_queue_push_sorted(size_t n_args, const mp_obj_t *args) {
106+
STATIC mp_obj_t task_queue_push(size_t n_args, const mp_obj_t *args) {
107107
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(args[0]);
108108
mp_obj_task_t *task = MP_OBJ_TO_PTR(args[1]);
109109
task->data = mp_const_none;
@@ -116,9 +116,9 @@ STATIC mp_obj_t task_queue_push_sorted(size_t n_args, const mp_obj_t *args) {
116116
self->heap = (mp_obj_task_t *)mp_pairheap_push(task_lt, TASK_PAIRHEAP(self->heap), TASK_PAIRHEAP(task));
117117
return mp_const_none;
118118
}
119-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(task_queue_push_sorted_obj, 2, 3, task_queue_push_sorted);
119+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(task_queue_push_obj, 2, 3, task_queue_push);
120120

121-
STATIC mp_obj_t task_queue_pop_head(mp_obj_t self_in) {
121+
STATIC mp_obj_t task_queue_pop(mp_obj_t self_in) {
122122
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(self_in);
123123
mp_obj_task_t *head = (mp_obj_task_t *)mp_pairheap_peek(task_lt, &self->heap->pairheap);
124124
if (head == NULL) {
@@ -127,7 +127,7 @@ STATIC mp_obj_t task_queue_pop_head(mp_obj_t self_in) {
127127
self->heap = (mp_obj_task_t *)mp_pairheap_pop(task_lt, &self->heap->pairheap);
128128
return MP_OBJ_FROM_PTR(head);
129129
}
130-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_queue_pop_head_obj, task_queue_pop_head);
130+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_queue_pop_obj, task_queue_pop);
131131

132132
STATIC mp_obj_t task_queue_remove(mp_obj_t self_in, mp_obj_t task_in) {
133133
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(self_in);
@@ -139,9 +139,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(task_queue_remove_obj, task_queue_remove);
139139

140140
STATIC const mp_rom_map_elem_t task_queue_locals_dict_table[] = {
141141
{ MP_ROM_QSTR(MP_QSTR_peek), MP_ROM_PTR(&task_queue_peek_obj) },
142-
{ MP_ROM_QSTR(MP_QSTR_push_sorted), MP_ROM_PTR(&task_queue_push_sorted_obj) },
143-
{ MP_ROM_QSTR(MP_QSTR_push_head), MP_ROM_PTR(&task_queue_push_sorted_obj) },
144-
{ MP_ROM_QSTR(MP_QSTR_pop_head), MP_ROM_PTR(&task_queue_pop_head_obj) },
142+
{ MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&task_queue_push_obj) },
143+
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&task_queue_pop_obj) },
145144
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&task_queue_remove_obj) },
146145
};
147146
STATIC MP_DEFINE_CONST_DICT(task_queue_locals_dict, task_queue_locals_dict_table);
@@ -205,18 +204,18 @@ STATIC mp_obj_t task_cancel(mp_obj_t self_in) {
205204
// Not on the main running queue, remove the task from the queue it's on.
206205
dest[2] = MP_OBJ_FROM_PTR(self);
207206
mp_call_method_n_kw(1, 0, dest);
208-
// _task_queue.push_head(self)
207+
// _task_queue.push(self)
209208
dest[0] = _task_queue;
210209
dest[1] = MP_OBJ_FROM_PTR(self);
211-
task_queue_push_sorted(2, dest);
210+
task_queue_push(2, dest);
212211
} else if (ticks_diff(self->ph_key, ticks()) > 0) {
213212
// On the main running queue but scheduled in the future, so bring it forward to now.
214213
// _task_queue.remove(self)
215214
task_queue_remove(_task_queue, MP_OBJ_FROM_PTR(self));
216-
// _task_queue.push_head(self)
215+
// _task_queue.push(self)
217216
dest[0] = _task_queue;
218217
dest[1] = MP_OBJ_FROM_PTR(self);
219-
task_queue_push_sorted(2, dest);
218+
task_queue_push(2, dest);
220219
}
221220

222221
self->data = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_CancelledError));
@@ -281,7 +280,7 @@ STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
281280
// Put calling task on waiting queue.
282281
mp_obj_t cur_task = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
283282
mp_obj_t args[2] = { self->state, cur_task };
284-
task_queue_push_sorted(2, args);
283+
task_queue_push(2, args);
285284
// Set calling task's data to this task that it waits on, to double-link it.
286285
((mp_obj_task_t *)MP_OBJ_TO_PTR(cur_task))->data = self_in;
287286
}

extmod/uasyncio/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __iter__(self):
4141

4242
def __next__(self):
4343
if self.state is not None:
44-
_task_queue.push_sorted(cur_task, self.state)
44+
_task_queue.push(cur_task, self.state)
4545
self.state = None
4646
return None
4747
else:
@@ -115,11 +115,11 @@ def wait_io_event(self, dt):
115115
# print('poll', s, sm, ev)
116116
if ev & ~select.POLLOUT and sm[0] is not None:
117117
# POLLIN or error
118-
_task_queue.push_head(sm[0])
118+
_task_queue.push(sm[0])
119119
sm[0] = None
120120
if ev & ~select.POLLIN and sm[1] is not None:
121121
# POLLOUT or error
122-
_task_queue.push_head(sm[1])
122+
_task_queue.push(sm[1])
123123
sm[1] = None
124124
if sm[0] is None and sm[1] is None:
125125
self._dequeue(s)
@@ -142,7 +142,7 @@ def create_task(coro):
142142
if not hasattr(coro, "send"):
143143
raise TypeError("coroutine expected")
144144
t = Task(coro, globals())
145-
_task_queue.push_head(t)
145+
_task_queue.push(t)
146146
return t
147147

148148

@@ -167,7 +167,7 @@ def run_until_complete(main_task=None):
167167
_io_queue.wait_io_event(dt)
168168

169169
# Get next task to run and continue it
170-
t = _task_queue.pop_head()
170+
t = _task_queue.pop()
171171
cur_task = t
172172
try:
173173
# Continue running the coroutine, it's responsible for rescheduling itself
@@ -203,15 +203,15 @@ def run_until_complete(main_task=None):
203203
else:
204204
# Schedule any other tasks waiting on the completion of this task.
205205
while t.state.peek():
206-
_task_queue.push_head(t.state.pop_head())
206+
_task_queue.push(t.state.pop())
207207
waiting = True
208208
# "False" indicates that the task is complete and has been await'ed on.
209209
t.state = False
210210
if not waiting and not isinstance(er, excs_stop):
211211
# An exception ended this detached task, so queue it for later
212212
# execution to handle the uncaught exception if no other task retrieves
213213
# the exception in the meantime (this is handled by Task.throw).
214-
_task_queue.push_head(t)
214+
_task_queue.push(t)
215215
# Save return value of coro to pass up to caller.
216216
t.data = er
217217
elif t.state is None:
@@ -256,7 +256,7 @@ def run_until_complete(aw):
256256
def stop():
257257
global _stop_task
258258
if _stop_task is not None:
259-
_task_queue.push_head(_stop_task)
259+
_task_queue.push(_stop_task)
260260
# If stop() is called again, do nothing
261261
_stop_task = None
262262

extmod/uasyncio/event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def set(self):
1717
# Note: This must not be called from anything except the thread running
1818
# the asyncio loop (i.e. neither hard or soft IRQ, or a different thread).
1919
while self.waiting.peek():
20-
core._task_queue.push_head(self.waiting.pop_head())
20+
core._task_queue.push(self.waiting.pop())
2121
self.state = True
2222

2323
def clear(self):
@@ -26,7 +26,7 @@ def clear(self):
2626
async def wait(self):
2727
if not self.state:
2828
# Event not set, put the calling task on the event's waiting queue
29-
self.waiting.push_head(core.cur_task)
29+
self.waiting.push(core.cur_task)
3030
# Set calling task's data to the event's queue so it can be removed if needed
3131
core.cur_task.data = self.waiting
3232
yield

extmod/uasyncio/funcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def done(t, er):
7878
# Still some sub-tasks running.
7979
return
8080
# Gather waiting is done, schedule the main gather task.
81-
core._task_queue.push_head(gather_task)
81+
core._task_queue.push(gather_task)
8282

8383
ts = [core._promote_to_task(aw) for aw in aws]
8484
for i in range(len(ts)):

extmod/uasyncio/lock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ def release(self):
2222
raise RuntimeError("Lock not acquired")
2323
if self.waiting.peek():
2424
# Task(s) waiting on lock, schedule next Task
25-
self.state = self.waiting.pop_head()
26-
core._task_queue.push_head(self.state)
25+
self.state = self.waiting.pop()
26+
core._task_queue.push(self.state)
2727
else:
2828
# No Task waiting so unlock
2929
self.state = 0
3030

3131
async def acquire(self):
3232
if self.state != 0:
3333
# Lock unavailable, put the calling Task on the waiting queue
34-
self.waiting.push_head(core.cur_task)
34+
self.waiting.push(core.cur_task)
3535
# Set calling task's data to the lock's queue so it can be removed if needed
3636
core.cur_task.data = self.waiting
3737
try:

extmod/uasyncio/task.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,14 @@ def __init__(self):
9999
def peek(self):
100100
return self.heap
101101

102-
def push_sorted(self, v, key):
102+
def push(self, v, key=None):
103103
assert v.ph_child is None
104104
assert v.ph_next is None
105105
v.data = None
106-
v.ph_key = key
106+
v.ph_key = key if key is not None else core.ticks()
107107
self.heap = ph_meld(v, self.heap)
108108

109-
def push_head(self, v):
110-
self.push_sorted(v, core.ticks())
111-
112-
def pop_head(self):
109+
def pop(self):
113110
v = self.heap
114111
assert v.ph_next is None
115112
self.heap = ph_pairing(v.ph_child)
@@ -150,7 +147,7 @@ def __next__(self):
150147
raise self.data
151148
else:
152149
# Put calling task on waiting queue.
153-
self.state.push_head(core.cur_task)
150+
self.state.push(core.cur_task)
154151
# Set calling task's data to this task that it waits on, to double-link it.
155152
core.cur_task.data = self
156153

@@ -171,10 +168,10 @@ def cancel(self):
171168
if hasattr(self.data, "remove"):
172169
# Not on the main running queue, remove the task from the queue it's on.
173170
self.data.remove(self)
174-
core._task_queue.push_head(self)
171+
core._task_queue.push(self)
175172
elif core.ticks_diff(self.ph_key, core.ticks()) > 0:
176173
# On the main running queue but scheduled in the future, so bring it forward to now.
177174
core._task_queue.remove(self)
178-
core._task_queue.push_head(self)
175+
core._task_queue.push(self)
179176
self.data = core.CancelledError
180177
return True

0 commit comments

Comments
 (0)