Skip to content

Commit 222758f

Browse files
committed
uasyncio.core: Switch to dedicated utimeq class.
Allows zero-allocation scheduling of tasks. As long as tasks don't use await/yield from with coroutines, and don't allocate memory themselves, there will be no allocation and GC.
1 parent 845f8eb commit 222758f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

uasyncio.core/uasyncio/core.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import utime as time
33
except ImportError:
44
import time
5-
import uheapq as heapq
5+
import utimeq
66
import logging
77

88

@@ -14,8 +14,8 @@
1414

1515
class EventLoop:
1616

17-
def __init__(self):
18-
self.q = []
17+
def __init__(self, len=128):
18+
self.q = utimeq.utimeq(len)
1919

2020
def time(self):
2121
return time.ticks_ms()
@@ -37,12 +37,12 @@ def call_later_ms_(self, delay, callback, args=()):
3737
def call_at(self, time, callback, *args):
3838
if __debug__ and DEBUG:
3939
log.debug("Scheduling %s", (time, callback, args))
40-
heapq.heappush(self.q, (time, callback, args), True)
40+
self.q.push(time, callback, args)
4141

4242
def call_at_(self, time, callback, args=()):
4343
if __debug__ and DEBUG:
4444
log.debug("Scheduling %s", (time, callback, args))
45-
heapq.heappush(self.q, (time, callback, args), True)
45+
self.q.push(time, callback, args)
4646

4747
def wait(self, delay):
4848
# Default wait implementation, to be overriden in subclasses
@@ -52,9 +52,13 @@ def wait(self, delay):
5252
time.sleep_ms(delay)
5353

5454
def run_forever(self):
55+
cur_task = [0, 0, 0]
5556
while True:
5657
if self.q:
57-
t, cb, args = heapq.heappop(self.q, True)
58+
self.q.pop(cur_task)
59+
t = cur_task[0]
60+
cb = cur_task[1]
61+
args = cur_task[2]
5862
if __debug__ and DEBUG:
5963
log.debug("Next coroutine to run: %s", (t, cb, args))
6064
# __main__.mem_info()

0 commit comments

Comments
 (0)