Skip to content

Benchmark findings: the baseline

This page covers MicroGoose at perf-baseline against native C. Both run on the same three boards, with the same clocks, RTOS settings, priorities and stack sizes. Each number comes from the runs in Results: 5 boots per run, every sample pooled, at revision d9d2275. That revision is perf-baseline plus the benchmark code, with no runtime changes. The ratios below are Goose's median divided by C's, so 2.0 means Goose took twice as long.

Each finding says what was measured. Where it offers a reason, it's marked as a hypothesis: the profiling phase will test it, and nothing has been optimized yet.

Where Goose matches C

S3 WROOM H7
mandel_f64 1.00 1.00 1.01
mandel_f32 1.03 1.08 1.04
scale_main (Mandelbrot at 64 × 48, 256 iterations) 1.03 — 1.00
clock_1000 1.00 1.00 2.90 (see below)
timer_idle_gap, timer_busy_gap: 1 ms ticks, timestamps 1000 µs apart (p99) equal equal equal

Floating-point loops with few array accesses compile to code as fast as C's. The program's own tasks don't hold the timers up, idle or busy, in either language.

Where Goose is ahead

  • Timer wake-up while a worker computes. A worker spins at the same priority as the program, and a 1 ms timer ticks. This is the time from the tick to the program having it:

    Goose median (p99) C median (p99)
    WROOM 17 µs (17) 1,357 µs (1,856)
    H7 4 µs (4) 663 µs (965)
    S3 147 µs (1,554) 1,386 µs (1,884)

    The H7 port waits for an interrupt one priority up, so the interrupt switches to the waiting task at once (PR #30). The C reference waits on a FreeRTOS queue at the worker's priority, so it gets the core only when the time slice ends.

    The C medians stay nearly constant within a boot but differ between boots: on the H7 they range from 115 to 965 µs. That fits the phase between the timer and the 1 ms tick, which is fixed for a boot (hypothesis).

    On the S3, Goose's per-boot medians spread from 14 to 1,058 µs, so its result there depends on where the scheduler puts the worker. That is not established yet. A C programmer would raise the waiting task's priority, and the C reference deliberately doesn't, to keep the priorities equal. So this measures MicroGoose's default against C's default, not against tuned C.

  • crc32. 0.90 on the S3, 0.89 on the WROOM and 0.79 on the H7: Goose was faster in every run. Code placement alone moves this benchmark. On the H7, the same C source took 2,171 µs and 2,425 µs in two builds that differ only elsewhere in the image (c and c-fpu). So only the H7's 21% margin exceeds a swing that's been measured. Why Goose's loop is faster hasn't been looked at.

  • Contained faults. A worker's out-of-bounds write ends only that worker, and the supervisor learns of it from thread_wait with the message. That took 478 µs on the H7, 527 µs on the WROOM and 1,693 µs on the S3, spawn included. After 20 faults, a normal worker still ran. C has nothing comparable: its fault ends the program.

Where Goose is behind

S3 WROOM H7
queue_2000 (2000 messages, worker → main) 13.0 18.4 5.2
spawn_wait (spawn and wait for an empty worker) 18.1 3.1 4.8
pingpong (round trip through two queues) 4.7 4.6 2.0
matmul_f32 3.0 3.5 1.07
heapsort 2.0 2.0 2.2
clock_1000 1.00 1.00 2.9
timer_idle_wake (no worker) 13 vs 8 µs 17 vs 10 µs 1 vs 2 µs (ahead)

In absolute terms, a message costs about 22 µs on the S3 and 44 µs on the WROOM, against C's 1.7 µs and 2.4 µs. On the H7 it's 4.8 µs against 0.9 µs. A round trip on the S3 takes 71 µs against 15 µs.

Hypotheses for the profiling phase. None of these is measured yet:

  • Queues. Each Goose message may be allocated, copied and freed, under a pthread mutex and condition variable. A FreeRTOS queue copies into a preallocated ring.
  • Spawning on the S3. It takes 1,465 µs, against 306 µs on the slower-clocked WROOM. That points at memory placement: the S3 has PSRAM and the WROOM doesn't.
  • matmul_f32 and heapsort. Goose checks array bounds and indexes with i64, which takes two registers on these 32-bit cores. On the H7, software floating point dominates matmul, which would hide the difference there.
  • The H7 clock. Goose's micros() goes through clock_gettime(). That converts cycles to nanoseconds, then nanoseconds to seconds and back to microseconds, in 64-bit divisions in software. It costs 0.75 µs a read, against 0.26 µs for C's direct conversion.
  • The ESP32s' idle timer wake. timer_every uses esp_timer, whose callbacks run in a task. The C reference takes a GPTimer interrupt directly.

Hardware the baseline leaves unused: the H7's FPU

MicroGoose's H7 build uses software floating point, as PlatformIO's board default does. The same C built for the FPU (c-fpu) shows the cost:

c c-fpu ratio
mandel_f64 74,956 µs 6,961 µs 0.093
mandel_f32 51,052 µs 3,683 µs 0.072
matmul_f32 5,250 µs 607 µs 0.116
scale_main 279,158 µs 25,544 µs 0.092

Floating-point work runs 8.6 to 14 times faster with the FPU. That is the largest single gap on any board. On the S3, f64 is 22 times slower than f32, because its FPU is single-precision, in both languages.

Multicore

The times are in Results.

Speedup over 1 worker 2 workers 4 workers
S3, Goose 1.92 1.90
S3, C 1.94 1.94
WROOM, C 1.82 1.82
H7, Goose and C 1.00 1.00 (one core)

On the S3, Goose scales across both cores as well as C does, to within 1%. A worker costs nothing measurable over the same work on main: the 1-worker run took 0.997 times as long as main on the S3, and 1.001 times on the H7.

On the WROOM, Goose couldn't start a second worker, as the next section explains.

Memory and size

S3 WROOM H7
Image (flash), Goose / C 251 / 187 KB 232 / 173 KB 60 / 22 KB
Static RAM, Goose / C 60 / 78 KB 57 / 75 KB 54 / 63 KB
Free heap when the suite starts, Goose / C 8,605 / 8,666 KB 130 / 188 KB 363 / 420 KB

The C suite keeps its sample buffers in static RAM, which is why its static RAM is larger. The Goose program reserves its data stacks from the heap before main: six 16 KB stacks (96 KB) on every board. The image carries the Goose runtime, about 38 KB on the H7 and 59–64 KB on the ESP32s, where ESP-IDF's components count too. Everything in the image sizes comes from the build logs kept with each run.

The WROOM can't run the suite's scaling benchmarks.

  • What fails: in all 5 boots, the second worker of scale_2w failed to get a data stack, with 130 KB still free (goose runtime error: cannot reserve data stack address space).
  • Why: the ESP32 port takes a data stack from internal RAM only while that leaves 64 KB for ESP-IDF (MG_INTERNAL_KEEP). The WROOM has no PSRAM to spill into, and every data stack in a program has the same reservation. The suite has a 16 KB global, so it can't use smaller stacks: at 8 KB the program overflowed initializing its globals (in results/dev).
  • Where the WROOM numbers come from: its timer and fault numbers come from runs of those groups alone (--only), on fresh boots.
  • Since Optimizations, section 9 the WROOM runs scale_2w as built, and the whole suite when its workers' data stacks are 4 KB (--worker-reserve 4K).

Rust against C

Rust runs on Embassy: async tasks on a cooperative executor, with no RTOS. Methodology lists how it differs from C. Each board has 5 boots at ba8db73, and every result matches C's.

Tasks and channels. Embassy's are far cheaper than FreeRTOS tasks and queues, on every board:

Rust ÷ C S3 WROOM H7
spawn_wait 0.05 (4 vs 81 µs) 0.04 (4 vs 98 µs) 0.01 (1 vs 94 µs)
pingpong 0.40 0.33 0.25
queue_2000 0.57 0.49 0.30

Starting a task reuses a slot in a static pool, and a channel is a ring in static memory. Nothing is allocated, and no context switch takes place between tasks on one core. This is what makes Embassy's model cheap (explanation, not profiled). The cost is preemption: a task that doesn't await holds its core.

Timer wake-up beside a busy worker. The busy worker yields every 4,096 iterations. The program then has its tick after 261 µs on the S3, 288 µs on the WROOM and 32 µs on the H7. That's ahead of C, which waits for the 1 ms time slice, but behind MicroGoose on the WROOM and the H7 (17 and 4 µs). The figure follows how often the worker yields, which the programmer chooses.

Floating point on the H7. Rust uses the hardware FPU, which is the default for its target, so it runs with c-fpu, not c. For example, mandel_f64 takes 7,000 µs, against 6,961 for c-fpu and 74,956 for c.

Where Rust is behind C.

Rust ÷ C S3 WROOM H7
matmul_f32 2.58 2.52 1.03 against c-fpu
heapsort 1.51 1.47 1.16
mandel_f32 1.18 1.26 1.05 against c-fpu

matmul_f32 and heapsort index arrays in their inner loops. Rust checks those indexes unless it can prove them in range, which would explain the gap (hypothesis, not profiled). The C build doesn't check them, and Goose does: Goose is at 3.0 and 2.0 on the S3.

crc32. On the H7, Rust took 268 µs against C's 2,171. LLVM recognized the bit-by-bit CRC loop and replaced it with a 256-entry lookup table: the ELF holds .L.crctable, whose entries are the standard CRC-32 table's. GCC doesn't do this, and neither does espup's Xtensa LLVM, which has no such table in the ESP32 builds. There Rust is at 0.89, level with Goose.

Multicore. On the S3, Rust's 2 workers took 0.51 times as long as one, against C's 0.52. On the WROOM, where Goose can't run a second worker, Rust and C are level at 0.55.

Size.

  • Image: Rust's is smaller than C's on the ESP32s (102 KB against 187 KB on the S3), since there is no ESP-IDF. On the H7 it's larger (46 KB against 22 KB).
  • Static RAM: larger everywhere (116 KB against 78 KB on the S3). Everything is static: the 2,000-message channel alone is 32 KB, and there is no heap.

TinyGo against C

TinyGo runs the suite as Go: goroutines, channels and a garbage-collected heap. Methodology lists how it differs from C. Each board has 5 boots at 06500d6. Every result matches C's, except timer_busy on TinyGo's multicore scheduler (below).

The WROOM runs at 160 MHz. That's the clock TinyGo's runtime sets on the ESP32, against 240 MHz for C, Rust and Goose. The benchmarks that only compute show it. mandel_f64 and scale_1w are both at 1.50 × C on the WROOM, which is 240 / 160: per cycle, TinyGo matches C there. On the S3, which TinyGo runs at 240 MHz, mandel_f64 is at 1.00.

Compute.

TinyGo ÷ C S3 WROOM (160 MHz) H7 (software float)
crc32 0.22 0.44 0.12
heapsort 1.19 1.70 1.39
mandel_f64 1.00 1.50 1.77
mandel_f32 1.21 1.93 1.83
matmul_f32 1.61 2.18 1.97
  • crc32. It runs at 888 µs on the S3 against C's 3,977. All three TinyGo ELFs hold the standard CRC-32 table, which fits LLVM replacing the bit loop with a table lookup, as it did for Rust on the H7. Unlike espup's LLVM, TinyGo's (LLVM 22) does this on Xtensa too. The table wasn't traced to the function.
  • Indexed loops. heapsort and matmul_f32 are behind on every board, as they are for Rust and Goose. Go checks array indexes, which would explain it (hypothesis, not profiled).
  • Floating point on the H7. TinyGo's cortex-m7 target uses software floating point, as c does, and it's slower: mandel_f64 takes 132,378 µs against 74,956. TinyGo's software float comes from LLVM's compiler-rt, and GCC's from libgcc, which would explain the gap (hypothesis, not profiled).

Goroutines and channels.

TinyGo ÷ C S3 WROOM H7
spawn_wait 0.62 (50 vs 81 µs) 0.85 (83 vs 98 µs) 0.19 (18 vs 94 µs)
pingpong 0.67 0.81 0.38
queue_2000 1.42 1.77 0.74
  • Medians. A goroutine switch is cheaper than a FreeRTOS task switch, and the medians show it. Starting a goroutine allocates its stack from the heap, though.
  • spawn_wait tail on the ESP32s. It has spikes of about 2 ms (p99 2,050 µs on the S3, 1,440 µs on the WROOM): likely the garbage collector running when an allocation finds the heap full (not profiled).
  • queue_2000. It's behind C on the ESP32s, but not on the H7.

Timer wake-up.

  • Idle, ESP32s: a TinyGo bug. With nothing else to run, a goroutine woken by the timer interrupt runs only when the scheduler's sleep ends: at the 100 ms receive timeout. So timer_idle_wake has a median of 91,467 µs on both ESP32s, and most ticks are dropped while the channel is full (timer_idle_gap p99 84,000 µs).
    • Why: TinyGo's ESP32 sleepTicks goes back to sleep after an interrupt. Its cortex-m version returns, so the H7 wakes in 4 µs. This comes from reading TinyGo's source, and the measurement fits it.
  • Busy worker. The busy worker yields every 4,096 iterations. The tick then arrives after 68 µs on the S3, 156 µs on the WROOM and 15 µs on the H7. That's ahead of C (1,386, 1,357 and 663 µs) and Rust, and behind Goose on the H7 (4 µs).

TinyGo's multicore scheduler (tinygo-cores, S3). -scheduler=cores runs goroutines on both cores:

  • Scaling. 2 workers take 0.51 times as long as one, against C's 0.52.
  • Timer wake-up. The idle timer wakes in 13 µs.
  • timer_busy gets no tick at all. All 1,010 receives time out, in 5 boots of 5. An instrumented build stopped printing altogether. The likely cause, from reading the source:

    • the scheduler's lock is a spinlock taken with interrupts enabled;
    • an interrupt handler's channel send takes the same lock;
    • so an interrupt that lands while its own core holds the lock spins forever.

    This is a hypothesis, not confirmed with a debugger.

  • Channels across cores. queue_2000 takes 11,989 µs, against 4,908 on one core.

Problems the H7 run ran into. These are TinyGo's, and the board code works around them (board_h7.go):

  • The RTT console was corrupted. TinyGo's RTT control block is in cached memory, and the CPU's write offset shares a cache line with the debugger's read offset. An MPU region makes the block uncached.
  • The runtime's clock sometimes ran 31 times too fast. The timer that keeps it (TIM3) lost its prescaler write in some boots: 3 of 5 in the builds that showed it.
    • Why: TinyGo writes the prescaler right after enabling the timer's clock, a write the H7 can lose; ST's HAL reads the register back first.
    • What the board code does: it reruns TinyGo's timer setup when the prescaler reads 0, and @meta tim3_psc records what each boot found. No boot of the published run needed it. The runs that hit it are in results/dev.

Memory and size.

  • Image: smaller than C's on the ESP32s (83 KB against 187 KB on the S3), larger on the H7 (83 KB against 22 KB).
  • Static RAM: about the same as C's (78 KB against 78 KB on the S3). The channels and goroutine stacks are on the heap.
  • Free heap after a collection (first boot):

    • The H7 and tinygo-cores: it stays within 210 bytes of its start through the run.
    • The WROOM: it is 8 KB lower after timer_busy (95,180 against 103,468 bytes).
    • The S3: it is 16.7 KB lower after timer_idle and 8.4 KB lower after timer_busy (287,911 and 296,199 against 304,631).

    These are multiples of the 8 KB goroutine stack. What holds them wasn't investigated.

MicroPython against C

MicroPython runs the suite as Python: bytecode, asyncio tasks and a garbage-collected heap, on MicroPython 1.29.0 built for each board. Methodology lists how it differs from C. Each board has 5 boots at ea0bed6 for the bytecode build (micropython) and for the native-code one (micropython-native). Every result matches C's, except the scaling checks on the ESP32s (below).

Single precision on the ESP32s. MicroPython's ESP32 builds have only f32 floats. scale_* come out at 220,022 iterations against C's 220,079, and the report flags them. mandel_f64 happens to give C's 59,272. Either way, the ESP32s' mandel_f64 isn't the same computation as C's, whose doubles are software ones there: its ratio (13 on the S3) is f32 in MicroPython against software f64 in C. The H7's build has double precision in the FPU, so its floats are best set against c-fpu.

The WROOM runs at 160 MHz, the default of MicroPython's ESP32_GENERIC build, as TinyGo's is. The S3 runs at 240 MHz.

Compute. Two to three orders of magnitude behind C:

MicroPython ÷ C S3 WROOM (160 MHz) H7 H7 ÷ c-fpu
crc32 520 809 366 —
heapsort 245 394 293 —
mandel_f32 305 474 13 185
matmul_f32 543 579 36 315
mandel_f64 13 (f32 against software f64) 22 (the same) 9.1 98
  • Big integers. Python's integers have no fixed width. MicroPython keeps those under 2^30 in the object pointer, and allocates larger ones on the heap. The CRC register and half of the sort's values are larger, so crc32 and heapsort allocate as they go (from the source; not profiled).
  • Floats. Every float result is a new heap object, so the floating-point loops allocate at every operation too.

The native emitter (micropython-native) gains little: 1.1 to 1.4 times, and nothing on the WROOM's heapsort:

bytecode ÷ native S3 WROOM H7
crc32 1.24 1.26 1.19
heapsort 1.43 0.98 1.30
mandel_f64 1.14 1.13 1.23
matmul_f32 1.16 0.68 (see below) 1.25

Native code skips the bytecode dispatch, but each operation on an object still calls the runtime, and big integers and floats are still allocated (hypothesis, not profiled). @micropython.viper, which works on machine integers, isn't measured.

Tasks and queues.

MicroPython ÷ C S3 WROOM H7
spawn_wait 2.31 (187 vs 81 µs) 9.96 (976 vs 98 µs) 0.83 (78 vs 94 µs)
pingpong 69 120 82
queue_2000 80 192 54
  • Starting a task. Creating and awaiting an asyncio task is cheaper on the H7 than creating and deleting a FreeRTOS task.
  • The WROOM is behind the S3 by more than its clock: 5.2 times on spawn_wait and 3.3 times on queue_2000, against 1.5 for the clock. Its heap is 167 KB, against 8.3 MB on the S3. queue_2000 holds 2000 messages at once, about 64 KB, so the collector would run during it and mark them (hypothesis, not profiled).
  • pingpong slows down until a collection. On the H7 the round trip grows from 183 µs to 1,113 µs over the first boot's 1,000 rounds, and the median is 659 µs.
    • Why: every await of a coroutine allocates a generator object of more than one 16-byte block. MicroPython's allocator starts its search for a multi-block object at the first free block, and moves that start only after single-block allocations (py/gc.c). So each allocation scans past the garbage left since the last collection.
    • Evidence: the same loop on MicroPython's Unix port grows the same way, and a gc.collect() every 300 rounds keeps it flat.

Timer wake-up.

  • Idle. 169 µs on the H7, where the callback is a hard interrupt. On the ESP32s it's 497 µs (S3) and 1,349 µs (WROOM), measured from the soft callback, so the time from the interrupt to the callback isn't in them. The ESP32s' tick gaps vary by up to ±0.9 ms (S3 timer_idle_gap min 58, max 2,161 µs), which is the soft callback's timing, not the timer's.
  • Busy worker. It yields every 4,096 iterations. In MicroPython those take about 9 ms on the H7, 28 ms on the S3 and 56 ms on the WROOM (from the gaps below). On the H7 the tick waits for half that at the median, 4,445 µs. On the ESP32s the medians are 8,048 and 7,884 µs, because the tick buffer caps the wait (next item).
  • Ticks dropped on the ESP32s. The callback keeps the last 16 ticks, as the other builds' tick channel holds 16. A 28 ms or 56 ms wait overflows it, and the 16 ticks kept wait 0 to 16 ms (max 15,967 and 16,034 µs). So every 17th gap is a hole: 12 ms on the S3 and 40 ms on the WROOM (timer_busy_gap p99). On the H7 the 9 ms wait fits. timer_busy's check counts only receives that timed out, so it is 0.

Multicore. None: asyncio runs every task on one core, so scale_2w and scale_4w take as long as scale_1w on every board.

Garbage collection. A probe (not in the suite) timed it:

  • H7: a collection takes 1.1 ms on the full 494 KB heap and 0.95 ms on an empty one. The longest pause in a loop that allocates floats is the same 1.1 ms.
  • S3: the longest pause in that loop over 900,000 iterations is 644 µs, on the 8.3 MB heap in PSRAM.

Results depend on what else is alive. Each published build is consistent from boot to boot: each compute benchmark's boot medians are within 0.5% of each other, except matmul_f32 on the WROOM. Between builds, though, they move:

  • An A/B test on the S3, with firmware that runs only the compute benchmarks: deleting the CRC buffer and the sort array once they're used (two del lines) makes mandel_f64 1.74 s instead of 2.06 s, and matmul_f32 483 ms instead of 613 ms. crc32, which runs before them, doesn't change. Each variant gave the same numbers on two builds.
  • The trial runs (results/dev/*-micropython-*-trial) had those del lines. Their mandel_f64 was 1.79 s on the S3 and 2.98 s on the WROOM, against 2.21 s and 3.69 s published. On the H7 the difference was 0.5%.
  • matmul_f32 on the WROOM has two speeds, chosen per boot: 829 ms in four bytecode boots and 1,407 ms in the fifth; 661 ms in two native boots and 1,229 ms in the other three. So its native median is slower than the bytecode one, which is the luck of the boots, not the emitter.

    The published suite keeps the arrays until compute() returns, as ordinary code would. What makes live arrays cost 6–27% in later code on the ESP32s, and not on the H7, wasn't found.

Memory and size.

  • Image. 321 KB on the H7, 1.73 MB on the ESP32s, where MicroPython's default build includes WiFi and Bluetooth (CONFIG_BT_ENABLED).
  • Static RAM. 53 KB on the S3 and 58 KB on the WROOM. On the H7 it's 280 KB, of which 256 KB are the Ethernet DMA buffers MicroPython's stm32 port reserves in D2 SRAM, unused here.
  • Heap. 494 KB on the H7, 8.3 MB on the S3 (PSRAM), 167 KB on the WROOM. After queue_2000 the free heap is lower than at the start by 8 KB on the H7, 76 KB on the S3 and 10 KB on the WROOM, and back after spawn_wait. The 8 KB is the size of the 2000-slot queue's list. What holds the rest wasn't investigated.

Correctness issues found while measuring

These issues were investigated apart from any performance work: the first four after the baseline, the next six when a performance change or its probe made them show, the next two when the suite first ran on the Pico 2 W, and the last when it first ran on the WROVER-E. The probes are in tests/stress/, and the runs are in bench/results/correctness/. Three were fixed in the H7 port, and two by the runtime's own locks, one on the ESP32s and one on the H7. One has a fix in the ESP32 port that these runs can't confirm. One is in the Goose runtime, and a report for upstream is drafted. One doesn't happen on MicroGoose. Three were fixed in the ESP32 port, and two in the Pico 2 W port.

  1. Contained aborts keep memory (Goose runtime, not fixed here). The runtime keeps a bit for each aborted worker's id and a record holding the abort's message. On the H7 (abort_records.goose):

    Case Heap kept
    200 aborts, each waited with thread_wait(id, why) 0 bytes
    50 aborts, each waited with plain thread_wait(id) 8,536 bytes (about 171 each)
    50 aborts nobody waits for 8,152 bytes (about 163 each)
    One abort after 4,000 / 8,000 / 12,000 / 16,000 spawns 496 / 512 / 1,024 / 0 bytes
    • Messages. Only a wait that asks for the message frees it. A supervisor should wait with thread_wait(id, why).
    • Bits. The bit array is sized by the highest aborted id and never shrinks: one bit for every worker ever started, about 125 KB per million. The steps above are its doubling.
    • Spawning in a burst. thread_spawn_contained can't report that a thread failed to start. 50 spawned in a row without waiting ended the program with cannot create thread.

    The runtime is generated by upstream Goose (v2026.4), so the fix belongs there.

  2. Finished workers' memory came back late on the H7 (fixed). A finished worker's task was deleted only at the next spawn or when the idle task ran, so a burst of workers held their memory after it was waited for. Now thread_wait's wake deletes them too.

    • Memory. In the full suite, 5 boots each, the lowest free heap went from 296,112 bytes (after scale_4w) to 345,520, and the end from 345,036 to 361,584. It starts at 362,924. One finished worker, about 17 KB, can still be held. thread_wait can wake before the worker it waits for has marked itself done, and then that worker is freed at the next wait, spawn or idle run. That happened after queue_2000 in every boot and after spawn_wait in one.
    • Cost. The other 16 benchmarks' medians moved by 0.3% or less. Two moved more. pingpong is timed in whole microseconds, so its mean shows the change better than its median, which went from 16 to 17:

      before after
      queue_2000 median 9,648 µs 9,746 µs (+1.0%)
      pingpong mean 16.39 µs 16.68 µs (+1.8%)

      In runs of the threads group alone, with and without the change, pingpong's mean went from 16.37 to 16.80 µs. Padding the unchanged build with 2, 6 or 12 nops moved it to 16.34, 16.35 and 16.59 µs, so code placement alone accounts for up to 0.25 µs of that.

  3. A WROOM spawn failed in some boots (fix added, not confirmed). It was reproduced in 2 of 12 boots of the suite, in spawn_wait with 130 KB free.

    • Cause. thread_wait returns once a worker is done, but ESP-IDF frees a task that deleted itself only when an idle task next runs. Spawning and waiting 1,000 times (spawn_reclaim.goose), the free heap before a spawn was sometimes 16.5 KB or 33 KB lower: one or two finished workers still held. That left less than a 16 KB data stack plus the 64 KB kept for ESP-IDF, so the spawn was refused.
    • Fix. The port now lets the idle tasks run, for up to five ticks, before it refuses a data stack. That path only runs when the spawn would otherwise have ended the program. The exit line says how many data stacks waited.
    • After. No boot failed in spawn_wait in two runs of 12 boots with the fix. Every boot still stops at scale_2w, the limit above. In the first run, 2 or 3 spawns in 9 of the boots took 400 to 591 µs, where the rest took about 310. In the second, the counter showed that no spawn before scale_2w waited, and no spawn was that slow.
    • What isn't shown. So in these builds the failure didn't happen even where the retry wasn't needed. Each build places code differently and shifts the timing: spawn_wait's median was 306, 310 and 325 µs in the three runs. The retry covers the cause the probe measured, but these runs don't prove that it's what prevented the failure.
  4. A refused data stack doesn't shrink later ones on MicroGoose. The runtime retries a refused region at half the size only above GS_REGION_MIN, 1 MB. MicroGoose's data stacks are 16 KB, so the first refusal ends the program instead (region_shrink.goose). The suspected overflow can't happen.
  5. realloc read past the block it grew on the H7 (fixed). The H7 port builds with GNU Arm Embedded 7-2017-q4-major. Its newlib-nano realloc moves a block to grow it with memcpy(new, old, new_size), so it reads new_size - old_size bytes past the old block's end. newlib fixed this in 4.0.0, with ce4044adeebf, "don't read past source in nano_realloc".
    • Effect. Usually the read only copies whatever follows into the new block's unused tail. But a worker's native stack begins with a 32-byte MPU guard, and the block in front of one can be a block the runtime grows: its bits for aborted workers, or a task's array of thread-local variables. Then the copy faults. It showed up while changing the runtime's locks. In that build, abort_records.goose faulted in every run, in memcpy called from _realloc_r, with the fault address at the guard of the running worker's own stack. Main's layout didn't put those two blocks together, and its runs passed.
    • Fix. The port defines _realloc_r itself: the toolchain's, with newlib's fix. It copies only what the old block holds.
    • Test. realloc_copy.goose grows an 8-byte block, with a 32-byte block just after it, to 128 bytes. Then it checks whether the second block's bytes came across. On main, on the H7, they did. With the fix, only the old block's bytes do. The S3 and the WROOM, whose realloc is ESP-IDF's, pass without it.
    • Cost. In the full suite, 5 boots, every median was within 0.17% of main's, and every check matched. Conformance (22 samples) and the other stress programs give main's results. In queue_2000, the race in item 2 held one finished worker (16.5 KB) after 3 of 5 boots, against 1 of 5 for main. One boot ended queue_2000 with 12 bytes less free, which these runs don't explain.
    • Where a program is built from matters to abort_records. These runs were built in git worktrees, main's and the fix's alike. There, the 50 unwaited aborts keep 9,552 bytes and the 50 plain waits 9,944: 28 bytes more per abort than item 1's table, which was built in the main checkout. Each kept message ends with the source file's absolute path, and a worktree's path is 29 or 30 characters longer. newlib-nano rounds a block up to 4 bytes, which makes that 28.
  6. Timed queue waits stopped working once the wall clock was set, on the ESP32s (fixed). After ntp_sync sets the wall clock, qget<T>(ms) can't receive a message during its wait.
    • Cause. The runtime gives a timed wait its deadline on the monotonic clock, which counts from boot. ESP-IDF's pthread_cond_timedwait compares it with the wall clock, by which it has long passed, and returns at once without letting go of the queue's mutex. The runtime finds time left on its own clock and waits again, so it spins, holding the mutex, until the deadline.
    • Test. In wall_clock.goose, after the set, main's 500 ms wait for a message sent at 20 ms ran its full 500 ms on both ESP32s, and the message was found afterwards.
    • Fix. It came with the runtime's own locks (Optimizations), whose timed waits use the monotonic clock. With them the message arrives after 20.1 ms on the S3 and 19.5 ms on the WROOM.
  7. Timed waits could end before their deadline on the H7 (fixed). The H7 port's pthread_cond_timedwait turned the time left into 1 ms ticks, and counted ticks from when it started. When a tick came just after it started, the wait returned ETIMEDOUT up to a tick before the deadline. POSIX says it times out only once the deadline has passed.

    • Test. sync_stress.goose's timed waits, built for main with the port's own functions, in three runs (bench/results/probes/20261130-h7-timed-waits/). 51 of 60 waits ended early in each run. 1 ms waits ended after 114 to 999 µs, 3 ms waits after 2,142 to 2,996, and 7 ms waits after 6,092 to 6,996.
    • Effect. The runtime's queue waits check the deadline on their own clock and wait again, so a Goose qget<T>(ms) didn't end early: on main, qget<T>(15) timed out after 15.1 and 15.2 ms. C code that a program calls, and that waits with pthread_cond_timedwait, saw the early timeouts.
    • Fix. It came with the runtime's own locks, whose timed waits end only after the deadline. None of the 60 ended early. The latest ended 1,002 µs after its deadline, on the tick after it.
  8. event_wait could wake a tick late on the H7 (fixed). It showed in the locks' run: in one boot of five, 11 of timer_busy_wake's 1,000 wakes came 997 to 999 µs late (Optimizations). The port's wait for an interrupt had two causes.

    • A gap. event_wait checked for an event, then registered to be woken by the next one. An interrupt in between woke nobody, so the wait slept until the next 1 ms tick. With no worker the wait is a WFI, and an interrupt between the check and the WFI did the same: its handler ran first, and the WFI then slept until the tick.
    • A yield. With a worker alive, a task waits one priority up, so the interrupt that ends its wait switches to it at once. Lowering its priority again yields, and FreeRTOS puts it behind the other tasks of its priority. The next of them can then keep the CPU until the next tick. That happened when the interrupt came before the task blocked, and with two workers it happened on almost every wake.
    • Test. event_wake.goose, with event_wake.h. The header restarts a 1 ms event timer at a chosen offset after SysTick wraps. Both run off the same PLL, so the offset holds. The program sweeps the offset over the millisecond in 2 µs steps, and over its first and last 10 µs in 25 ns steps (with no worker, the first 5 µs in 10 ns steps). At each offset it takes 20 events and counts those it got more than 500 µs after their timestamp, with no worker, one spinning worker and two. Late wakes in the last 20 µs before the tick are counted apart: there the program has its event, and the tick gives the CPU to a worker for a time slice before the program reads the clock. Each column below is the same probe, built with that version of the port (main, gap fixed, both fixed):

      Late wakes main gap fixed both fixed
      No worker (20,000 events) 100 0 0
      One worker (26,000 events) 1,244 766 0
      Two workers (26,000 events) 11,399 10,993 0
      Apart: one worker, just before the tick 1,281 1,018 0
      Apart: two workers, just before the tick 501 501 1,120
      • No worker. Main's came with the event 0.44 to 0.60 µs after the tick, at 16 of the 500 fine offsets.
      • One worker. Main's came 1.47 to 4.98 µs after the tick. With the gap fixed, 3.31 to 5.26 µs after it.
      • Two workers. On main, 4,957 of the 5,000 events in the first half of the millisecond came late, by up to 2,001 µs. Later in the millisecond, a wait until the next tick is under 500 µs and isn't counted.
    • The trace. With the gap fixed, a trace of the port's wait (run) showed the rest. The interrupt came after the task had armed its wait and before its check. So the check found nothing, and ulTaskNotifyTake returned at once. Lowering the priority then switched to the spinner, and the task found its event at the next tick, 998 µs late.

    • Fix. The wait is armed before the check (IRQ_WAIT), so an interrupt in between ends the sleep that follows: the task's notification stays pending, and the WFI is skipped if a handler ran since the arming. The task comes back down from the waiting priority in a critical section, where the yield is dropped unless another waiter is ready above it (unboost). uart_read, can_send and can_receive wait the same way and have the same fix.
    • Just before the tick. With one worker there are no late wakes there now, and with two there are more: 1,120 against 501, with the event 2.1 to 5.0 µs before the tick. A hypothesis, from reading FreeRTOS: on main, the yield after a wake handed the CPU to a worker only until that tick, so those wakes weren't late. Now the task keeps the CPU, and the tick hands it to the next worker for a whole time slice. With one worker, the next in turn at the tick is the task itself.
    • The ESP32s. Their event_wait has no such gap. An event gives a binary semaphore, which stays given, so one that comes between the check and the take ends the take at once. Their waits don't change priority, so there's no yield to drop. With a busy worker, a wake waits for the worker's time slice instead (item 7 in the list below).
    • Cost. Three full suites, 5 boots each, built in the same folder, with only the port's two files changed: main's, the gap fixed alone, and both fixes (main's, gap fixed, both). Every check matched in every boot.
      • timer_busy_wake. None of main's 5,000 wakes came late this time. With the gap fixed alone, 500 of boot 1's 1,000 did, every other one, 998 µs late: the yield above. With both fixes, none did. The wakes on time got slower: the median went from 4 to 5 µs and the mean from 4.23 to 4.96. Main's boot medians were 4, 5, 4, 4 and 4, and with both fixes 5 in every boot. With the gap fixed alone, boots 2 to 5 had 5, 4, 5 and 5. A hypothesis: arming the wait before each check, a critical section and a search of the slots, is on the path from the interrupt to the program.
      • spawn_wait and fault_contained: 29% faster (453 to 320 µs, 478 to 343). That's where memset landed, as measured in Optimizations: at 0x08009ccc on main and 0x08009d3c with the gap fixed alone, 4 bytes past an 8-byte boundary, and at 0x08009da8, on one, with both. It isn't this fix.
      • scale_1w, scale_2w and scale_4w: 0.5%, 1.0% and 1.9% faster, and unchanged with the gap fixed alone. They spawn workers, so memset may account for it too. Not measured.
      • queue_2000: 2.2% slower (4,368 to 4,465 µs). With the gap fixed alone it was 2.9% faster (4,242). It doesn't wait on the port's interrupts, so where code landed is the likely cause. Not measured.
      • clock_1000: 0.4% slower (751 to 754 µs), and unchanged with the gap fixed alone. pingpong's mean went from 10.84 to 11.02 µs (11.12 with the gap fixed alone). Every other median is within 0.11% of main's.
      • Memory. After queue_2000, a finished worker (16.5 KB) was held in all 5 boots, and in none on main or with the gap fixed alone. That's the race in item 2. The locks' run, built from the same code as main, held it in 5 of 5 too. The image grew by 216 bytes of flash (56,668 to 56,884), and static RAM stayed at 53,588.
      • Checks (run).
        • Conformance: 22 of 22 pass in Renode, as on main's port in the same emulator. On the board, the three samples it reached passed, before the ST-Link stopped answering between back-to-back flashes.
        • sync_stress, on the board: it passes. None of the 60 timed waits ended early, and the latest ended 1,001 µs after its deadline. Renode can't run it, on main's port either: the port's clock counts with the DWT, which Renode doesn't model.
  9. delay_ms could return early on the ESP32s (fixed). Section 7's probe, tests/stress/clock_costs.goose, times delay_ms(50) with micros(). On the WROOM it came back after 49,770 µs in one run and 50,316 in another; on the S3, after 50,087 and 50,250 (logs). The ESP32 port's delay_ms was vTaskDelay for that many 1 ms ticks (components/microgoose/mg_hal.c), and FreeRTOS counts the tick already under way as the first, so a wait could be up to a tick short. The H7's delay_ms waits on its cycle counter, and took 50,000 µs.

    • Test. tests/stress/delay_early.goose times 5,700 delay_ms calls of 1, 2, 10 and 50 ms with micros(): started at every point in the tick, back to back as in a loop, and from a worker (runs). Before the fix, 3,696 came back early on the S3 and 3,005 on the WROOM. A 1 ms wait ended after as little as 9 µs on the S3 and 12 on the WROOM, and a 50 ms wait after 49,516 and 49,812. Back to back, all 2,400 were early on the S3 and all but one on the WROOM. From a worker, 98 of 100 waits of 10 ms were, the shortest after 9,036 and 9,035 µs.
    • Fix. delay_ms takes the wait's end from the clock micros() reads, blocks in vTaskDelay for the whole ticks, so other tasks run, and waits out what is left, under a tick, on that clock. None of the 5,700 is early on either board. The shortest 1 ms wait took 1,001 µs on the S3 and 1,000 on the WROOM, and the shortest 50 ms wait 50,001 and 50,000. The longest waits are where they were: the fix only adds to a wait that would have been short. On the main thread a wait could already run up to 0.51 ms over on the S3 and 0.79 ms on the WROOM, before and after. The cause wasn't looked into.
    • Cost. The end of a wait that vTaskDelay would have cut short is spun. The probe measures the time a core isn't idle during a wait, from the cycles each core's idle task ran: 4 to 12 µs a call before. After, 25 to 125 µs with the calls spread over the tick, and 112 to 321 µs back to back, whatever the wait's length. In a loop of delay_ms(1) that is 27% of the wait on the S3 and 14% on the WROOM; in a loop of delay_ms(50), 0.6% and 0.2%. Such a loop also turns a little slower, since a wait no longer ends on the tick before its time: a turn of delay_ms(1) went from 997 to 1,005 µs on the S3 and from 999 to 1,009 on the WROOM.
    • Checks. clock_costs passes on both boards: its delay_ms(50) took 50,005 µs on the S3 and 50,169 on the WROOM. Conformance: all 24 samples that run on the S3 pass, and all 21 on the WROOM. The H7 has its own delay_ms, which the fix doesn't touch, and wasn't run: its ST-Link wasn't answering.
  10. A spawn could fail at its native stack on the WROOM (fixed). Optimizations, section 9 lowered what a data stack must leave free on a WROOM without WiFi, from 64 KB to 8. With that, a spawn could run out at its native stack while finished workers' stacks were still to be freed, and nothing waited for them there, as item 3's fix does for a data stack. The suite ended with cannot create thread in one boot of ten. A pthread_create that fails for lack of memory now waits up to five ticks too. tests/stress/low_heap.goose fails without the wait and passes with it. Section 9 has the runs.
  11. A contained abort could end the program on the Pico 2 W (fixed). The suite's first run there ended in the faults group with a FreeRTOS assertion, event_groups.c:332, instead of catching the fault.
    • Cause. The Pico SDK puts a mutex round malloc when its multicore library is linked (PICO_USE_MALLOC_MUTEX), and takes it before newlib's own lock. FreeRTOS's heap_3 calls malloc with the scheduler suspended: for a new task's stack, among others. If another task had taken the SDK's mutex and been switched out before it let go, the wait for it went to the RP2350 FreeRTOS port's lock wait, an event-group wait with no timeout, which FreeRTOS asserts against while the scheduler is suspended. This comes from reading the SDK and FreeRTOS, not from a trace: which task held the mutex wasn't caught.
    • Fix. The port builds with PICO_USE_MALLOC_MUTEX=0. newlib's own lock round malloc (__malloc_lock in mg_rp2350.c) already lets one caller in at a time on the one core FreeRTOS runs on. The C reference is built the same way.
    • Test. tests/stress/contained_aborts.goose spawns 3,000 contained workers that abort, one after another (runs). Without the fix, each of three runs ended at the assertion before its 1,000th abort, two of them before the 500th. With it, each of three caught 3,000 of 3,000, and the published suite's faults group passed in all 5 boots.
    • Checks. Conformance: all 22 samples that fit in the Pico's RAM pass, as before.
  12. microgoose monitor couldn't restart a running program on the Pico 2 W (fixed). It restarts the board with picotool reboot, over USB. The program went on running and stayed on USB, the restart never came, and the monitor stopped with the Pico at COM9 doesn't answer. So bench/run.py could make only one boot a run there.
    • Cause. The SDK's USB reset interface restarts the chip through its watchdog (watchdog_reboot), set to fire after a short delay. MicroGoose's port feeds the watchdog from the FreeRTOS tick, so the restart never came.
    • Fix. watchdog_enable writes a value to a watchdog scratch register, and watchdog_reboot overwrites it. The port keeps the value it started with and stops feeding once it changes. The C reference feeds its watchdog the same way.
    • Test. microgoose monitor, run after the probe (runs). With the fix it restarted the program twice in a row, and the probe ran again to its end each time. With the port's mg_rp2350.c as it was, it stopped with doesn't answer. The published runs restart the board this way for each boot after the first.
  13. A spawn was refused on the WROVER-E with 4 MB free (fixed). Every boot of the suite there ended at scale_4w with cannot create thread, as built and with --worker-reserve 4K.
    • Cause. A worker's native stack has to be in internal RAM, and what the WROVER-E has left of it holds four 16 KB stacks. At the refused spawn, three held the round's running workers and the fourth a worker of the round before, which had ended and deleted its task. ESP-IDF leaves such a task's stack to an idle task, and both idle tasks were ready and not running: the three workers kept both cores. Items 3 and 10's wait of up to five ticks can't help while that lasts.
    • Fix. On the ESP32s with PSRAM the port creates a worker's thread joinable and joins it as it ends, from a task above the program's priorities, so its stack is freed then and not left to an idle task. There a worker's data stack also keeps out of internal RAM that is broken up. On the WROOM-32 joining each thread cost every spawn, so it frees them as before.
    • Test. The benchmark suite on the WROVER-E. On main (e5c3555) it stopped at scale_4w in 5 boots of 5 (run); with the fix it runs to its end in 5 of 5, as built and with --worker-reserve 4K, and is the board's run in Results.
    • Checks. The stress programs pass on the S3, the WROOM-32 and the WROVER-E as they did before the fix, but for low_heap on the WROVER-E, which is written for a board without PSRAM and fails before and after it, at different places. Conformance in QEMU passes as before. Optimizations, section 16 has the probe, the runs and the costs.

What the baseline points to, largest gap first

  1. The H7's FPU: 8.6 to 14 times on floating-point work. Done: see Optimizations.
  2. Typed queues: 5 to 18 times C. The locks, the message nodes and the waits are done: see Optimizations, sections 2, 3 and 5. queue_2000 now takes 0.80 of C's time on the S3 and 0.57 on the WROOM, and 2.40 times C's on the H7, where most of its nodes still come from malloc.
  3. Spawning on the S3: 18 times C, and 5 times the WROOM's own time. Done: see Optimizations, section 4. spawn_wait was then 2.57 times C on the S3, 2.85 on the WROOM and 1.11 on the H7. Section 9 took it to 1.89, 2.17 and 0.69.
  4. The round trip through queues: 2 to 4.7 times C. After the locks, 2.5 on both ESP32s and 1.4 on the H7. After the message nodes, 1.87 on the S3, 1.76 on the WROOM and 1.12 on the H7. Done: see Optimizations, section 5. pingpong is now 1.67 times C on the S3, 1.19 on the WROOM and 1.12 on the H7. On the S3 the round trip runs at one of two speeds, about 19 or 24 µs, depending on the build, for reasons not found.
  5. Bounds-checked, i64-indexed loops: heapsort about 2 times C, matmul_f32 about 3 times on the ESP32s. Measured, and done for matmul_f32: see Optimizations, section 6. matmul_f32's gap was the shape Goose's C gives an in-order sum, not its indexes. It now takes 1.31 times C on the S3, 1.37 on the WROOM and 1.01 on the H7: C fuses each multiply and add, and Goose doesn't. heapsort still takes 1.93 to 2.15 times C. In C, 64-bit indexes alone take 1.51 to 1.77 times as long, and MicroGoose can't narrow Goose's i64.
  6. The H7's micros(): 2.9 times C's read. Done: see Optimizations, section 7. A read made three 64-bit divisions, in software on this core. The port now keeps the time as seconds, microseconds and cycles, and a read makes one 32-bit division: clock_1000 takes 0.169 of its time, and 0.52 of C's.
  7. Timer wake-ups on the ESP32s, idle and busy. Done: see Optimizations, section 8. timer_every() was an esp_timer, whose task stamped a tick 10 to 11 µs after it was due and then woke the program on the other core. It is a hardware timer now, stamped in its interrupt on the program's core: from due to the program having it, a tick takes 12 µs on the S3 and 14 on the WROOM, against 23 and 28. A worker on the program's core held every tick up by as much as a time slice; event_wait() now moves that worker to the idle core at the first late event. timer_busy_wake went from 574 µs to 10 on the S3, and timer_idle_wake is 1.25 times C's on the S3 and 1.10 on the WROOM.
  8. The WROOM's memory rules: one data-stack size for every stack, and 64 KB held back for ESP-IDF. Done for workers: see Optimizations, section 9. A worker of the suite held 70 KB and wrote 1 KB of it: a 16 KB data stack it never wrote to, and a copy of the program's 37 KB of globals it never used. A worker's copy now holds only the globals it uses, on every board. --worker-reserve gives workers' data stacks a size of their own, and a WROOM without networking leaves 8 KB free, not 64. spawn_wait takes 0.73 of its time on the S3, 0.75 on the WROOM and 0.63 on the H7. With --worker-reserve 4K the WROOM runs the whole suite. As built it still stops at scale_4w, and main's six data stacks are still one size.

Each will be profiled before it's changed, and rerun with the full suite and the conformance tests after.