From 222758f0c223d8a8bdeddc9cdc5c9f3b2a3f049b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 22 Dec 2016 13:04:09 +0300 Subject: [PATCH] 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. --- uasyncio.core/uasyncio/core.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/uasyncio.core/uasyncio/core.py b/uasyncio.core/uasyncio/core.py index 056c7af5..758b47d2 100644 --- a/uasyncio.core/uasyncio/core.py +++ b/uasyncio.core/uasyncio/core.py @@ -2,7 +2,7 @@ try: import utime as time except ImportError: import time -import uheapq as heapq +import utimeq import logging @@ -14,8 +14,8 @@ type_gen = type((lambda: (yield))()) class EventLoop: - def __init__(self): - self.q = [] + def __init__(self, len=128): + self.q = utimeq.utimeq(len) def time(self): return time.ticks_ms() @@ -37,12 +37,12 @@ class EventLoop: def call_at(self, time, callback, *args): if __debug__ and DEBUG: log.debug("Scheduling %s", (time, callback, args)) - heapq.heappush(self.q, (time, callback, args), True) + self.q.push(time, callback, args) def call_at_(self, time, callback, args=()): if __debug__ and DEBUG: log.debug("Scheduling %s", (time, callback, args)) - heapq.heappush(self.q, (time, callback, args), True) + self.q.push(time, callback, args) def wait(self, delay): # Default wait implementation, to be overriden in subclasses @@ -52,9 +52,13 @@ class EventLoop: time.sleep_ms(delay) def run_forever(self): + cur_task = [0, 0, 0] while True: if self.q: - t, cb, args = heapq.heappop(self.q, True) + self.q.pop(cur_task) + t = cur_task[0] + cb = cur_task[1] + args = cur_task[2] if __debug__ and DEBUG: log.debug("Next coroutine to run: %s", (t, cb, args)) # __main__.mem_info()