-
Notifications
You must be signed in to change notification settings - Fork 165
utime.ticks_diff() should handle wrap-arounds #231
Comments
ticks_diff() handles wrap-arounds. It's only that ticks_ms() and ticks_us() overflow at 0xffffffff. uasyncio expects the overflow at 0x3fffffff. In micropython.org, where utimeq comes form, ticks_ms() overlows at 0x3fffffff. So these two have to be matched. It seems easier to do that in the ticks domain, because ticks_ms() and ticks_us() are not guaranteed to overflow at a certain value. It's only certain that they will. |
I don’t see anything in the code that accounts for it. I just see a substraction. What am I missing? I ended up creating a work around in .py that masks the ticks_ms() time as well as forces the value to be a small int using the hash() function. Super gross hacky workaround but it works... |
Tangentially: I think it would be better to change ticks_...() to also wrap at 0x3fffffff. Aside of the compatibility with original MicroPython another downside of the current implementation is that values above 0x3fffffff get heap allocated, instead of being efficient “small ints”. |
It is relatively easy to revert back to the methods of the MicroPython.org version. The code is still there in extmod/utime_mphal.c and just has to be enabled. Add to mpconfigport.h the line:
and change the QSTR definition list at the end of esp32/mods/modutime.c into:
Also add the needed declarations to esp32/mods/modutime.c with:
The respective ticks... and sleep... functions in esp32/mods/modutime.c can then be removed or undef'ed. |
There is just on "property" with these genuine implementations of ticks_diff() and ticks_add(). The "time" arguments must be short int's. There is no conversion in the functions which convert other int formats. So the arguments must either come from one of the ticks_xx calls, or be a constant smaller than 1,073,741,824. Since that is the intended use, it's not real limitation, and since these functions are often used in tight loops, there is a speed advantage. It just came to my eyes when testing the adapted code and puzzled me for a while. |
Hello! |
Uh oh!
There was an error while loading. Please reload this page.
The original
utime.ticks_diff
from micropython supports handling wrap-arounds / roll-overs using serial distance: https://docs.micropython.org/en/latest/library/utime.html#utime.ticks_diffIn the Pycom implementation this has been removed (in favor of supporting diffing usec-accuracy timestamps?).
This seem like a bad idea to me because it breaks API compatibility with micropython.
For example,
uasyncio
relies onutime.ticks_diff
. Right now theuasyncio
scheduler will start to malfunction after 12.42 days of uptime...cc @iwahdan88 @robert-hh
The text was updated successfully, but these errors were encountered: