Skip to content

Commit 568636d

Browse files
committed
run_background_tasks: Do nothing unless there has been a tick
This improves performance of running python code by 34%, based on the "pystone" benchmark on metro m4 express at 5000 passes (1127.65 -> 1521.6 passes/second). In addition, by instrumenting the tick function and monitoring on an oscilloscope, the time actually spent in run_background_tasks() on the metro m4 decreases from average 43% to 0.5%. (however, there's some additional overhead that is moved around and not accounted for in that "0.5%" figure, each time supervisor_run_background_tasks_if_tick is called but no tick has occurred) On the CPB, it increases pystone from 633 to 769, a smaller percentage increase of 21%. I did not measure the time actually spent in run_background_tasks() on CPB. Testing performed: on metro m4 and cpb, run pystone adapted from python3.4 (change time.time to time.monotonic for sub-second resolution) Besides running a 5000 pass test, I also ran a 50-pass test while scoping how long an output pin was set. Average: 34.59ms or 1445/s on m4, 67.61ms or 739/s on cbp, both matching the other pystone result reasonably well. import pystone import board import digitalio import time d = digitalio.DigitalInOut(board.D13) d.direction = digitalio.Direction.OUTPUT while True: d.value = 0 time.sleep(.01) d.value = 1 pystone.main(50)
1 parent 40a47d4 commit 568636d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

py/circuitpy_mpconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,8 @@ extern const struct _mp_obj_module_t ustack_module;
653653
FLASH_ROOT_POINTERS \
654654
NETWORK_ROOT_POINTERS \
655655

656-
void run_background_tasks(void);
657-
#define RUN_BACKGROUND_TASKS (run_background_tasks())
656+
void supervisor_run_background_tasks_if_tick(void);
657+
#define RUN_BACKGROUND_TASKS (supervisor_run_background_tasks_if_tick())
658658

659659
// TODO: Used in wiznet5k driver, but may not be needed in the long run.
660660
#define MICROPY_THREAD_YIELD()

supervisor/shared/tick.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdatomic.h>
28+
2729
#include "supervisor/shared/tick.h"
2830
#include "supervisor/filesystem.h"
2931
#include "supervisor/shared/autoreload.h"
3032

33+
static atomic_bool tick_up;
3134
static volatile uint64_t ticks_ms;
3235

3336
#if CIRCUITPY_GAMEPAD
@@ -44,6 +47,7 @@ void supervisor_tick(void) {
4447

4548
ticks_ms ++;
4649

50+
atomic_store(&tick_up, true);
4751

4852
#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
4953
filesystem_tick();
@@ -74,3 +78,11 @@ uint64_t supervisor_ticks_ms64() {
7478
uint32_t supervisor_ticks_ms32() {
7579
return ticks_ms;
7680
}
81+
82+
extern void run_background_tasks(void);
83+
84+
void supervisor_run_background_tasks_if_tick() {
85+
if (atomic_exchange(&tick_up, false)) {
86+
run_background_tasks();
87+
}
88+
}

supervisor/shared/tick.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
extern void supervisor_tick(void);
3434
extern uint32_t supervisor_ticks_ms32(void);
3535
extern uint64_t supervisor_ticks_ms64(void);
36+
extern void supervisor_run_background_if_tick(void);
3637

3738
#endif

0 commit comments

Comments
 (0)