Optimizations¶
This page covers each change to MicroGoose's performance since
perf-baseline. The changes go one subsystem
at a time, in the order the baseline's
findings
ranked the gaps. Each section gives:
- the cause and the change;
- the full suite before and after, on the boards the change touches;
- everything else that moved, regressions included;
- the checks that were run.
"Before" is main just before the change, measured again on the same board,
because main can change between the baseline and a change. Those runs are
in bench/results/before/. Results shows the latest runs
beside the baseline's, and the report sums up the first
nine sections. Sections 10 to 16 came after it.
Where the changes have taken the benchmarks they aimed at, from the baseline to the latest runs:
1. The H7's FPU¶
Cause. The H7 port was built for software floating point, which is
PlatformIO's default for the board. The C reference built for the FPU
(c-fpu) ran floating-point work 8.6 to 14 times faster than the same C
without it.
Change.
- Build: the H7 port is built for the double-precision FPU
(
-mfpu=fpv5-d16 -mfloat-abi=hard) and links the hard-float newlib and libm. - FreeRTOS: the H7 now uses FreeRTOS's ARM_CM4F port. At a task switch it saves the task's FPU registers, if the task has used them.
-
setjmpandlongjmp: the port has its own pair. The toolchain's newlib (GCC 7.2.1) has two problems for hard float:- its
setjmpsaves r4–r11, sp and lr, but not the FPU's d8–d15, and hard-float code keeps values in those across calls; - its
jmp_bufhas no room for them.
An abort in a contained worker returns through this pair.
- its
-
Fault report: a fault while lazily stacking a task's FPU registers into a stack guard now counts as a native stack overflow, as the other stacking faults do.
Before and after. H7, 5 boots each. Before is
20261130T041848Z-h7-goose-892b62a-main
at 892b62a. After is
20261130T041715Z-h7-goose-caf99aa.
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
751 | 751 | 1.000 | 3.07 |
crc32 |
1,723 | 1,723 | 1.000 | 0.71 |
heapsort |
1,776 | 1,799 | 1.013 | 2.18 |
mandel_f64 |
75,453 | 7,259 | 0.096 | 1.04 |
mandel_f32 |
53,072 | 3,391 | 0.064 | 0.92 |
matmul_f32 |
5,724 | 970 | 0.169 | 1.60 |
queue_2000 |
9,743 | 9,814 | 1.007 | 5.33 |
pingpong |
17 | 16 | 0.941 | 2.00 |
spawn_wait |
456 | 323 | 0.708 | 3.44 |
scale_main |
279,898 | 26,479 | 0.095 | 1.04 |
scale_1w |
280,251 | 26,813 | 0.096 | 1.05 |
scale_2w |
280,822 | 27,150 | 0.097 | 1.05 |
scale_4w |
281,764 | 27,808 | 0.099 | 1.07 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
4 | 4 | 1.000 | 0.01 |
fault_contained |
479 | 346 | 0.722 | — |
Floating point. Floating-point work runs 5.9 to 15.6 times faster.
Mandelbrot in f64 is 1.04 times c-fpu, and in f32 it is 0.92 times
c-fpu, so it's faster than C.
spawn_wait and fault_contained: 133 µs faster, by accident. Both
spawn a worker and wait for it. Neither runs any floating point. Main
before the change took as long as the baseline (456 and 479 µs). What
was measured:
- The code is the same. Every function on the spawn path has the same
size in both builds: the C library's
memset,memcpyand allocator, FreeRTOS's task creation, and the port'spthread_createand worker start. The one exception is the context switch. - Its place in memory isn't. newlib-nano's
memsetis a loop that writes one byte at a time. In the old build it starts 4 bytes past an 8-byte boundary, so its 32-bit store instruction straddles one. In the new build it starts on a boundary. - That placement alone costs a third. The same loop, copied in assembly to both placements and timed with the cycle counter over 16 KB, took 2.0 cycles a byte on the boundary and 3.0 cycles a byte off it, in 6 of 6 runs.
- A spawn uses
memseta lot. One spawn and wait writes 33,100 bytes with it (counted by wrapping it at link time), including the 16 KB FreeRTOS fills each new task's stack with. At one cycle a byte that is about 83 µs of the 133. The rest isn't explained.
So this gain comes from where the linker placed memset, not from the
FPU, and another change in layout could take it away. It also points at
the spawning work (item 3): every spawn fills 33 KB one byte at a time.
Other movements, all smaller than 2%:
heapsort: 1.3% slower in every boot (1,776 against 1,799 µs). It runs no floating point. Code placement, which movedcrc32by 12% between two C builds of the baseline, would explain it. That's a hypothesis.timer_idle_wake's mean: 0.80 to 0.95 µs. The median stayed at 1 µs. 0.15 µs is about 60 cycles. That fits the FPU registers now being saved when switching to a task that has used them, as the program's task has. Also a hypothesis.queue_2000: 0.7% slower;pingpong's mean 1.3% faster.
Checks.
- Results: every benchmark's result matches C's, in all 5 boots.
- Conformance:
tests/conformance.py --target h7passes 22 of 22, as before. - Stress programs:
abort_records,spawn_reclaimandregion_shrinkgive the same results as before.abort_records's memory costs match to the byte (8,536 and 8,152). - FPU registers (
tests/stress/fpu_registers.goose, new):- The new pair: it keeps all of d8–d15. Built with newlib's pair instead, the same check reports all eight changed.
- Task switches: two tasks computing a Mandelbrot set in
f64, switched at every 1 ms tick, both get the result one task gets alone, in 10 of 10 rounds. - Aborts: after each of 50 contained aborts in a worker computing in floats, the next worker's result is right.
- Without the fix: with newlib's pair, the contained aborts still
came out right. No frame above the runtime's
setjmpkeeps a float in d8–d15 today, so nothing that runs now depended on the new pair.
- Overflows: both are still reported as such:
- a data stack overflow (
examples/overflow.goose); - a native call stack overflow, from a recursion with a float in every frame.
- a data stack overflow (
- Size: the image went from 60,372 to 56,588 bytes of flash, because
the software floating-point routines are gone. The routines that
convert 64-bit integers to and from
f64stay, since the FPU has no instruction for that; C links the same ones.
What it leaves.
matmul_f32is now 1.60 timesc-fpu. Software floating point had hidden that: the baseline had it at 1.07 timesc. The ESP32s run it at 3.0 and 3.5 times C. The baseline's findings put that, as a hypothesis, down to bounds checks andi64indexes (item 5).- Stale H7 builds. The H7 build doesn't rebuild when a header given
with
-includechanges (mg_prefix.hormg_baremetal.h), because PlatformIO's dependency scanner doesn't see those headers. This change alters the compiler flags, which rebuilds everything, so it isn't affected. It is a separate defect.
2. Queues: the runtime's locks¶
Cause. A message through a queue allocates a node and copies the
value into it, takes the queue's mutex and gives it back twice, signals
its condition variable once, and frees the node. The new
tests/stress/queue_costs.goose times each of those on main, in two
rounds of 2,000. The ranges below cover both rounds, in nanoseconds:
| main | S3 | WROOM | H7 |
|---|---|---|---|
| a mutex taken and given back | 4,401–4,410 | 5,412–5,438 | 867–871 |
| a signal with nobody waiting | 5,954–5,970 | 7,208–7,228 | 133–145 |
malloc and free, 32 bytes |
5,230–6,200 | 8,771–9,948 | 989–1,046 |
qput, on one thread |
16,009–16,016 | 19,735–19,754 | 2,530–2,686 |
qget, on one thread |
7,705–7,706 | 9,441–9,459 | 2,088–2,120 |
| a message from a worker to main | 22,372–22,448 | 45,153–45,402 | 4,714–4,725 |
- The ESP32s. The runtime used ESP-IDF's pthreads. A pthread mutex there is a FreeRTOS mutex. A signal takes a lock of its own, even when nobody waits. Two mutex round trips and a signal add up to 14.8 µs of a message on the S3, and 18.1 µs on the WROOM.
- The H7. The port made a FreeRTOS mutex for each pthread mutex. Taking one and giving it back went through FreeRTOS's semaphore calls. Its condition variables were already a list of waiting tasks under a critical section, so a signal with nobody waiting was cheap. Two mutex round trips and a signal add up to 1.9 µs of a message.
Change. components/microgoose/mg_sync.c implements the runtime's
mutexes and condition variables on FreeRTOS, for all three boards:
- Mutexes. A mutex is its pthread word: free, held, or held with a task that may be waiting. Taking a free one, and giving back one that no task waits for, are one compare-and-swap each.
- Signals. A condition variable's word counts the tasks waiting on it, so a signal with nobody waiting is one load.
- Waiting. A task that has to wait joins one list, kept under a critical section, and sleeps on its task notification until a wake takes it off the list.
- Timed waits take their deadline on
CLOCK_MONOTONIC, the clock the runtime gives them, and never end before it. - No priority inheritance. Every Goose task runs at one priority.
On the ESP32s, core.py makes the runtime include mg_sync.h, which
renames the runtime's pthread calls. The rest of ESP-IDF keeps its own
pthreads. On the H7, the pthread names are aliases of these functions,
in place of the port's.
The same probe on the branch. On the ESP32s, mg_sync's functions are timed beside ESP-IDF's, which take as long as they did on main. On the H7, the pthread names are mg_sync's functions, so both time the same code, and the ranges cover both:
| branch | S3 | WROOM | H7 |
|---|---|---|---|
| a mutex taken and given back | 382–392 | 453–469 | 181–183 |
| a signal with nobody waiting | 90–92 | 109–116 | 45–54 |
qput, on one thread |
6,105–6,109 | 7,688–7,689 | 1,289–1,367 |
qget, on one thread |
3,538–3,539 | 4,290–4,301 | 830–870 |
| a message from a worker to main | 4,257–4,272 | 12,664–13,018 | 2,066–2,072 |
Before and after. 5 boots each, built from git worktrees with paths of the same length (see the checks).
- S3. Before:
20261130T064459Z-s3-goose-40e2958-main
at
40e2958. After: 20261130T070403Z-s3-goose-b25e5ac. - WROOM. Before:
20261130T065811Z-esp32-goose-40e2958-main.
After:
20261130T070055Z-esp32-goose-b25e5ac.
As in every WROOM run, each boot stops at
scale_2w, for lack of memory (findings item 3). - H7. Before:
20261130T134345Z-h7-goose-52b6bb6-main
at
52b6bb6: main with the realloc fix (findings item 5), which this change is built on. After: 20261130T134428Z-h7-goose-b25e5ac. b25e5ac, where every board's "after" was built, holds the same code as this change's commit,35b265e. The two differ only in results and docs.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,815 | 1.000 | 1.99 |
mandel_f64 |
166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
3,874 | 3,874 | 1.000 | 3.00 |
queue_2000 |
44,954 | 8,417 | 0.187 | 2.44 |
pingpong |
71 | 38 | 0.535 | 2.53 |
spawn_wait |
1,476 | 1,422 | 0.963 | 17.56 |
scale_1w |
629,107 | 628,982 | 1.000 | 1.03 |
scale_2w |
326,990 | 326,876 | 1.000 | 1.04 |
scale_4w |
330,718 | 330,695 | 1.000 | 1.05 |
scale_main |
631,151 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 13 | 1.000 | 1.62 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
1,180 | 536 | 0.454 | 0.39 |
fault_contained |
1,635 | 1,594.5 | 0.975 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,614 | 1.000 | 1.96 |
mandel_f64 |
171,323 | 171,323 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,432 | 1.000 | 1.08 |
matmul_f32 |
4,976 | 4,976 | 1.000 | 3.47 |
queue_2000 |
85,535 | 25,730 | 0.301 | 5.44 |
pingpong |
96 | 52 | 0.542 | 2.48 |
spawn_wait |
334 | 262 | 0.784 | 2.67 |
scale_1w |
653,213 | 653,107 | 1.000 | 1.04 |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
751 | 751 | 1.000 | 3.07 |
crc32 |
1,723 | 1,723 | 1.000 | 0.71 |
heapsort |
1,799 | 1,799 | 1.000 | 2.18 |
mandel_f64 |
7,259 | 7,258 | 1.000 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
970 | 969 | 0.999 | 1.60 |
queue_2000 |
9,804 | 4,369 | 0.446 | 2.37 |
pingpong |
16 | 11 | 0.688 | 1.38 |
spawn_wait |
323 | 454 | 1.406 | 4.83 |
scale_1w |
26,813 | 26,941 | 1.005 | 1.05 |
scale_2w |
27,149 | 27,406 | 1.009 | 1.06 |
scale_4w |
27,806 | 28,320 | 1.018 | 1.09 |
scale_main |
26,479 | 26,478 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
4 | 4 | 1.000 | 0.01 |
fault_contained |
347 | 477 | 1.375 | — |
Against the other implementations. Medians in µs. Goose is after this change; the others are their latest runs, in Results:
| Goose | C | Rust | TinyGo | MicroPython | |
|---|---|---|---|---|---|
S3 queue_2000 |
8,417 | 3,456 | 1,963 | 4,909 | 275,329 |
S3 pingpong |
38 | 15 | 6 | 10 | 1,029 |
WROOM queue_2000 |
25,730 | 4,726 | 2,297 | 8,378 | 905,506 |
WROOM pingpong |
52 | 21 | 7 | 17 | 2,526 |
H7 queue_2000 |
4,369 | 1,841 | 556 | 1,372 | 99,881 |
H7 pingpong |
11 | 8 | 2 | 3 | 659 |
H7's C is c-fpu.
spawn_wait and fault_contained on the H7: 40% slower, from where
memset landed. spawn_wait went from 323 to 454 µs, and
fault_contained from 347 to 477, in every boot. Section 1 found the cause
of the opposite move. newlib-nano's memset writes a byte at a time, at
2.0 cycles a byte when it starts on an 8-byte boundary and 3.0 when it
starts 4 bytes past one. A spawn and wait writes 33,100 bytes with it. On
main, memset starts on a boundary (0x08009ca0). With this change it
starts 4 bytes past one (0x08009ccc).
-
The test. The port was given its own
memset: the same seven instructions as newlib-nano's, in assembly. It was built once on a boundary and once 4 bytes past one, with nothing else changed. Each run keeps its patch asexperiment.patch. Both images match the hashes in their runs'env.json, withmemsetat0x08005d80and0x08005d84: on a boundary and 4 bytes past, 5 boots each:H7, medians (µs) main this change memseton a boundary4 bytes past spawn_wait323 454 319 455 fault_contained347 477 342 477 scale_1w26,813 26,941 26,806 26,942 scale_2w27,149 27,406 27,135 27,408 scale_4w27,806 28,320 27,777 28,325 -
So the locks didn't slow spawning. With
memseton a boundary,spawn_waitandfault_containedtake 4 and 5 µs less than on main. - The
scaleruns too.scale_1w,scale_2wandscale_4wstart 1, 2 and 4 workers. With this change they lost 128, 257 and 514 µs: about 130 µs a worker, asspawn_waitdid. Withmemseton a boundary they're within 0.1% of main. - Not fixed here. This change leaves
memsetwhere the linker puts it, and any change can move it again, as section 1 and this one show. Amemsetthat doesn't depend on where it lands is a change of its own (see "What it leaves").
Other movements.
- Spawning got faster on the ESP32s.
spawn_waitwent from 1,476 to 1,422 µs on the S3, and from 334 to 262 µs on the WROOM.fault_containedwent from 1,635 to 1,594.5 µs on the S3. All three moved in every boot. Spawning takes the runtime's thread mutex, andthread_waitwaits on its condition variable, so the cheaper locks would explain it. That's a hypothesis: nothing timed those parts alone. timer_busy_wakeon the S3: no change. In the full suite its median went from 1,180 to 536 µs. But its boot medians after the change spread from 200 to 854 µs, and atperf-baselineit was 147. Run alone (--only timers, two runs of 5 boots for each build, inbench/results/dev/), it was 1,053 µs before in both runs, and 1,072.5 and 1,073 after. Every one of those runs spread from about 550 to 1,550 µs. So the suite's number depends on what ran before it, and alone the branch wakes 20 µs (1.9%) later. Why isn't measured.- Free heap at the start went up by 1,640 bytes on the S3 and 1,700 on the WROOM. ESP-IDF's pthreads allocate memory for a mutex or a condition variable on its first use, and mg_sync allocates none. That would account for it, but the bytes weren't counted.
- Size: the S3's image went from 251,968 to 251,904 bytes, and its
static RAM from 59,792 to 59,824. The WROOM's went from 232,224 to
232,256 bytes, and 57,220 to 57,228. ESP-IDF's pthreads are still
linked: workers are still started with
pthread_create. - Free heap on the H7. At the start it went down by 68 bytes
(362,856 against 362,924) in every boot. Why isn't measured. From
pingpongon it was 532 bytes higher than main's, then 632 fromscale_1wand 820 fromtimer_busy. Main's port made a FreeRTOS mutex on a pthread mutex's first use, and a list on a condition variable's, and mg_sync makes neither. That would account for the rise, but the bytes weren't counted. - A finished worker held after
queue_2000on the H7, in 5 of 5 boots (main: 4 of 5), about 16.5 KB. It's the race in findings item 2:thread_waitcan wake before the worker it waits for has marked itself done, and then that worker is freed at the next spawn. timer_busy_wakeon the H7: 11 wakes a tick late. The median stayed at 4 µs, and the mean went from 4.0 to 6.6 µs: in one boot of five, 11 of the 1,000 wakes came 997 to 999 µs late. None came late in the 10 boots of thememsettest, which have the same locks. None came late in the 41 boots of H7 runs before this change either. A hypothesis, from reading the port:event_waitchecks for an event, then registers to be woken by the next one. An event that comes in between wakes nobody, so the wait sleeps until the next 1 ms tick. That would be a defect in the port'sevent_wait, apart from the locks. It isn't confirmed here. It was afterwards, with a second cause, and fixed in the port: findings, item 8.- Size on the H7: the image went from 56,612 to 56,660 bytes of flash, and static RAM from 53,580 to 53,588.
Checks.
- Results: every benchmark's result matches C's, in every boot on every board.
- Conformance:
tests/conformance.pypasses 24 of 24 on the S3, 21 of 21 on the WROOM, and 22 of 22 on the H7. -
Locks under load (
tests/stress/sync_stress.goose, new):S3 WROOM H7 mutex: 5 tasks, 100,000 increments, count exact yes yes yes unlocks that woke a waiter 33,364 21,082 12,285 2 tasks taking turns, 4,000 turns, woken out of turn 0 0 0 3 tasks taking turns, 6,000 turns, woken out of turn 81 93 3,150 60 timed waits: early / latest past deadline (µs) 0 / 1,000 0 / 998 0 / 1,002 queues: 9,000 messages, 3 producers, 3 consumers, sum and order right right right qget<T>(1000), message after 20 ms: came after (µs)20,007 19,767 20,039 qget<T>(15), nothing sent: timed out after (µs)15,460 15,741 15,566 With 3 tasks, a broadcast wakes all of them and only one has its turn, so out-of-turn wakes are expected there. With 2, a signal wakes the one whose turn it is. The 15 ms timeout ends on the 1 ms tick after its deadline is reached, and
micros()started up to a tick before it.On the H7, the same program was built for main, with the port's own functions in place of mg_sync's (findings item 7 has the runs). It fails there: 51 of the 60 timed waits end early, by up to 0.9 ms. Its 3 tasks woke out of turn 13 to 17 times in three runs, against 3,150 with this change. A hypothesis, from reading both: mg_sync wakes the task that has waited longest first, and main's port woke the newest first. On one core the woken tasks then run in that order. A task that wakes out of turn and waits again goes behind the task whose turn comes next, so the same order can come back at every turn. With main's order it goes in front, and the next turn ends the pattern. What that costs a program isn't measured.
-
Stress programs, against main built from a path of the same length.
abort_recordskeeps each abort's message, which ends with the source file's absolute path, so its numbers change with the path's length.region_shrinkends as on main on both ESP32s.- WROOM
abort_records: the 50 unwaited aborts keep 25,856 bytes (main 25,852), and the 50 plain waits 26,240 (main 26,228). After 4,000 to 16,000 spawns, one abort costs 32 bytes less than on main, in each of the four rounds. - S3
abort_records: the unwaited aborts keep 42,344 and 42,348 bytes in two runs, against 42,356 in both of main's. The other numbers move in steps of 16,492 bytes, one finished worker's memory, by whether ESP-IDF's idle task has freed that worker when the heap is measured. Main gave the same numbers in both its runs. The branch gave different ones in each, for example 49,476 and −16,492 bytes for the 200 waited aborts, against main's −16,492. spawn_reclaim: the lowest free heap, in steps of the same 16.5 KB, was 82,772 bytes below the start for main on the S3 (twice), and 82,564 and 99,056 for the branch. On the WROOM it was 66,280 for main and 33,088 for the branch. These are finished workers ESP-IDF hadn't freed yet (findings item 3).- H7:
abort_recordsandregion_shrinkgive main's results, to the byte.spawn_reclaimspawns and waits for 1,000 workers. Its lowest free heap was 120 bytes below the start for main, in three runs, and 16,716 bytes below for the branch, in three runs. With the branch, from the 131st or 202nd spawn on, a finished worker was sometimes still held at the next spawn. That's the race in findings item 2, which the branch apparently hits more often. The next spawn frees it, so they don't pile up: each run ended 24 bytes below the start.
- Timed waits after the wall clock is set
(
tests/stress/wall_clock.goose, new): see below.
A correctness fix that came with it: timed waits after the wall clock
is set (ESP32s). ntp_sync sets the wall clock. After that, a timed
queue wait (qget<T>(ms)) on main can't receive anything.
- Cause. The runtime gives a timed wait a deadline on the monotonic
clock, which counts from boot. ESP-IDF's
pthread_cond_timedwaitcompares it with the wall clock instead, by which it has long passed, so it returns at once without letting go of the queue's mutex. The runtime checks the deadline on its own clock, finds time left, and waits again. So it spins, holding the mutex, until its deadline, and no message can arrive. - Test.
wall_clock.gooseruns two waits before and after setting the wall clock: a worker sends a message after 20 ms while main waits up to 500 ms for it, then main waits 50 ms for a message nobody sends. After the set, on main, the message didn't arrive during the wait on either ESP32: the wait ran its full 500 ms, and the message was found afterwards. On the branch it arrived after 20.1 ms on the S3 and 19.5 ms on the WROOM, and the 50 ms waits took 50.5 and 50.7 ms. Before the set, both builds were right. The H7 has no wall clock to set, and the test passes there with this change. - mg_sync's timed waits use the monotonic clock, so the same change fixes it. The findings list it as item 6.
And one on the H7: timed waits that ended early. Main's port counted
a timed wait's 1 ms ticks from when the wait started, so it could time
out up to a tick before its deadline. In sync_stress, built for main
with the port's functions, 51 of 60 timed waits did, by up to 0.9 ms.
The runtime's queue waits check the time again and wait on, so Goose's
qget<T>(ms) wasn't affected. C code that waits directly was. With
mg_sync none ended early. The findings list it as item 7.
What it leaves.
- The allocator. With the locks this cheap, a 32-byte
mallocandfreetake 6 µs on the S3, 9 to 10 µs on the WROOM and 1.0 µs on the H7, againstqput's 6.1, 7.7 and 1.3 µs. Every message allocates a node, andqget's caller frees it. That's the next change. Done: section 3. memseton the H7. newlib-nano's writes a byte at a time, and where the linker puts it decides about 130 µs of every spawn: gained by accident in section 1, lost here. Amemsetthat neither depends on its placement nor writes a byte at a time belongs to the spawning work (item 3 in the findings' list of gaps), measured as a change of its own. Done: section 4.- The H7's
event_waitmay wake a tick late, as intimer_busy_wakeabove. That's for the port, to confirm and fix apart from this change. Done: findings, item 8. - A message on the WROOM takes 12.7 to 13.0 µs from a worker to main,
about as long as
qputandqgeton one thread together (12.0 µs). On the S3 it takes 4.3 µs, less than half of that. On both boards the probe reports the worker on core 0 and main on core 1, so their work could overlap. On the S3 it apparently does, and on the WROOM it apparently doesn't. Why isn't measured. With queue nodes from a cache it takes 3.0 to 3.2 µs (section 3).
3. Queues: the message nodes¶
Cause. Each message through a queue is a node: qput mallocs it and
copies the value in, and qget's caller copies the value out and frees it.
The new tests/stress/alloc_costs.goose times what a 32-byte malloc and
free are made of on main (c11151a). It runs four rounds of 2,000: two
alone, and two with a worker waiting, as a queue's producer would be. The
ranges below cover all four, in nanoseconds:
| main | S3 | WROOM | H7 |
|---|---|---|---|
malloc and free, one at a time |
6,178–6,180 | 9,924–9,929 | 994–1,017 |
one malloc of 500 in a row |
4,906–4,955 | 6,532–6,542 | 498–542 |
then one free of 500 |
2,794–2,797 | 3,425–3,436 | 489–505 |
heap_caps_malloc and heap_caps_free, internal RAM |
5,889–5,891 | 9,621–9,624 | — |
multi_heap_malloc and multi_heap_free, on a heap of the probe's own |
2,887–2,917 | 3,515–3,570 | — |
| the allocator's lock, taken and given back | 761–763 | 923–925 | 425–451 |
| this core's interrupts masked and unmasked | 129–132 | 125–127 | 75 |
| a compare-and-swap that succeeds, and one back | 263–265 | 305–311 | 113 |
- The ESP32s. A small
mallocgoes toheap_caps_mallocfor internal RAM. Withheap_caps_freethat takes 5.9 µs on the S3 and 9.6 on the WROOM. The same allocator (ESP-IDF'smulti_heap) on a heap of the probe's own takes 2.9 and 3.5 µs. From ESP-IDF's source:heap_caps_malloclooks through the registered heaps for one with the memory asked for, andheap_caps_freelooks for the heap that holds the block. That search would be the difference, but it wasn't timed alone. Each heap has a lock, which takes 0.76 µs to take and give back on the S3 and 0.92 on the WROOM. - The H7. newlib-nano's
mallocandfreetake 1.0 µs together. The port's lock around them suspends the scheduler and takes 0.43 to 0.45 µs of that.
Change. components/microgoose/mg_qnode.c keeps queue nodes in a
cache, on all three boards:
- Size classes. Nodes up to 128 bytes, header included, come from the
cache, in one size class for each 16 bytes.
queue_2000's 16-byte message makes a 32-byte node. Larger nodes come frommalloc. - Free. A free pushes the node onto its class's shared list with one compare-and-swap, from any core.
- Alloc. An alloc takes a node from its own core's list, with that core's interrupts masked, so no other task runs there in between. When that list is empty, it takes the whole shared list with one compare-and-swap. Nothing ever takes a single node off the shared list. That rules out the ABA problem, where a lock-free list hands out a node that's in use.
- Chunks. When both lists are empty, the cache mallocs a 512-byte chunk
and cuts it into nodes. It takes 8 chunks at most (4 KB) and keeps them
until reset. Past that, nodes come from
mallocas before. The 4 KB limit was chosen before the suite ran, and wasn't changed after. It is about 1.2% of the S3's free internal RAM at boot and 1.7% of the WROOM's (333,491 and 243,696 bytes, fromalloc_costs). - Hooks.
core.pyadds#include "mg_qnode.h"right after the queue declarations, in the runtime and in the generated program, in a board's build with threads. From there on in those files:freeismg_qnode_free. It knows the cache's nodes by their address and passes anything else to the C library.qput'smallocismg_qnode_alloc.- The build stops if the runtime's text isn't where
core.pyexpects it.
On the branch, the same probe times the cache's own alloc and free of a
32-byte node, freed and taken back as a message's node is. That takes
672–698 ns on the S3, 862–892 on the WROOM and 311–312 on the H7: about
a ninth, an eleventh and a third of what malloc and free take on
main.
The first version lost on the H7. The suite with the first version
(e74ea37) took queue_2000 from 8,417 to 4,731 µs on the S3 and from
25,734 to 5,725 on the WROOM. But on the H7 it went from 4,465 to 4,756 µs:
6.5% slower, in every boot.
- Where its nodes came from. The new
tests/stress/qnode_flow.gooserunsqueue_2000's pattern 17 times and counts where each run's nodes came from. On the H7, 1,320 to 1,645 of each run's 2,000 nodes came frommallocbecause the cache was full. With one core, more messages wait at once than the cache's 128 nodes of 32 bytes. - What a node from
malloccost. It paid for the cache's work first. The alloc counted it with a compare-and-swap, and the free compared it with all 8 chunks. -
queue_costskeeps 2,000 messages waiting on one thread, so most of its nodes come frommalloc. In ns:H7 main first version second version qput, on one thread1,281–1,444 1,616–1,733 1,504–1,636 qget, on one thread838–839 997–1,058 839–842 a message from a worker to main 2,027–2,062 2,251–2,449 2,095–2,177 -
The second version (
8fa701f) makes a node frommalloccost little more thanmalloc:- The alloc counts it under the mask it already holds, with no compare-and-swap.
- The free first checks the one address range that holds every chunk, and a node outside it goes straight to the C library. The range's low end only goes down and its high end only up. A free that races with a new chunk can only see a range that's too wide, never one that misses its node's chunk.
The second version is the one measured below. The first version's runs are in
bench/results/dev/.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- S3. Before: 20261130T204336Z-s3-goose-c11151a-main. First version: 20261130T205204Z-s3-goose-e74ea37. After: 20261130T215426Z-s3-goose-8fa701f.
- WROOM. Before:
20261130T205204Z-esp32-goose-c11151a-main.
First version:
20261130T204336Z-esp32-goose-e74ea37.
After:
20261130T215426Z-esp32-goose-8fa701f.
As in every WROOM run, each boot stops at
scale_2wfor lack of memory (findings item 3). - H7. Before: 20261130T210926Z-h7-goose-c11151a-main. First version: 20261130T211148Z-h7-goose-e74ea37. After: 20261130T213202Z-h7-goose-8fa701f.
8fa701f, where every board's "after" was built, holds this change's code. Later commits add only results and docs.
| S3, medians | before (µs) | first version (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|---|
clock_1000 |
855 | 855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,815 | 4,815 | 1.000 | 1.99 |
mandel_f64 |
166,166 | 166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,451 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
3,874 | 3,874 | 3,874 | 1.000 | 3.00 |
queue_2000 |
8,417 | 4,731 | 4,238 | 0.504 | 1.23 |
pingpong |
38 | 28 | 28 | 0.737 | 1.87 |
spawn_wait |
1,422 | 1,449 | 1,430 | 1.006 | 17.65 |
scale_1w |
628,983 | 629,031 | 628,998 | 1.000 | 1.03 |
scale_2w |
326,876 | 326,937 | 326,923 | 1.000 | 1.04 |
scale_4w |
330,617 | 330,648 | 330,664 | 1.000 | 1.05 |
scale_main |
631,150 | 631,150 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 13 | 13 | 1.000 | 1.62 |
timer_busy_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
362 | 65 | 556 | 1.536 | 0.40 |
fault_contained |
1,593 | 1,650 | 1,639.5 | 1.029 | — |
| WROOM, medians | before (µs) | first version (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|---|
clock_1000 |
584 | 584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,614 | 5,614 | 1.000 | 1.96 |
mandel_f64 |
171,323 | 171,323 | 171,323 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,432 | 8,432 | 1.000 | 1.08 |
matmul_f32 |
4,976 | 4,976 | 4,976 | 1.000 | 3.47 |
queue_2000 |
25,734 | 5,725 | 5,721 | 0.222 | 1.21 |
pingpong |
52 | 36 | 37 | 0.712 | 1.76 |
spawn_wait |
262 | 284 | 275 | 1.050 | 2.81 |
scale_1w |
653,076 | 653,133 | 653,112 | 1.000 | 1.04 |
| H7, medians | before (µs) | first version (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|---|
clock_1000 |
754 | 754 | 753 | 0.999 | 3.07 |
crc32 |
1,723 | 1,723 | 1,723 | 1.000 | 0.71 |
heapsort |
1,799 | 1,802 | 1,802 | 1.002 | 2.18 |
mandel_f64 |
7,258 | 7,258 | 7,259 | 1.000 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
970 | 970 | 970 | 1.000 | 1.60 |
queue_2000 |
4,465 | 4,756 | 4,400 | 0.985 | 2.39 |
pingpong |
11 | 9 | 9 | 0.818 | 1.12 |
spawn_wait |
320 | 321 | 320 | 1.000 | 3.40 |
scale_1w |
26,807 | 26,808 | 26,806 | 1.000 | 1.05 |
scale_2w |
27,138 | 27,137 | 27,136 | 1.000 | 1.05 |
scale_4w |
27,780 | 27,781 | 27,780 | 1.000 | 1.07 |
scale_main |
26,479 | 26,479 | 26,479 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
5 | 5 | 4 | 0.800 | 0.01 |
fault_contained |
341 | 345 | 344 | 1.009 | — |
Against the other implementations. Medians in µs. Goose is after this change; the others are their latest runs, in Results:
| Goose | C | Rust | TinyGo | MicroPython | |
|---|---|---|---|---|---|
S3 queue_2000 |
4,238 | 3,456 | 1,963 | 4,909 | 275,329 |
S3 pingpong |
28 | 15 | 6 | 10 | 1,029 |
WROOM queue_2000 |
5,721 | 4,726 | 2,297 | 8,378 | 905,506 |
WROOM pingpong |
37 | 21 | 7 | 17 | 2,526 |
H7 queue_2000 |
4,400 | 1,841 | 556 | 1,372 | 99,881 |
H7 pingpong |
9 | 8 | 2 | 3 | 659 |
H7's C is c-fpu.
- The ESP32s. Goose's
queue_2000is now 1.23 and 1.21 times C's, and less than TinyGo's. It's still 2.2 and 2.5 times Rust's.pingpongis 1.87 and 1.76 times C's, and behind Rust's and TinyGo's. - The H7.
queue_2000is still 2.39 times C's, 3.2 times TinyGo's and 7.9 times Rust's.
Where queue_2000's nodes come from. qnode_flow runs
queue_2000's pattern 17 times after boot, with nothing else before it.
The cache keeps its chunks from one run to the next.
qnode_flow, after this change |
S3 | WROOM | H7 |
|---|---|---|---|
| median of the 17 runs (µs) | 3,655 | 5,656 | 4,367 |
runs with no node from malloc |
12 | 11 | 0 |
| their times (µs) | 3,261–4,433 | 4,736–7,344 | — |
nodes from malloc in the other runs |
100–372 | 8–579 | 1,360–1,616 |
| their times (µs) | 4,715–6,461 | 4,021–9,776 | 4,242–4,710 |
- S3. The first 11 runs needed 5 chunks and took 3,261 to 3,889 µs.
The 12th filled the cache, took 372 nodes from
mallocand 6,461 µs. After that, runs took 0 to 202 nodes frommalloc, in 4,433 to 4,954 µs. - WROOM. The first 11 runs needed 2 to 4 chunks and took 4,736 to
7,344 µs. The 12th and 13th filled the cache, with 258 and 579 nodes
from
malloc, in 7,350 and 9,776 µs. The last four took 8 to 26 nodes frommallocand were the fastest of the 17, at 4,021 to 4,561 µs. Why isn't measured. - H7. Every run filled the cache: 1,360 to 1,616 of its 2,000 nodes
came from
malloc. With one core, more messages wait at once than the cache holds.
Other movements.
-
Spawning on the ESP32s: slower in the suite, not alone. In the suite,
spawn_waittook 8 µs longer on the S3 and 13 µs on the WROOM, and the S3'sfault_contained46.5 µs longer, in every boot. Both come afterqueue_2000in the suite. So they were run again, 5 boots each, with nothing using a queue before them. Forspawn_wait, the threads group ran withspawn_waitfirst.fault_containedran alone (--only faults). The runs are inbench/results/dev/, each with itsexperiment.patch:medians (µs) main this change S3 spawn_wait, in the suite1,422 1,430 S3 spawn_wait, first (main, this change)1,426.5 1,425 S3 fault_contained, in the suite1,593 1,639.5 S3 fault_contained, alone (main, this change)1,590 1,598 WROOM spawn_wait, in the suite262 275 WROOM spawn_wait, first (main, this change)268 269 - Run first,
spawn_waittakes the same time with and without the cache, within 1.5 µs. Alone,fault_containedtook 8 µs (0.5%) longer, in every boot. Why isn't measured. - So most of the suite's difference comes from running after
queue_2000. A hypothesis: the chunks the cache keeps from then on change where a spawn's allocations land in the heap. That wasn't tested. queue_2000moves with the order too. Withspawn_waitfirst, it took 4,443 µs on the S3 and 5,526 on the WROOM, against 4,238 and 5,721 in the suite. Main's took 8,449 and 25,599, against 8,417 and 25,734.
- Run first,
-
The H7's
heapsortandfault_contained.heapsortwent from 1,799 to 1,802 µs (0.2%), andfault_containedfrom 341 to 344 (0.9%), in every boot. Neither uses a queue. The cache's code moves what the linker places after it, which section 2 found can move timings, but that wasn't checked here. - A message from a worker to main (
queue_costs) takes 2.1 to 2.3 µs on the S3, against 4.2 on main, and 3.0 to 3.2 on the WROOM, against 13.0. On the H7 it takes 2.1 to 2.2 µs, against 2.0 to 2.1. -
Many messages waiting: slower.
queue_costsputs 2,000 messages on a queue before it takes any, on one thread. More messages wait than the cache holds, so most nodes come frommalloc, after the cache's check. In ns:queue_costsmain this change S3 qput6,105–6,109 6,381–6,556 S3 qget3,538–3,540 3,760–3,860 WROOM qput7,686–7,687 7,943–8,209 WROOM qget4,292–4,301 4,252–4,384 H7 qput1,281–1,444 1,504–1,636 H7 qget838–839 839–842 That's 3 to 9% more on the ESP32s for
qputand the S3'sqget. A program that keeps more messages waiting than the cache holds pays it. How much of it is the cache's check isn't measured. On the H7,mallocitself is slower on the branch (see "What it leaves"). -
A worker held after
queue_2000on the H7: in 0 of 5 boots (main: 4 of 5), about 16.5 KB. It's the race in findings item 2, which this change doesn't touch. Section 2's runs held one in 5 of 5 boots. timer_busy_wakeon the S3 went from 362 to 556 µs in the suite. Its boot medians spread from 200 to 536 µs on main, and from 36 to 802 after the change. Section 2 found that the suite's number depends on what ran before it.- Free heap.
- At the start, 192 bytes less on the S3, 208 on the WROOM and 92 on the H7.
- After
queue_2000, 4,320 bytes less than main on the S3, 4,336 on the WROOM and 4,248 on the H7 (against main's one boot that held no worker). Less what was missing at the start, that's 4,128, 4,128 and 4,156 bytes: the cache's 8 chunks of 512 bytes, and the allocator's headers. In one WROOM boot the cache took 6 chunks, and 3,096 bytes. - The cache keeps them until reset.
- Size.
- The S3's image went from 251,904 to 252,464 bytes, and its static RAM from 59,824 to 60,016.
- The WROOM's went from 232,256 to 232,896 bytes, and 57,228 to 57,436.
- The H7's went from 57,564 to 58,176 bytes, and 53,588 to 53,744.
Checks.
- Results: every benchmark's result matches C's, in every boot on every board.
- Conformance:
tests/conformance.pypasses 24 of 24 on the S3 and 21 of 21 on the WROOM, on the boards, and 22 of 22 on the H7 in Renode (--emulate). -
The cache under load (
tests/stress/qnode_stress.goose, new):S3 WROOM H7 24,000 messages with nodes of 32, 64, 128 and 136 bytes, 2 producers, a consumer for each size, queues of 32: count, sum and each producer's order right right right main sends itself 1,000 messages before taking any, twice: nodes from malloc968, 968 978, 978 968, 968 200 rounds of 10 blocks from malloc,callocandrealloc, 1 to 600 bytes, freed through the cache'sfree: bytes wrong0 0 0 nodes from the cache / from malloc, cache full / frommalloc, larger than 128 bytes11,554 / 8,898 / 6,093 11,863 / 8,570 / 6,108 12,430 / 7,757 / 6,111 the whole heap's check ( heap_caps_check_integrity_all)passes passes — The bursts' messages all came back intact and in order. The counts include the
qputs that found their queue full, which free their node and try again: that's why more than 6,000 nodes came frommallocfor being too large. -
Locks under load (
sync_stress, from section 2) passes on all three boards. - Stress programs, against main built from a path of the same length:
abort_records: its numbers differ from main's by steps of 16,492 bytes (a finished worker not yet freed) and by 4 to 104 bytes.- On the WROOM, an abort after 4,000 to 16,000 spawns costs 28 to 32 bytes more than on main, in each of the four rounds: about what it costs on the S3 and the H7.
- The 50 unwaited aborts keep 8 more bytes on both ESP32s.
- The 50 plain waits keep 68 more on the WROOM, 4 more on the H7 and 8 fewer on the S3.
- Why isn't measured.
spawn_reclaimends within 4 bytes of where main does, on every board. Its lowest free heap moves in the same 16.5 KB steps on the ESP32s. On the H7 it was 16,716 bytes below the start, against main's 16,696.region_shrinkends with the same error as main on every board. It sends a message before its first measure, and from there it has 668 to 828 bytes less free than main. On the H7 it ends with 512 bytes more in use than main: one chunk. A hypothesis: the cache's first chunk is most of the difference. It wasn't counted byte by byte.
- Unit tests (
tests/test_fs.py,tests/test_fixes.py) check the build's hooks: the include comes before every queue call in the runtime and the program, adding it twice changes nothing, and an unknown runtime stops the build. - Raw data. Every probe, stress and conformance log behind this section is in bench/results/probes/20261130-queue-nodes.
What it leaves.
- The H7's
queue_2000is still 2.39 times C's. Most of its nodes come frommalloc, because one core lets more messages wait than the cache holds. A larger cap would help this benchmark, but the cap was set before the suite ran, and isn't tuned to it. Where the rest of the time goes isn't profiled. - Nodes on one core's list can't be used by the other core. When the
shared list is empty, a core grows the cache or falls back to
malloc, even while the other core holds free nodes. - The chunks stay until reset: up to 4 KB the rest of the program can't use after a burst has passed.
- Many messages waiting cost 3 to 9% more on the ESP32s, as above.
mallocon the H7 is slower on the branch.alloc_coststimes amallocandfreeat 1,062 to 1,089 ns, against 994 to 1,017 on main, and onemallocof 500 in a row at 575 to 616 ns, against 498 to 542. Nothing in the cache runs in those. Why isn't measured.- Spawning after
queue_2000is slower on the ESP32s. Whether the chunks are why wasn't tested.
4. Spawning: a worker's copy of the globals¶
Cause. The new tests/stress/spawn_costs.goose has the suite's
globals: 36,864 bytes, in BUF, A, MA, MB and MC. It spawns an
empty worker and waits for it 200 times, after 5 warm-ups, then times the
parts of a spawn alone. On main (7bd4af2), in µs:
| main | S3 | WROOM | H7 |
|---|---|---|---|
| spawn and wait, median of 200 | 1,402 | 317 | 316 |
calloc and free of 36,864 bytes (PSRAM on the S3) |
1,183.1 | 58.5 | 186.4 |
| the same in internal RAM | 56.5 | — | — |
memset of 36,864 bytes (PSRAM on the S3) |
1,135.5 | 48.7 | 184.6 |
| the same in internal RAM | 48.5 | — | — |
| an empty pthread, started and waited for | 81.8 | 101.6 | 95.5 |
| an empty FreeRTOS task, started and waited for | 68.5 | 92.9 | 88.7 |
- The globals. Each worker's entry, as Goose generates it, starts
with
gs_gl = calloc(1, sizeof(gs_globals_t)): a zeroed copy of every global in the program. The globals the worker's program uses are then copied in from the spawn (Goose's spec, §11.2). So every spawn zeroes at least the arrays' 36,864 bytes, even forspawn_wait's empty worker, which uses none. In the suite's program the whole struct is 37,136 bytes (measured on the WROVER-E, below). The probe times blocks of 36,864. - The S3. ESP-IDF puts a
mallocorcallocover 16 KB in PSRAM (CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384), and the S3's PSRAM runs at 40 MHz. Zeroing 36,864 bytes there takes 1,135.5 µs, about 31 ns a byte. In internal RAM it takes 48.5. - The H7. newlib-nano's
memsetwrites a byte at a time: 184.6 µs for 36,864 bytes. FreeRTOS also fills each new task's stack with it, 16 KB for a worker, because the port setsconfigUSE_TRACE_FACILITY. - The WROOM has no PSRAM, and its
memsetis ESP-IDF's: zeroing the globals takes 48.7 µs of its 317.
An experiment stamped the time at each step of a spawn in the generated
runtime (experiment.patch, not part of the change). Means of 200, in µs,
on main:
| main, with the marks | S3 | WROOM | H7 |
|---|---|---|---|
thread_spawn until pthread_create |
7.6 | 9.8 | 1.7 |
pthread_create until the worker runs |
75.9 | 91.8 | 90.6 |
| the worker's setup, until it reserves its data stack | 10.3 | 35.1 | 9.8 |
its data stack (gs_reserve_region) |
35.8 | 48.0 | 3.8 |
its thread program: the globals' calloc, the empty body |
1,224.8 | 59.3 | 189.1 |
| its release and completion | 34.2 | 34.2 | 9.9 |
until thread_wait returns on main |
6.6 | 10.0 | 11.4 |
| total | 1,395.3 | 288.2 | 316.1 |
In the first table, the globals' calloc and free are 84% of a spawn
and wait on the S3, 59% on the H7 and 18% on the WROOM.
Change.
- The hook.
core.pychanges each worker entry'scallocof the globals intomg_globals_alloc(sizeof(gs_globals_t)), in a board's build. The build stops if a worker allocates its globals any other way. - The ESP32s (
components/microgoose/mg_compat.c). Where PSRAM was found,mg_globals_alloctakes the globals from internal RAM while that leaves 64 KB free, the rule data stacks already follow. A worker reserves its data stack first, so its own stack keeps its place. Otherwise, or with data stacks set to PSRAM (CONFIG_MICROGOOSE_STACKS_IN_PSRAM), it callscallocas before. The WROOM's build has PSRAM support but finds none, so it callscalloc. The first version (5e59547) also asked for internal RAM there, wherecalloctakes it anyway. The second (0ab7aa1) leavescallocto choose where no PSRAM was found. - The H7 (
ports/stm32h7/src/mg_h7.c).mg_globals_allociscalloc, and the port has its ownmemset, which writes 4 bytes at a time, 32 per loop, after up to 3 single bytes to reach a word boundary. It replaces newlib-nano's in the whole image, the stacks FreeRTOS fills included. GCC is told not to turn its loops back into a call tomemset. This is thememsetsection 2 left for this work.
The same probe, after the change (0ab7aa1), in µs:
| after | S3 | WROOM | H7 |
|---|---|---|---|
| spawn and wait, median of 200 | 212 | 291 | 102 |
memset of 36,864 bytes |
1,135.5 (PSRAM), 48.5 (internal) | 48.8 | 39.9 |
| an empty pthread, started and waited for | 81.8 | 101.2 | 29.7 |
| an empty FreeRTOS task, started and waited for | 68.7 | 92.9 | 24.3 |
The probe's own calloc still puts 36,864 bytes in PSRAM on the S3: only
a worker's globals go through mg_globals_alloc.
The WROOM: the same code, and a spread between builds. On the WROOM the change adds a call and the check of a static. Even so, its spawn moved with each build, both ways:
| WROOM, spawn and wait, medians (µs) | main | first version | second version |
|---|---|---|---|
the suite's spawn_wait, 5 boots |
275 | — | 279 |
spawn_costs |
317, 317 | 307 | 290, 291 |
- The suite took 4 µs (1.5%) longer after the change, in every boot.
spawn_coststook 26 to 27 µs less, in each of its two runs.- With no code change. Main built with the probe's first version, which
lacks only its
memsetcheck, took 278 µs, against 317 with the current one. Its empty FreeRTOS task took 81.7 µs against 92.9, and that task's code didn't change.
So on the WROOM, a build that changes code elsewhere in the image moves a spawn by up to 39 µs. A hypothesis: where the code lands changes what the flash cache holds. That wasn't tested. The suite's 4 µs is reported as a loss.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- S3. Before: 20261130T234623Z-s3-goose-7bd4af2-main. After: 20261130T235109Z-s3-goose-0ab7aa1.
- WROOM. Before:
20261130T235109Z-esp32-goose-7bd4af2-main.
After:
20261130T234623Z-esp32-goose-0ab7aa1.
As in every WROOM run, each boot stops at
scale_2wfor lack of memory (findings item 3). - H7. Before: 20261130T234623Z-h7-goose-7bd4af2-main. After: 20261130T234715Z-h7-goose-0ab7aa1.
0ab7aa1, where every board's "after" was built, holds this change's code. Later commits add only results and docs.- The "before" runs list
tests/stress/spawn_costs.gooseandspawn_costs.has untracked files: they were copied in to run the probe on main. The suite doesn't build them.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,815 | 1.000 | 1.99 |
mandel_f64 |
166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
3,874 | 3,874 | 1.000 | 3.00 |
queue_2000 |
4,227 | 3,912 | 0.925 | 1.13 |
pingpong |
28 | 28 | 1.000 | 1.87 |
spawn_wait |
1,429 | 208 | 0.146 | 2.57 |
scale_1w |
628,998 | 627,785 | 0.998 | 1.03 |
scale_2w |
326,918 | 323,031 | 0.988 | 1.03 |
scale_4w |
330,667 | 327,970 | 0.992 | 1.04 |
scale_main |
631,151 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 13 | 1.000 | 1.62 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
356 | 14 | 0.039 | 0.01 |
fault_contained |
1,630.5 | 333 | 0.204 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,614 | 1.000 | 1.96 |
mandel_f64 |
171,324 | 171,323 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,432 | 1.000 | 1.08 |
matmul_f32 |
4,976 | 4,976 | 1.000 | 3.47 |
queue_2000 |
5,815 | 5,629 | 0.968 | 1.19 |
pingpong |
37 | 37 | 1.000 | 1.76 |
spawn_wait |
275 | 279 | 1.015 | 2.85 |
scale_1w |
653,078 | 653,131 | 1.000 | 1.04 |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
753 | 751 | 0.997 | 3.07 |
crc32 |
1,723 | 1,723 | 1.000 | 0.71 |
heapsort |
1,802 | 1,799 | 0.998 | 2.18 |
mandel_f64 |
7,259 | 7,258 | 1.000 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
970 | 969 | 0.999 | 1.60 |
queue_2000 |
4,400 | 4,244 | 0.965 | 2.31 |
pingpong |
9 | 9 | 1.000 | 1.12 |
spawn_wait |
320 | 104 | 0.325 | 1.11 |
scale_1w |
26,806 | 26,580 | 0.992 | 1.04 |
scale_2w |
27,137 | 26,701 | 0.984 | 1.04 |
scale_4w |
27,780 | 26,915 | 0.969 | 1.04 |
scale_main |
26,479 | 26,477 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
5 | 4 | 0.800 | 0.01 |
fault_contained |
344 | 128 | 0.372 | — |
Against the other implementations. spawn_wait, medians in µs.
Goose is after this change; the others are their latest runs, in
Results:
| Goose | C | Rust | TinyGo | MicroPython | |
|---|---|---|---|---|---|
| S3 | 208 | 81 | 4 | 50 | 187 |
| WROOM | 279 | 98 | 4 | 83 | 976 |
| H7 | 104 | 94 | 1 | 18 | 78 |
H7's C is c-fpu, which spawns as fast as c.
- Goose now takes 2.57 times C's time on the S3, 2.85 on the WROOM and 1.11 on the H7. Before this change it took 17.6 times on the S3.
- MicroPython still spawns faster than Goose on the S3 and the H7. On the WROOM it takes 3.5 times Goose's time.
- Rust and TinyGo don't start an RTOS task: Embassy reuses a task slot and a goroutine is TinyGo's own (findings). Goose and C both start one.
Other movements.
- The
scaleruns andfault_containedinclude spawns.scale_1w,scale_2wandscale_4wtime from before the first spawn to after the last wait.- H7. They took 226, 436 and 865 µs less with 1, 2 and 4 workers:
216 to 226 µs a worker.
spawn_waitsaved 216. - S3. They took 1,213, 3,887 and 2,697 µs less. With one worker
that's about what
spawn_waitsaved (1,221 µs). With 2 and 4 workers on two cores, spawns overlap the workers' work, and how much of each gain is the spawns wasn't measured. fault_containedspawns a worker that faults. It took 1,297.5 µs less on the S3 and 216 less on the H7. The WROOM's suite stops before it.
- H7. They took 226, 436 and 865 µs less with 1, 2 and 4 workers:
216 to 226 µs a worker.
queue_2000is timed from after its worker has started, so it doesn't include the spawn.- H7. It went from 4,400 to 4,244 µs (3.5%), in every boot. The new
memsetreplaces newlib-nano's everywhere, and it moves what the linker places after the port's code. Which of those is the cause wasn't checked. - S3. It has two modes. Boots that ended it with 2,176 bytes less
free than at the start took 3,844 to 3,846 µs. Boots that ended it
with 2,692 to 4,240 bytes less took 4,111 to 4,704. The differences
are steps of 516 bytes, one of the queue node cache's chunks
(section 3): the fast boots took 4 chunks, the others 5 or 8. So the
slower boots had more messages waiting at once.
- The fast mode came in 1 boot of 5 on main and 4 of 5 after the change. Boots 2 and 4 of each run printed the same bytes, so that's 1 of 4 and 3 of 4 distinct boots. Why the change shifts the modes isn't measured.
- WROOM. It went from 5,815 to 5,629 µs, but the boot medians overlap (5,633 to 5,950 before, 5,606 to 5,954 after).
- H7. It went from 4,400 to 4,244 µs (3.5%), in every boot. The new
timer_busy_wakeon the S3: no change. In the suite it went from 356 to 14 µs. Its boot medians spread from 159 to 861 µs on main, and from 14 to 265 after the change. Run alone (--only timers, 5 boots each), it took 1,094.5 µs on main and 1,093.5 with this change, with every boot's median between 1,093 and 1,094.5. As section 2 found, the suite's number depends on what ran before it.- The WROOM's slow spawns. In one boot after the change, 2 of 50 spawns took 554 and 583 µs, as in findings item 3. None did on main in this run.
- The H7's other benchmarks.
clock_1000,heapsort,matmul_f32,mandel_f64andscale_maintook 1 to 3 µs less, in every boot. They don't spawn. The newmemsetreplaced newlib-nano's in the whole image and moved the code after it. Whether they callmemset, or the layout moved them, wasn't checked. - Free heap.
- S3: 16 bytes less at the start, which is the 16 bytes more of static RAM.
- H7: the same as main at every point, in every boot.
- WROOM: the same at the start. After
queue_2000, main had 127,464 bytes free in every boot. After the change it had 127,464 to 129,012, which is the cache's chunks again.
- Size.
- The S3's image went from 252,464 to 252,592 bytes, and its static RAM from 60,016 to 60,032.
- The WROOM's went from 232,896 to 233,056 bytes, with the same 57,436 of static RAM.
- The H7's went from 58,176 to 58,304 bytes, with the same 53,744.
Checks.
- Results: every benchmark's result matches C's, in every boot on every board.
- Conformance:
tests/conformance.pypasses 24 of 24 on the S3 and 21 of 21 on the WROOM, on the boards, and 22 of 22 on the H7 in Renode (--emulate). memset:spawn_costschecks it at every start offset from 0 to 7, every length from 0 to 200 and three byte values, between guard bytes. No byte was wrong on any board. On the ESP32s that's ESP-IDF'smemset, as a control.- Stress programs, against main built from a path of the same length:
sync_stressandqnode_stress(sections 2 and 3) pass on all three boards.abort_records: the H7's numbers match main's.- An abort after 4,000 to 16,000 spawns measured 32 bytes less than main on the S3, in each of the four rounds. On the WROOM it measured 68 more in three rounds. In the other, four finished workers were freed during the round, and the rest matched main.
- From one round's start to the next, the free heap fell by the same bytes as on main, on both boards. So the difference is in when 32 or 68 bytes were held, not in what an abort keeps.
- The other differences are 16,492 bytes, a finished worker not yet freed, and 12 to 36 bytes. Why isn't measured.
spawn_reclaimends where main does on every board. Its lowest free heap moves in 16.5 KB steps on the ESP32s, one finished worker at a time: after the change it was 66,072 bytes below the start on the S3 (main: 82,564) and 16,664 on the WROOM (main: 66,072). On the H7 it was 16,704 (main: 16,716).region_shrinkends with the same error as main on every board.
- Unit tests (
tests/test_fs.py,tests/test_fixes.py): every worker's globals go throughmg_globals_alloc, after it reserves its stacks; changing a program twice changes nothing; and a worker whose globals are allocated another way stops the build. - Raw data. Every probe, stress and conformance log behind this section is in bench/results/probes/20261130-spawning.
The WROVER-E, measured after the merge: no gain in the suite. The
WROVER-E runs the WROOM's image, but it has 8 MB of PSRAM on a 40 MHz
quad SPI bus. So it's the one board where this change's PSRAM path runs
on the classic ESP32. Its boot log shows ESP-IDF finding the PSRAM. It
wasn't one of the boards in Results then, so its runs are
in bench/results/dev/.
-
The probe gains.
spawn_costs, in µs:WROVER-E main after spawn and wait, median of 200 2,287 297 callocandfreeof 36,864 bytes (PSRAM)1,836.2 1,834.3 the same in internal RAM 58.7 58.9 memsetof 36,864 bytes (PSRAM)1,747.9 1,747.4 an empty pthread, started and waited for 106.1 106.6 Zeroing in its PSRAM costs about 47 ns a byte, against 31 on the S3. With the change, the probe's worker gets its globals from internal RAM.
-
The suite doesn't. 5 boots each, before (main) and after (
0ab7aa1):WROVER-E, medians before (µs) after (µs) after ÷ before clock_1000584 584 1.000 crc323,702 3,702 1.000 heapsort5,614 5,614 1.000 mandel_f64171,324 171,324 1.000 mandel_f328,432 8,432 1.000 matmul_f324,976 4,976 1.000 queue_20006,408 6,331 0.988 pingpong37 37 1.000 spawn_wait2,330.5 2,326 0.998 scale_1w652,690 652,715 1.000 scale_2w366,163 366,125 1.000 queue_2000's boot medians overlap (5,822 to 6,565 before, 6,267 to 6,390 after). -
Why. One more boot printed where each worker's globals landed (run, with its
experiment.patch).- When the first worker started, 97,999 bytes of internal RAM were free. The globals need 37,136, plus the 64 KB kept free: 102,672. So every worker's globals went to PSRAM, as on main.
- The suite's program keeps 96 KB of data stacks for main, against 16 KB in the probe. The probe started with 214,003 bytes of internal RAM free.
- That boot's own times aren't used: its build has the extra code.
- The WROOM is faster here. With the same chip and image, the WROOM
spawns in 279 µs in the suite: without PSRAM,
calloctakes internal RAM, whatever is left. On the WROVER the reserve sends the globals to PSRAM, so spawning takes 8 times as long. - Every boot stops at
scale_4w, before and after: ESP-IDF couldn't create the fourth worker's task (pthread: Failed to create task). Task stacks come from internal RAM. The WROOM stops one step earlier, atscale_2w. - Checks.
- Every result matches C's, in every boot.
- Conformance passes 21 of 21, the WROOM's set.
sync_stressandqnode_stresspass.memsethad no wrong byte.abort_records: an abort measured 36 to 44 bytes more than main in each round. From one round's start to the next, the free heap fell by the same bytes as on main. The 50 plain waits kept 84 more.spawn_reclaimends with 68 bytes more free than main. Its lowest point was 49,584 bytes below the start, against main's 16,672: two finished workers not yet freed, at spawns 796 and 797.region_shrinkends with main's error.- The logs are in the probes folder above, named
-wrover-.
What it leaves.
- The WROVER-E's reserve. On a classic ESP32 with PSRAM, a program
whose main keeps less internal RAM, like the probe, gets the gain. The
suite's program doesn't. Letting the globals into the 64 KB reserve, as
callocdoes on the WROOM, would leave less internal RAM for ESP-IDF, whose WiFi needs it. That would be a change of its own, not made here. - The globals are still zeroed whole. On the S3 that's 56.5 µs of a
212 µs spawn, in internal RAM. On the H7 the
callocandfreetake 41.3 µs of 102, and on the WROOM 58.5 of 291. The generated code zeroes them before it copies in the ones the worker uses. Skipping that would need to know that nothing reads a global the spawn didn't copy, which isn't established, so the zeroing stays. - Internal RAM is shared on the S3. Each running worker's globals now
take internal RAM that a later worker's data stack would have used. A
program with more workers running at once, or larger globals, gets data
stacks in PSRAM sooner. In the suite,
scale_4wgot faster, but where its four workers' memory landed wasn't recorded. A worker whose globals don't fit goes to PSRAM, as before, at the old cost. - Starting the thread on the ESP32s. From
pthread_createuntil the worker runs takes 76 µs of a spawn on the S3 and 92 on the WROOM, and an empty pthread costs 82 and 101 µs. Their data stacks take another 36 and 48 µs. The H7's takes 3.8. - The WROOM gains nothing from this change. Its spawn is 2.85 times C's, and its worker's setup takes 35 µs before the data stack, against 10 on the S3. Why isn't measured.
- newlib-nano's
memcpyon the H7 is left as it is. It wasn't timed.
5. Queues: the round trip¶
Cause. The new tests/stress/pingpong_costs.goose times the suite's
pingpong: main puts a Ping, a worker puts a Pong back, 1,000 times
after 20 warm-ups. It times the same round trip in C, five ways, and the
pieces of a message on one thread. Each runs twice. On main with the
probe (bc2a340), medians in ns, with both runs' range where they
differ:
| main | S3 | WROOM | H7 |
|---|---|---|---|
Goose, the suite's loop (micros()) |
28,000 | 38,000 | 9,000 |
| Goose, timed with the cycle counter | 26,891 | 38,129 | 8,222–8,272 |
– its qput |
5,029 | 10,091 | 1,515–1,527 |
| – its wait for the Pong | 21,862 | 28,037 | 6,697–6,742 |
C, FreeRTOS queues (as bench.c) |
14,733 | 20,566 | 7,960–7,962 |
| C, task notifications | 18,654 | 26,191 | 3,260–3,262 |
| C, binary semaphores | 14,579 | 20,408 | 7,365–7,372 |
| C, a mailbox under mg_sync's mutex and condition variable | 25,570–26,729 | 35,904 | 7,097–7,107 |
On the ESP32s, main runs on core 1. The workers aren't pinned, and they ran on core 0: in a loop whose worker counts its cores, at least 994 of 1,000 round trips in every build below, and the C task at least 984 of 1,001. The pieces, on one thread:
| main | S3 | WROOM | H7 |
|---|---|---|---|
qput and qget |
2,145–2,162 | 2,538–2,542 | 1,081 |
| mg_sync's critical section, entered and left | 787–790 | 969 | 165 |
| a binary semaphore given to itself and taken | 2,635–2,636 | 3,178–3,182 | 548–549 |
| a task notification given to itself and taken | 2,751–2,753 | 3,360–3,363 | 658 |
| a binary semaphore made in static memory and deleted | 1,578–1,580 | 1,983–1,987 | 406–417 |
| a compare-and-swap, as mg_sync's | 147 | 171–175 | 68 |
| the chip's compare-and-swap, inline | 78 | 88–94 | 68 |
| interrupts masked and unmasked | 160–162 | 175–177 | 85 |
- The time is in mg_sync's waits. In C, a mailbox under mg_sync's
mutex and condition variable takes about as long as Goose: 25.6 to
26.7 µs on the S3 against 26.9, and 35.9 on the WROOM against 38.1.
FreeRTOS's own queues take 14.7 and 20.6. Goose's
qputandqgettogether take 2.2 µs on one thread on the S3. The rest of the round trip is two waits and two wakes. - ESP-IDF wakes a semaphore sooner. Woken from the other core, a task blocked on a binary semaphore makes the round trip in 14.6 µs on the S3 and 20.4 on the WROOM. On its task notification, which mg_sync sleeps on, it takes 18.7 and 26.2. On the H7, with one core, it's the other way round: 3.3 µs on notifications, 7.4 on semaphores.
- The critical section. A message that wakes its receiver enters
mg_sync's critical section three times: when the receiver joins the
list, when the wake takes it off, and when the receiver checks that it
was woken. On the ESP32s that's ESP-IDF's
portENTER_CRITICALandportEXIT_CRITICAL: 790 ns on the S3, so 4.7 µs a round trip. Masking interrupts and an inline compare-and-swap take 160 and 78. - The H7 is already near C: Goose takes 8.2 µs and FreeRTOS's queues in C 8.0. Notifications, which mg_sync already uses there, take 3.3.
Change. Six commits, in components/microgoose/mg_sync.c and, for
the compare-and-swap, mg_qnode.c. Only the first changes the H7.
2894a82, every board. A wake gives the notification first and then sets the waiter'swokenflag, and touches nothing of the waiter after. A waiter that took a notification and sees the flag returns at once, without entering the critical section. One that doesn't see it checks under the critical section, as before.f7d2e90, the ESP32s. A waiter sleeps on a binary semaphore, which it makes for the wait and deletes after. The H7 keeps the task notification.33cffdf, the ESP32s. mg_sync and the node cache called ESP-IDF'sesp_cpu_compare_and_set, which checks for PSRAM and isn't inline. In internal RAM they now use the chip's instruction inline, and in PSRAM, where a queue can be, ESP-IDF's as before.4ff67e2, the ESP32s. Making and deleting a semaphore costs 1.6 µs on the S3. The semaphores are now kept for the next wait, in a free list: as many as tasks have waited at once.6cb895e, the ESP32s. mg_sync's critical section is this core's interrupts masked and a word taken with the compare-and-swap. That is what ESP-IDF's does, without its bookkeeping. It doesn't nest, and the FreeRTOS call inside it, the wake's give, takes ESP-IDF's own lock inside it, never the other way round.528e4b2, the ESP32s. A lock that finds the mutex held, with no task waiting for it, watches it for up to 4 µs before it sleeps, and takes it if it's let go meanwhile. 4 µs is about what the wake that ends a sleep costs the waker on the S3 (below). On one core the holder can't run meanwhile, so the H7 doesn't watch. This step is forqueue_2000(below), not the round trip.
Each step. The probe's medians in ns, both runs' range. Steps 2 to 6 don't change the H7's code, so it ran at steps 1 and 4 only.
| main | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| S3, Goose | 26,891 | 25,983 | 21,541 | 21,187 | 19,829–19,833 | 23,670–23,800 | 22,725–22,937 |
| S3, Goose, the worker counting its cores | 26,833–26,837 | 26,533 | 21,412 | 21,383 | 19,725 | 18,670 | 18,679 |
| S3, C, mg_sync's mailbox | 25,570–26,729 | 20,566 | 20,537 | 20,045 | 18,520 | 17,720 | 17,750 |
| WROOM, Goose | 38,129 | 32,545 | 38,566 | 41,925 | 26,279–26,291 | 24,504–24,525 | 24,525 |
| WROOM, Goose, counting cores | 37,029 | 36,866 | 29,041 | 28,691 | 26,416 | 24,820 | 24,820 |
| WROOM, C, mg_sync's mailbox | 35,904 | 35,595 | 28,000 | 27,437 | 34,812 | 29,050 | 23,695 |
| H7, Goose | 8,222–8,272 | 7,945–7,967 | 7,967–7,972 | ||||
| H7, C, mg_sync's mailbox | 7,097–7,107 | 6,835 | 6,805–6,807 |
FreeRTOS's own round trips in C didn't move: on the S3, its queues took 14,716 to 14,733 ns in every build, and its semaphores 14,579.
Two speeds. The same round trip runs at one of two speeds, and which one changes with the build:
- On the S3 at steps 5 and 6, Goose's loop took 23.7 to 23.8 and
22.7 to 22.9 µs, and the same loop with a worker that also counts the
cores it runs on took 18.7. The loops differ only in that count, in the
worker, between its
qgetand itsqput. At step 4 they took 19.8 and 19.7. - On the WROOM at steps 2 and 3, Goose's loop took 38.6 and 41.9 µs, and the counting one 29.0 and 28.7. C's mailbox under mg_sync moved between 23.7 and 35.9 µs from build to build, and FreeRTOS's queues didn't move.
Four experiments on the S3 at step 5, none of them part of the change (their patches are in the probes folder):
- FreeRTOS in IRAM. ESP-IDF builds FreeRTOS into flash by default.
With
CONFIG_FREERTOS_IN_IRAM, the two loops took 23.6 and 18.1 to 18.2 µs: still two speeds. On the WROOM, Goose took 25.1 µs against 24.5, and FreeRTOS's queues in C 21.3 against 20.6. So flash isn't the cause, and the option isn't taken. - Counters in mg_sync (
experiment-counts.patch). Both loops do the same work. In the slow one, 1,020 round trips made 2,043 condition waits and as many gives, and no lock slept. In the fast one, 2,020 waits, and 4 locks slept. - The gives, timed on each core (
experiment.patch). In the slow loops, main's give to the worker took 6.4 to 6.7 µs on average, and the worker's give to main 3.7 to 4.0. In the fast loop both took 3.9 µs in one build of the experiment, and 4.7 to 5.0 in the other, which also has the pause below. - A pause before main's
qput(the same patch). Main spun for 0 to 12 µs before eachqput, outside the timed span. From 3 µs on, main's give took 4.7 µs and the loop 21.5 to 21.6 µs, against 6.4 and 23.4 with none. That's 1.9 µs of the 4.8 between the two speeds in that build.
So in the slow mode, main's give, from core 1 to the worker on core 0,
costs 1.6 to 2.8 µs more than in the fast loop of the same build, and a
pause before it recovers 1.9 µs of the loop. The rest of the gap isn't
accounted for. Why the give costs more, and what picks the mode, aren't
known. The suite's pingpong runs the plain loop, so it gets whichever
speed the build lands on.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- S3. Before: 20261201T020330Z-s3-goose-5856189-main. After: 20261201T030125Z-s3-goose-528e4b2. Step 5: 20261201T020815Z-s3-goose-6cb895e.
- WROOM. Before:
20261201T020815Z-esp32-goose-5856189-main.
After:
20261201T025816Z-esp32-goose-528e4b2.
Step 5:
20261201T020330Z-esp32-goose-6cb895e.
As in every WROOM run, each boot stops at
scale_2wfor lack of memory (findings item 3). - H7. Before:
20261201T020330Z-h7-goose-5856189-main.
After:
20261201T020420Z-h7-goose-6cb895e,
built at step 5. Step 6 doesn't change the H7's code: the suite's
image built from
528e4b2has the same.text, byte for byte. 528e4b2holds this change's code. Later commits add only results and docs.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,815 | 1.000 | 1.99 |
mandel_f64 |
166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
3,874 | 3,874 | 1.000 | 3.00 |
queue_2000 |
4,192 | 2,754 | 0.657 | 0.80 |
pingpong |
28 | 25 | 0.893 | 1.67 |
spawn_wait |
207 | 223 | 1.077 | 2.75 |
scale_1w |
627,792 | 627,792 | 1.000 | 1.03 |
scale_2w |
323,020 | 323,015 | 1.000 | 1.03 |
scale_4w |
327,942 | 327,900 | 1.000 | 1.04 |
scale_main |
631,150 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 13 | 1.000 | 1.62 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
542 | 864.5 | 1.595 | 0.62 |
fault_contained |
331.5 | 370.5 | 1.118 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,614 | 1.000 | 1.96 |
mandel_f64 |
171,323 | 171,323 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,432 | 1.000 | 1.08 |
matmul_f32 |
4,976 | 4,976 | 1.000 | 3.47 |
queue_2000 |
5,534 | 2,675 | 0.483 | 0.57 |
pingpong |
37 | 25 | 0.676 | 1.19 |
spawn_wait |
288 | 268 | 0.931 | 2.73 |
scale_1w |
653,120 | 653,092 | 1.000 | 1.04 |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
751 | 751 | 1.000 | 3.07 |
crc32 |
1,723 | 1,723 | 1.000 | 0.71 |
heapsort |
1,799 | 1,799 | 1.000 | 2.18 |
mandel_f64 |
7,258 | 7,258 | 1.000 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
969 | 969 | 1.000 | 1.60 |
queue_2000 |
4,244 | 4,411 | 1.039 | 2.40 |
pingpong |
9 | 9 | 1.000 | 1.12 |
spawn_wait |
104 | 107 | 1.029 | 1.14 |
scale_1w |
26,580 | 26,583 | 1.000 | 1.04 |
scale_2w |
26,701 | 26,700 | 1.000 | 1.04 |
scale_4w |
26,915 | 26,917 | 1.000 | 1.04 |
scale_main |
26,477 | 26,477 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
5 | 5 | 1.000 | 0.01 |
fault_contained |
128 | 129 | 1.008 | — |
queue_2000 on the S3: step 6. After step 5, the suite's
queue_2000 took 4,536 µs on the S3 against 4,192 on main
(run). In it, a worker puts 2,000 messages and main takes
them, and the two meet at the queue's mutex. An experiment ran the
suite's queue_2000 and spawn_wait with counters in mg_sync
(threads_costs.goose, built with count3.py): 15 runs of
queue_2000 after 5 warm-ups, in three rounds. Per run, in each round:
S3, queue_2000, with the counters |
main | step 5 | step 6 |
|---|---|---|---|
| mean of the runs (µs) | 4,494, 4,672, 6,285 | 4,208, 4,491, 4,263 | 2,802, 3,140, 3,098 |
| locks that found the mutex held | 390, 386, 818 | 721, 706, 513 | 207, 394, 398 |
| of those, the ones that slept | 92, 94, 171 | 60, 66, 15 | 5, 5, 3 |
| condition waits | 68, 67, 98 | 65, 69, 26 | 6, 4, 5 |
- Each sleep costs both sides. The wake that ends it costs the waker its give, about 4 µs on the S3 (the gives above), and a wake that finds the mutex taken again sends the task back to sleep.
- With step 6, a lock that finds the mutex held watches it for up to 4 µs before it sleeps. Locks that slept went from 60 to 66 a run to 3 to 5, and condition waits from 65 to 69 to 4 to 6.
-
The spin's length. The same experiment with the spin at 2 and 8 µs, and at 4 again. Ranges over the three rounds, in µs:
S3, queue_2000, with the counters2 µs 4 µs 4 µs again 8 µs the fastest run 2,726–2,804 2,732–2,845 2,729–2,819 2,724–2,804 mean of the runs 2,822–2,860 2,802–3,140 2,812–2,943 2,739–3,923 the slowest run 2,963–2,987 2,898–5,852 2,948–3,259 2,750–7,552 locks that slept, per run 4.5–7.8 2.5–5.5 2.5–12.9 0–0.1 At every length the fastest run took 2.7 to 2.8 ms, against 3.3 to 4.2 at step 5. With 8 µs almost no lock slept, but two rounds had runs of 7.4 and 7.6 ms. The change keeps 4 µs.
-
spawn_waitdidn't meet at a mutex at all: in every build, 50 spawns and waits made 50 condition waits and no lock found a mutex held.
queue_2000 on the H7: placement, and a loss. On the H7, only step
1 changes the code, and queue_2000 went from 4,244 to 4,411 µs (1.039)
in every boot. To see what placement alone does, both builds were run
with 4 to 48 bytes of nops after sleep_on, in its own section: that
moves the code after it without changing any code that runs
(setpad.py). 3 boots each, medians in µs:
H7, queue_2000 |
no padding | 4 | 8 | 12 | 16 | 24 | 32 | 48 |
|---|---|---|---|---|---|---|---|---|
| main | 4,244 | 4,202 | 4,289 | 4,295 | 4,232 | 4,243 | 4,179 | 4,197 |
| after | 4,411 | 4,317 | 4,366 | 4,309 | 4,374 | 4,269 | 4,307 | — |
- Placement alone moves main between 4,179 and 4,295 µs, and the change between 4,269 and 4,411.
- The change is slower at every padding, by 14 to 167 µs. Averaged over the seven paddings both builds ran, it takes 4,336 µs against 4,241: 2.3% more. So about 2.3 points of the 3.9% loss aren't this placement.
- In a probe, the change is faster. Section 2's
queue_costs.goosesends messages from a worker to main asqueue_2000does. There, a message took 2,059 and 2,019 ns in the probe's two rounds after the change, and 2,111 and 2,091 on main: 2.5% and 3.4% less. Two runs of each printed the same numbers. Inpingpong_costs, the round trip went from 8.2 to 8.0 µs. So the same code is faster in those programs and slower in the suite's. What makes the suite's build slower isn't known. - No padding is the suite's runs, 5 boots each; 48 bytes was run on main only, 5 boots. In every run, each boot's median was the same, except the change with 4 bytes (4,262 to 4,350).
Against the other implementations. Medians in µs. Goose is after this change; the others are their latest runs, in Results:
pingpong |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 25 | 15 | 6 | 10 | 1,029 |
| WROOM | 25 | 21 | 7 | 17 | 2,526 |
| H7 | 9 | 8 | 2 | 3 | 659 |
queue_2000 |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 2,754 | 3,456 | 1,963 | 4,909 | 275,329 |
| WROOM | 2,675 | 4,726 | 2,297 | 8,378 | 905,506 |
| H7 | 4,411 | 1,841 | 556 | 1,372 | 99,881 |
H7's C is c-fpu. On the S3, TinyGo's run with goroutines on both cores
(tinygo-cores) took 15 µs for pingpong and 11,989 for queue_2000.
pingpongnow takes 1.67 times C's time on the S3, 1.19 on the WROOM and 1.12 on the H7. Rust and TinyGo still take less than C: Embassy's tasks and TinyGo's goroutines switch without FreeRTOS (findings).queue_2000now takes less than C's on both ESP32s: 0.80 of it on the S3 and 0.57 on the WROOM. C'sbench.csends through a FreeRTOS queue. On the H7 it takes 2.40 times C's.
Other movements.
-
spawn_waitandfault_containedon the S3: slower.spawn_waitwent from 207 to 223 µs, and every boot's median from 206–208 to 223–223.5. At step 5 it was 227.fault_contained, which spawns a worker that faults, went from 331.5 to 370.5 (step 5: 396).- Not in the probe. Section 4's
spawn_costsspawns the same empty worker with the suite's globals. Its spawn and wait took a median of 203 to 204 µs with the change and 212 to 218 on main. In the counter builds it took 204 on main and 203 to 204 at step 5; with the counters and step 6's spin it took 222. - No mutex in it. In
threads_costs, the suite'sspawn_waitmade one condition wait per spawn and no lock found a mutex held, in every build (above), so the spin doesn't run there. -
Not placement. The suite's thread benchmarks were run alone (
--only threads, 3 boots each) with 0 to 24 bytes aftersleep_on, in its section, which never run (setpad_esp.py). Medians in µs; the runs are inbench/results/dev/, named-main-threads-padNand-threads-padN:S3, --only threads0 4 8 12 16 24 spawn_wait, main209 208 208 209 208 206 spawn_wait, after215 217 219 224 215.5 222 queue_2000, main4,768 4,294 4,698 4,676 4,491 4,331 queue_2000, after2,760 2,740 2,757 2,759 2,744 2,754 pingpong, main28 28 28 28 28 28 pingpong, after25 25 25 25 25 25 spawn_waittook longer with the change at every padding, by 6 to 16 µs. Main'squeue_2000moved between 4,294 and 4,768, with boots of one run as far apart as 3,892 and 5,029; the change's stayed between 2,740 and 2,760. -
Not where the globals go. One more run of each build, 5 boots, recorded where each worker's globals went (
globals_where.py; inbench/results/dev/, named-globals-where, with the patch). In every boot of main, step 5 and the change, everyspawn_waitworker's globals came from internal RAM; only workers of the scaling benchmarks got PSRAM, the same 69 in every boot. With the record's code in,spawn_waittook 207 µs on main, 243 at step 5 and 238 with the change, andfault_contained341, 421 and 445.5. -
Spawns of about 1.45 ms. In the change's suite run, spawns 11 and 19 took 1,451 to 1,469 µs, in three of the five boots. On main, the
--only threadsruns had three (1,439 to 1,456 µs), in two of six runs; the change's had none. The runs that recorded where the globals went had none in any build. The extra 1.25 ms is about what zeroing the globals in PSRAM costs (section 4), but no run caught one, so that's a guess. They don't move the medians. -
fault_containedalone (--only faults, 3 boots each) took 312 µs on main and 381 with the change (1.221), every boot's median from 312 to 313 and from 380.5 to 381 (main, after). Its fastest fault took 369 µs, against 304 on main. -
A probe (
faults_costs.goose) timesfault_contained's spawn and wait, and the same with an empty worker, 50 times each after 5 warm-ups, in three rounds, the spawn and the wait apart, with the cycle counter on main. Its globals are small, so a spawn costs less than in the suite. Means in µs, the range over the rounds:S3, faults_costsmain 1 2 3 4 5 6 an empty worker 120–128 113–116 121–130 113–114 115–118 112–115 117–126 a worker that faults 199–202 180–185 213–222 221–229 309–311 272–298 255–263 – of it, the spawn 78–81 72–75 80–82 86–87 107 89–97 87–88 – and the wait 121–123 108–111 131–142 135–143 203–204 182–202 167–175 - mg_sync does the same for every fault. With its counters
(
count3.py), on main, at step 5 and with the change, each fault made one condition wait and one give, and no lock found its mutex held. - The spawn takes longer when the worker will fault. The call is the same; only the worker's function differs. On main it took 78 to 81 µs, against 72 to 77 for an empty worker. With the change it took 87 to 88, against 71 to 73, and 83 to 99 at every padding below, where main's took 68 to 76.
- It moves with the build. A fault's cost went down at steps 1 and 6 and up at steps 2 and 4, while an empty worker's stayed between 112 and 130 µs. The counter builds took 184 to 186 µs on main, 225 to 235 at step 5 and 210 to 223 with the change: 15 to 75 µs less than the same steps without the counters. Main, rebuilt and run again, printed the same numbers to the nanosecond.
- Not placement after
sleep_on. With 8 to 128 bytes there (setpad_esp.py), a fault took 177 to 191 µs on main and 228 to 264 with the change, averaged over the rounds: 50 to 82 µs more at every padding. The empty worker took 112 to 118 µs in both. - What makes a fault slower with the change isn't found.
- mg_sync does the same for every fault. With its counters
(
- Not in the probe. Section 4's
-
timer_busy_wakeon the S3. In the suite it went from 542 to 864.5 µs; its boot medians spread from 14 to 898 µs on main and from 227 to 1,373 after. Run alone (--only timers, 5 boots each), it took 1,093 µs on main and 1,107 with the change (1.013), with every boot's median from 1,093 to 1,093.5 and from 1,105 to 1,109 (main, after). The other three timer benchmarks didn't move. - The WROOM's
spawn_waitwent from 288 to 268 µs, the median of every boot (step 5: 269 to 269.5). - The H7's
spawn_waitwent from 104 to 107 µs. With the padding above, main took 103 to 106 µs and the change 103 to 107. - Free heap.
- S3: the same at the start. After
queue_2000, 8,604,511 to 8,605,027 bytes, against 8,604,187 to 8,604,703 on main: 324 bytes more. Within each build, boots differ by 516 bytes, one of the queue node cache's chunks. - WROOM: 16 bytes less at the start, which is the 16 bytes more
of static RAM. After
queue_2000, 129,836 to 130,352 bytes free, against 127,464 on main. - H7: the same as main at every point, in every boot.
- S3: the same at the start. After
- Size.
- The S3's image went from 252,592 to 253,952 bytes, with the same 60,032 of static RAM.
- The WROOM's went from 233,040 to 234,480 bytes, and its static RAM from 57,436 to 57,452.
- The H7's went from 58,304 to 58,352 bytes, with the same 53,744.
Checks.
- Results: every benchmark's result matches C's, in every boot on every board.
- Conformance:
tests/conformance.pypasses 24 of 24 on the S3 and 21 of 21 on the WROOM, on the boards, and 22 of 22 on the H7 in Renode (--emulate). - Stress programs, against main built from a path of the same length:
sync_stressandqnode_stresspass on all three boards. Insync_stress, the counts of how the tasks met moved, as counts of wakes do with timing. With three players taking turns, a broadcast woke a player out of turn 3,237 times on the S3 (main: 126), 3,438 on the WROOM (main: 45) and 4,458 on the H7 (main: 1,733). Every count matched what it should be.pingpong_costschecks each Pong's number, each C round trip and its compare-and-swap count. Every run passed, in every build on every board.abort_records: the H7's numbers match main's. On the ESP32s, from one round's start to the next, the free heap fell by the same bytes as on main (512, then 1,024), and each round started with less free than main's: 96 bytes on the S3, 144 on the WROOM. (On the WROOM that's from round 1: main's round 0 started with a finished worker's 16 KB not yet freed.)spawn_reclaimends where main does on the H7, and 96 bytes lower on the S3 and 112 on the WROOM. Its lowest free heap moves in 16.5 KB steps on the ESP32s, one finished worker not yet freed at a time: 82,764 bytes below the start on the S3 (main: 82,564), and 33,184 on the WROOM in both runs, step 5's and the change's (main: 16,664).region_shrinkends with the same error as main on every board.
- Unit tests (
tests/test_fixes.py,tests/test_fs.py,tests/test_headers.py,tests/test_bench.py) pass. - Raw data. Every probe, experiment, stress and conformance log behind this section is in bench/results/probes/20261201-pingpong.
The WROVER-E. It runs the WROOM's image, with 8 MB of PSRAM
(section 4). It wasn't one of the boards in Results then,
so its runs are in bench/results/dev/: 5 boots each, before
(main)
and after
(528e4b2).
| WROVER-E, medians | before (µs) | after (µs) | after ÷ before |
|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 |
crc32 |
3,702 | 3,702 | 1.000 |
heapsort |
5,614 | 5,614 | 1.000 |
mandel_f64 |
171,324 | 171,324 | 1.000 |
mandel_f32 |
8,432 | 8,432 | 1.000 |
matmul_f32 |
4,976 | 4,976 | 1.000 |
queue_2000 |
6,386 | 3,305 | 0.518 |
pingpong |
37 | 25 | 0.676 |
spawn_wait |
2,331 | 2,318 | 0.994 |
scale_1w |
652,692 | 652,748 | 1.000 |
scale_2w |
366,062 | 366,200 | 1.000 |
queue_2000's boot medians went from 6,330–6,552 µs to 3,192–3,587.- Two boots ran every benchmark. In the other three, and in every
boot on main, ESP-IDF couldn't create
scale_4w's fourth worker's task, as in every WROVER-E run in section 4. Why two got through isn't known. Main has nothing to compare their last seven benchmarks with, so they're left out above. - The probes. In
pingpong_costs, the round trip went from 38.1 to 24.5 µs, and the C mailbox under mg_sync's mutex and condition variable from 35.9 to 23.7. C through FreeRTOS's queues took 20.6 in both. Inspawn_costs, a spawn and wait went from 297 to 308 and 309 µs: on this board, slower in the probe too. - Checks.
- Every result matches C's, in every boot.
- Conformance passes 21 of 21, the WROOM's set.
sync_stress,qnode_stressandpingpong_costspass. A broadcast woke a player out of turn 3,121 times (main: 58).abort_records: each round started with 112 to 120 bytes less free than main's, and from round 1 to round 3 the free heap fell by the same 1,536 bytes. Round 2 started with finished workers not yet freed in both: two on main, three with the change.spawn_reclaimends 108 bytes below main. Its lowest point was 49,676 bytes below the start, against main's 49,584: three finished workers not yet freed, in both.region_shrinkends with main's error.- The logs are in the probes folder above, named
-wrover-.
What it leaves.
- The two speeds. On the S3 the suite's
pingpongruns in the slow mode, 25 µs; the fast mode's loop takes about 19. What picks the mode isn't known. A pause before the send helps it, so the cost is in how the two cores meet, but the change doesn't add one: it would slow every send. - Wakes across cores. Every round trip on the ESP32s still has two sleeps and two wakes, each wake a give from one core to the other: 3.7 to 6.7 µs of the giver's time on the S3. C through FreeRTOS's queues pays the same and takes 14.7 µs. A waiter could watch for its message for a while before it sleeps, as step 6 does for mutexes, but that would hold a core whenever a message is slow to come, so it isn't done here.
- The H7's
queue_2000: 2.3% slower beyond what placement moves, while the same code is faster in two probes. Not resolved. - Faults and spawns on the S3. With the change,
fault_containedtakes 1.12 times main's time in the suite and 1.22 run alone, andspawn_wait6 to 16 µs more at every placement tried. mg_sync does the same work for them as on main, and a fault's cost moves with the build by as much as the change moves it. What makes them slower wasn't found.
6. Indexed loops: matmul_f32 and heapsort¶
Cause. The new tests/stress/loops_costs.goose times the suite's
heapsort and matmul_f32, copied from bench.goose as they are,
beside C versions in loops_costs.h. Those start from bench.c's code
and add one of Goose's ingredients at a time. Each runs 15 times after 2
warm-ups, in three rounds. On main (8386400) with the probe, the median
of the 15 in µs, the range over the rounds where they differ:
heapsort, main |
S3 | WROOM | H7 |
|---|---|---|---|
| Goose | 4,719 | 5,777 | 1,733–1,734 |
C as bench.c: int indexes |
2,539 | 2,912 | 821–822 |
int64_t indexes, computed as Goose computes i64 |
4,482–4,486 | 4,884 | 1,242 |
int indexes, with the two bounds checks Goose keeps |
2,698 | 3,058 | 891–892 |
int64_t indexes and the two bounds checks |
3,921–3,922 | 4,474 | 1,672 |
matmul_f32, main |
S3 | WROOM | H7 |
|---|---|---|---|
| Goose | 3,960 | 5,315 | 977 |
C as bench/c builds it: each multiply and add fused |
1,297 | 1,438 | 607 |
| C, each operation rounded on its own, as Goose's | 1,715 | 1,987 | 613 |
– in blocks of 8, int counters |
2,562–2,566 | 3,102 | 967 |
– in blocks of 8, int64_t counters |
3,980 | 5,112 | 972 |
– one term at a time, int64_t counters |
1,696–1,697 | 1,968–1,969 | 612 |
Each variant's result was the same as Goose's, in every line.
matmul_f32: the loop shape. Goose's C adds the sum's terms in blocks of 8 (below). In C withintcounters, that shape alone takes 1.49 times as long as the plain loop on the S3, 1.56 on the WROOM and 1.58 on the H7. Withint64_tcounters, as Goose's, it takes 2.32, 2.57 and 1.59 times: 3,980 µs on the S3, where Goose took 3,960, and 5,112 on the WROOM against 5,315. The plain loop takes the same withint64_tcounters as withint: 1,697 µs against 1,715 on the S3. So on main, Goose'smatmul_f32cost what the blocked shape costs.- C fuses multiply and add.
bench/cis built with GCC's default,-ffp-contract=fast, somatmul_f32'ss += a * bis one fused multiply-add, rounded once:madd.son the ESP32s,vfma.f32on the H7. Goose's C is compiled under#pragma GCC optimize("fp-contract=off"): Goose never fuses a*followed by a+(Goose's spec, section 7.12). Rounding twice, C takes 1.32 times as long on the S3 (1,715 µs against 1,297), 1.38 on the WROOM and 1.01 on the H7.mandel_f32is fused too, and on the H7mandel_f64; Methodology now says so. heapsort: 64-bit indexes. In C,int64_tindexes, computed as Goose's release C computesi64, take 1.77 times as long asinton the S3, 1.68 on the WROOM and 1.51 on the H7. Ani64takes two registers on these 32-bit cores, and each compare or add two instructions or more.- The bounds checks. Goose keeps two of
heapsort's (bench.gooseline 100). Withintindexes they add 6.3% on the S3, 5.0% on the WROOM and 8.5% on the H7. Withint64_tindexes they made C faster on the ESP32s: 3,922 µs against 4,484 on the S3. In the S3's build, that sift function has no call to the check's failure, and one 64-bit shift fewer: GCC seems to have proved from its callers that the checks pass, and used the range they give. On the H7, GCC 7.2 made the same C slower: 1,672 µs against 1,242. - The rest of
heapsort. Goose takes 1.20 times as long as C withint64_tindexes and the two checks on the S3, 1.29 on the WROOM and 1.04 on the H7. Goose's C has more in it:sift_downinlined into its caller, the array reached through the globals' pointer, each compare's result cast touint8_t. What each of those costs isn't measured.
Goose's release C, which the suite runs, has no overflow checks: its
i64 arithmetic wraps. The probe's first version (4043dff) also had C
copies of the checks Goose emits in debug builds; they took 2.2 to 3.9
times Goose's own time, so they weren't Goose's code, and the last
version leaves them out (its logs are in the probes folder).
Change. One commit, 20690cd, in microgoose/core.py, for every
board. Goose emits an in-order sum (s += a[i] * b[i] in a counted loop)
in the shape its implementation notes call "sum terms in blocks of 8"
(Goose's implementation.md, section 6.12): while 8 terms or more are
left, a block computes 8 terms into a local array and then adds them to
the sum in order; a second copy of the loop then runs the rest one term
at a time. plain_sums turns the block's while (cond) into
while (0 && (cond)), so the block never runs and the second copy runs
every term. Both copies have the same body, so the terms, their order and
every rounding are the same, and so is every result. The generated C
keeps Goose's comment on the loop, with "run one at a time by microgoose"
added, and a mark at the top of the file. In the suite, two loops change:
matmul's sum and mat_check's, which isn't timed. The generated C of
the suite differs from main's in those two loops and the mark, on every
board, and nothing else. No other program in tests/stress has a sum in
blocks, besides the probe, and of Goose's samples only 31_mini_doom,
which isn't in the conformance set.
On the S3, main's block stores each product to the array on the stack and loads it back, and both of its 8-term loops count with a 64-bit counter: two compares and branches a term. With the change the inner loop is 8 instructions, counting down a 32-bit register.
The probe after the change. Goose's matmul_f32 took 1,969 µs on the
WROOM and 612 to 613 on the H7: what C takes one term at a time with
int64_t counters, rounding twice (1,969 and 612). On the S3 it took
1,834 µs, against C's 1,697. The same Goose source took 1,688 µs in the
suite's image and in the probe's first version, so on the S3 it moves by
9% from one image to another. Code the change doesn't touch moved there
too: Goose's heapsort took 4,846 µs against 4,719 on main (4,678
against 4,746 in the first version), and C in blocks of 8 with int
counters 2,536 to 2,540 against 2,562 to 2,566.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- S3. Before: 20261201T125250Z-s3-goose-8386400-main. After: 20261201T125844Z-s3-goose-4043dff.
- WROOM. Before:
20261201T125844Z-esp32-goose-8386400-main.
After:
20261201T125207Z-esp32-goose-4043dff.
As in every WROOM run, each boot stops at
scale_2wfor lack of memory (findings item 3). - H7. Before: 20261201T130327Z-h7-goose-8386400-main. After: 20261201T130756Z-h7-goose-4043dff.
4043dffis20690cdwith the probe. Later commits change only the probe, a docstring, results and docs.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,659 | 0.968 | 1.93 |
mandel_f64 |
166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
3,874 | 1,688 | 0.436 | 1.31 |
queue_2000 |
2,753 | 2,809 | 1.020 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
222 | 219 | 0.986 | 2.70 |
scale_1w |
627,798 | 627,768 | 1.000 | 1.03 |
scale_2w |
323,010 | 323,024 | 1.000 | 1.03 |
scale_4w |
327,520 | 327,423 | 1.000 | 1.04 |
scale_main |
631,151 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 13 | 1.000 | 1.62 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
275 | 39 | 0.142 | 0.03 |
fault_contained |
366 | 346 | 0.945 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,541 | 0.987 | 1.94 |
mandel_f64 |
171,324 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,425 | 0.999 | 1.08 |
matmul_f32 |
4,976 | 1,961 | 0.394 | 1.37 |
queue_2000 |
2,721 | 2,802 | 1.030 | 0.59 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
267 | 271 | 1.015 | 2.77 |
scale_1w |
653,125 | 653,128 | 1.000 | 1.04 |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
751 | 751 | 1.000 | 3.07 |
crc32 |
1,723 | 1,682 | 0.976 | 0.69 |
heapsort |
1,799 | 1,774 | 0.986 | 2.15 |
mandel_f64 |
7,258 | 7,251 | 0.999 | 1.04 |
mandel_f32 |
3,391 | 3,394 | 1.001 | 0.92 |
matmul_f32 |
969 | 615 | 0.635 | 1.01 |
queue_2000 |
4,411 | 4,361 | 0.989 | 2.37 |
pingpong |
9 | 9 | 1.000 | 1.12 |
spawn_wait |
107 | 106 | 0.991 | 1.13 |
scale_1w |
26,583 | 26,583 | 1.000 | 1.04 |
scale_2w |
26,700 | 26,700 | 1.000 | 1.04 |
scale_4w |
26,918 | 26,916 | 1.000 | 1.04 |
scale_main |
26,477 | 26,478 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
5 | 4 | 0.800 | 0.01 |
fault_contained |
129 | 129 | 1.000 | — |
matmul_f32 took the same time in every boot of each run, on every
board.
Against the other implementations. Medians in µs. Goose is after this change; the others are their latest runs, in Results:
matmul_f32 |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 1,688 | 1,292 | 3,339 | 2,080 | 701,836 |
| WROOM | 1,961 | 1,432 | 3,608 | 3,126 | 829,481 |
| H7 | 615 | 607 | 626 | 10,317 | 191,019 |
heapsort |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 4,659 | 2,420 | 3,661 | 2,872 | 593,805 |
| WROOM | 5,541 | 2,857 | 4,212 | 4,856 | 1,125,490 |
| H7 | 1,774 | 826 | 955 | 1,141 | 239,989 |
H7's C is c-fpu. TinyGo's matmul_f32 on the H7 uses software floating
point.
matmul_f32now takes less time than Rust's and TinyGo's on every board, and 1.31 times C's on the S3, 1.37 on the WROOM and 1.01 on the H7. That's about what fusing saves C: rounding twice, C takes 1.32 times as long on the S3 and 1.38 on the WROOM (above).heapsorttakes 1.93, 1.94 and 2.15 times C's, and more than Rust's and TinyGo's on every board. Rust indexes withusizeand TinyGo withint, both 32 bits on these cores; Goose'si64takes two registers.
Other movements.
heapsortandcrc32: the same Goose, different machine code. On the ESP32s they're in the same C function asmatmul,compute_g, and the change's smaller loop changes how GCC compiles the whole function: its stack frame offsets differ, andheapsort's timed lines compile to 189 instructions on the S3 against 191 on main, and 188 on the WROOM against 194.heapsorttook 0.968 of main's time on the S3 and 0.987 on the WROOM, every boot's median the same within each run.crc32's code differs too, but its time didn't move there. On the H7, whose images have no line information,crc32took 0.976 of main's time andheapsort0.986; theremain_gholds the suite's compute code, and it shrank by 120 bytes, the only function whose size changed.- Placement. Both versions' images were built again from one
worktree to compare them. Only
compute_g(main_gon the H7) changed size; every function after it sits 172 bytes lower on the S3, 196 on the WROOM and 112 or 120 on the H7.queue_2000took 1.020 of main's time on the S3 (boot medians 2,743 to 2,755 µs on main, 2,802 to 2,813 after), 1.030 on the WROOM (2,673 to 2,794, and 2,733 to 3,076) and 0.989 on the H7. Section 5 measured placement alone moving the H7'squeue_2000between 4,179 and 4,411 µs.spawn_waittook 219 µs on the S3 against 222 (boot medians 218 to 220, and 219 to 225), 271 on the WROOM against 267, and 106 on the H7 against 107.fault_containedon the S3 took 346 µs against 366 on main (boot medians 342.5 to 350, and 361.5 to 381.5).
timer_busy_wakeon the S3 went from 275 to 39 µs. Its boot medians spread from 14 to 1,443.5 µs on main and from 14 to 945 after, as in section 5.- Free heap.
- S3: the same as main up to
queue_2000. After it, 8,602,447 bytes free in every boot, against 8,604,511 to 8,605,027 on main: 4 or 5 more of the queue node cache's 516-byte chunks, which it keeps until reset. How many it takes depends on how many messages wait at once. - WROOM: the same up to
queue_2000. After it, 127,256 to 129,836 bytes free, against 129,320 to 130,352 on main. - H7: the same as main at every point, in every boot.
- S3: the same as main up to
- Size. The images are smaller by 160 bytes on the S3 (253,952 to 253,792), 208 on the WROOM (234,480 to 234,272) and 112 on the H7 (58,352 to 58,240), with the same static RAM.
Checks.
- Results: every benchmark's result matches C's, in every boot on
every board.
matmul_f32's result is the same as main's. - Conformance:
tests/conformance.pypasses 24 of 24 on the S3 and 21 of 21 on the WROOM, on the boards, and 22 of 22 on the H7 in Renode (--emulate). None of those programs has a sum in blocks, so they show only that nothing else changed. The rewrite is checked by the suite's and the probe's results, and the unit tests. - Stress programs, against main built from a path of the same length:
sync_stress,qnode_stressandpingpong_costspass on all three boards. Every count matched what it should be; counts of wakes moved, as they do with timing. With three players taking turns, a broadcast woke a player out of turn 3,237 times on the S3 (main: 3,078) and 3,428 on the WROOM (main: 3,188).spawn_costsprinted the same numbers as main on every board, to the µs, and itsmemsetcheck passed on the H7.abort_records: the H7's numbers match main's. On the S3, rounds 0 to 2 match main's, and round 3 started with 40 bytes more free. On the WROOM, each round started with 96 bytes more free than main's, and the free heap fell by the same bytes from round to round.spawn_reclaimends where main does on every board, with main's lowest free heap: 82,764 bytes below the start on the S3 and 66,168 on the WROOM.region_shrinkends with the same error as main on every board.
- Unit tests (
tests/test_fixes.py,tests/test_fs.py,tests/test_headers.py,tests/test_bench.py) pass, but for one intest_fs.pythat needs a C compiler on the host and is skipped. Two are new, intest_fixes.py: one builds a dot product with Goose and checks that each block's loop is turned off and that a second pass changes nothing; the other, that other loop shapes are left alone. - Raw data. Every probe, stress and conformance log behind this section is in bench/results/probes/20261201-loops.
The WROVER-E. It runs the WROOM's image, with 8 MB of PSRAM. Its runs
are in bench/results/dev/: 5 boots each, before
(main)
and after
(4043dff).
| WROVER-E, medians | before (µs) | after (µs) | after ÷ before |
|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 |
crc32 |
3,702 | 3,702 | 1.000 |
heapsort |
5,614 | 5,544 | 0.988 |
mandel_f64 |
171,324 | 171,317 | 1.000 |
mandel_f32 |
8,432 | 8,425 | 0.999 |
matmul_f32 |
4,976 | 1,961 | 0.394 |
queue_2000 |
3,175 | 3,100 | 0.976 |
pingpong |
25 | 25 | 1.000 |
spawn_wait |
2,322 | 2,331 | 1.004 |
scale_1w |
657,083 | 657,073 | 1.000 |
scale_2w |
366,151 | 366,145 | 1.000 |
- Three boots of five ran every benchmark after the change, and none
on main: in the others, ESP-IDF couldn't create
scale_4w's fourth worker's task, as in sections 4 and 5. The change doesn't touch memory. Why three got through isn't known, as with section 5's two. - Checks. Every result matches C's, in every boot. The board was off the USB after these runs, so its stress programs and conformance weren't run.
What it leaves.
heapsort: 1.93 to 2.15 times C. In C, 64-bit indexes alone take 1.51 to 1.77 times as long. MicroGoose compiles the C Goose writes and can't narrow itsi64: whether an index fits in 32 bits is for the Goose compiler to know. Rust and TinyGo index in 32 bits on these cores.matmul_f32on the ESP32s: 1.31 and 1.37 times C, which is C's fused multiply-add. Goose never fuses a*and a+. A program can ask for one rounding withmath'smul_add, but Goose's runtime fuses it only where__FMA__or arm64 is defined, so on these boards it rounds twice, though both chips have the instruction forf32, and the H7 forf64. MicroGoose could make it fused there; that isn't tried here, and the suite doesn't call it.- Goose's other restated loops. "Strip-mined by N" blocks (Goose's
implementation.md, section 6.12) are left as Goose emits them. No benchmark has one. Of Goose's samples,07_sieve,12_huffman,27_gfx_cubeand28_physics_boxesdo, and12_huffmanpasses conformance on every board. Their cost on these cores isn't measured.
7. The H7's clock¶
Cause. The new tests/stress/clock_costs.goose times 1,000 reads of
the clock with the cycle counter, from Goose and from C, and on the H7
the parts of a read, in C copies of the port's code that count cycles of
their own (clock_costs.h). Each runs 15 times after 2 warm-ups, in
three rounds. On main (365c0d6), cycles a read at 400 MHz, the range
of the rounds' medians, and ns:
| H7, main, a read | cycles | ns |
|---|---|---|
Goose's micros() |
303–306 | 758–766 |
the port's mg_micros(), called from C |
304–307 | 761–768 |
the port's clock_gettime(), called from C |
279–280 | 698–700 |
| the 64-bit count of cycles: interrupts masked, the counter read, its wraps counted | 24 | 60 |
– and / 400, to µs: all of C's read (bp_now_us) |
96–97 | 240–243 |
– or * 1000 / 400, to ns: clock_gettime()'s first line |
125–126 | 313–315 |
– and / 1000000000, % 1000000000: all of clock_gettime() |
267–268 | 668–671 |
– and sec * 1000000 + nsec / 1000: all of mg_micros() |
286–287 | 716–718 |
| seconds, µs and cycles kept, moved forward with one 32-bit division, masked | 28 | 70 |
| a base the tick would move, read without masking, one 32-bit division | 23 | 58 |
- Three 64-bit divisions.
mg_micros()calledclock_gettime(), which turned the count of cycles into nanoseconds and split those into seconds and nanoseconds;mg_micros()then made microseconds of them again. The Cortex-M7 divides 32 bits in hardware and 64 bits in software: one such division costs 72 cycles here, and the three, with the call, about 260 of the read's 305. - C makes one.
bench/c'sbp_now_us()divides the same count by 400 and is done: 96 cycles in the probe, and 245 µs for the suite's 1,000 reads. - None is needed. Kept as seconds, the microseconds in the second and the cycles past the microsecond, the time only moves forward by the cycles since the last read, which fit 32 bits. The two last rows read that way.
Change. One commit, 6df7348, in ports/stm32h7/src/mg_h7.c. The
ESP32s' code doesn't change.
- The time is kept as it's asked for. The port keeps seconds, the microseconds in the second and the cycles past the microsecond. Every read masks interrupts, takes the cycles the 32-bit counter has counted since the last read, and moves the time forward with one 32-bit division. The 1 ms tick reads it too, so the counter's wrap, every 10.74 s, is never missed, as before.
- Every clock reads it.
micros(),clock_gettime(), the times interrupts stamp events with, and the count of cyclesdelay_ms()andnanosleep()wait on. Each gives the value it gave before: the same count of cycles, divided the same way. - The counter's start. The time is set to 0 where the counter is
started, in
main. Before, a tick that came earlier had already read the counter. - The first version, and a build flag. The first version built the
time in a struct inside the read and returned it. A read of
micros()took 81 cycles that way, and 70 with the read inline. The port's code is built with-fno-tree-sra -fno-ipa-sra, a workaround for an Xtensa GCC miscompile that every board's build has (Methodology); with them GCC copied the struct through the stack. Without the two flags the same inline version took 54. The change's read hands back plain integers and takes 50 with the flags on. A struct isn't the cost in itself: the change's read with its numbers put in a struct by an inline function also takes 50, and 59 by a called one. These five builds ran after the change was merged (first_version.pyandstruct_time.py, in the probes folder), with the probe's last version. - Not taken: a read that doesn't mask interrupts. In the probe it takes 23 cycles against 28. It needs the tick to move its base, and a second pass when the base moved under it. The masked read is the old one's shape, and every read keeps it current.
The probe after the change (6df7348), cycles a read:
| H7, a read | main | after |
|---|---|---|
Goose's micros() |
303–306 | 51 |
mg_micros(), called from C |
304–307 | 52–53 |
clock_gettime(), called from C |
279–280 | 71 |
The C copies took what they took on main, to a cycle.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- Before: 20261202T013035Z-h7-goose-365c0d6-main.
- After: 20261202T013159Z-h7-goose-6df7348.
6df7348holds this change's code. Later commits change only the probe, a comment, results and docs.
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
751 | 127 | 0.169 | 0.52 |
crc32 |
1,682 | 1,682 | 1.000 | 0.69 |
heapsort |
1,774 | 1,777 | 1.002 | 2.15 |
mandel_f64 |
7,251 | 7,251 | 1.000 | 1.04 |
mandel_f32 |
3,394 | 3,394 | 1.000 | 0.92 |
matmul_f32 |
615 | 614 | 0.998 | 1.01 |
queue_2000 |
4,361 | 4,412 | 1.012 | 2.40 |
pingpong |
9 | 8 | 0.889 | 1.00 |
spawn_wait |
106 | 104 | 0.981 | 1.11 |
scale_1w |
26,583 | 26,585 | 1.000 | 1.04 |
scale_2w |
26,700 | 26,703 | 1.000 | 1.04 |
scale_4w |
26,916 | 26,917 | 1.000 | 1.04 |
scale_main |
26,478 | 26,479 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
5 | 4 | 0.800 | 0.01 |
fault_contained |
129 | 128 | 0.992 | — |
clock_1000 took 127 µs in every boot, against 751 in every boot of
main.
Against the other implementations. Medians in µs, on the H7. Goose is after this change; the others are their latest runs, in Results:
clock_1000 |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| H7 | 127 | 245 | 40 | 1,004 | 1,520 |
C is c-fpu. Goose's read now takes 0.52 of C's, which still makes its
one 64-bit division. Rust's takes 40 ns: embassy's clock is a timer that
counts microseconds (time-driver-tim2, tick-hz-1_000_000), so a read
divides nothing.
Other movements.
- Shorter because the clock is. Every sample is the difference of
two reads of
micros(), so it holds about one read's time, and a read costs 0.64 µs less.pingpong's mean went from 8.74 to 8.20 µs a round trip, and its median from 9 to 8;spawn_waitwent from 106 to 104 µs, andfault_containedfrom 129 to 128. Inpingpong_costs, which times the round trip with the cycle counter, it took 7,965 ns on main and 7,992 after; its copy of the suite's loop, timed withmicros(), went from a mean of 8,729 ns to 8,121. So those are the measurement's own cost, not the round trip's. queue_2000went from 4,361 to 4,412 µs (1.012), in every boot. Its code is the same. Section 5 measured placement alone moving it between 4,179 and 4,411 µs on this board.heapsortwent from 1,774 to 1,777 µs, andmatmul_f32from 615 to 614, in every boot.- Free heap. 68 bytes less at the start, 8 of them static RAM, and
72 less from
queue_2000on, in every boot. In one boot of five, the heap afterfault_containedwas 16,512 bytes lower again: one finished worker not yet freed when it was read, which can happen on main too (findings, correctness item 2). - Size. The image is 240 bytes smaller (58,240 to 58,000), with 8 bytes more of static RAM (53,744 to 53,752).
Checks.
- Results: every benchmark's result matches C's, in every boot.
- The clock (
clock_costs's checks, on main and on the change), until 23 s after boot, so across two wraps of the counter:- Alone: main read
micros(), a count of cycles the probe keeps for itself, andclock_gettime(), over and over until 19 s: 19.5 million times on the change, 6.1 million on main. No read went back,clock_gettime()was never outside themicros()on either side of it, andmicros()never left the probe's own count by more than 3 µs. - Together: for 4 s more, a worker read the clock the same way
(7.2 million times) while a 1 kHz timer's interrupt stamped 4,000
events and main took them. No read went back in either thread; no
event's time was before the last one's, or after the
micros()read once main had it. delay_ms(50)took 50,000 µs bymicros()and 50,001 by the probe's count (main: 50,001 and 50,002).
- Alone: main read
- Conformance:
tests/conformance.pypasses 22 of 22 in Renode (--emulate). Renode doesn't model the cycle counter, so the clock stands still there, on main too: that run shows the port still builds and runs every program, not that the clock is right. - Stress programs, against main built from a path of the same length:
sync_stress,qnode_stressandpingpong_costspass. Insync_stress, which waits onclock_gettime()'s deadlines, none of 60 timed waits ended early, and the latest ended 1,002 µs after its deadline (main: 1,001). With three players taking turns, a broadcast woke no player out of turn (main: 4,458 times); every count matched what it should be.spawn_costs: a spawn and wait took a median of 101 and 99 µs in its two rounds (main: 102 and 103), timed withmicros(). Itsmemsetcheck passed.abort_records: each round's cost is main's (496, 512, 1,024 and 0 bytes), and each round started with 20 bytes more free.spawn_reclaimends 16 bytes below its start (main: 20), and its lowest free heap was 16,704 bytes below the start in both.region_shrinkends with the same error as main. With 10 workers held it had 33,464 bytes free (main: 33,320), and its last line counts 8 data stacks past the MPU's guard regions and 482,748 bytes in use (main: 9, and 499,344): one data stack fewer when the last spawn was refused. Why isn't looked into.
- Unit tests (
tests/test_fixes.py,tests/test_fs.py,tests/test_headers.py,tests/test_bench.py) pass, but for one intest_fs.pythat needs a C compiler on the host and is skipped. - Raw data. Every probe, stress and conformance log behind this section is in bench/results/probes/20261202-clock.
The ESP32s. Their code is the same as main's, so they didn't run the suite again. They ran the probe once, on main, for what it measures:
| a read, ns | S3 | WROOM |
|---|---|---|
Goose's micros(), which is esp_timer_get_time() |
855 | 584 |
clock_gettime(CLOCK_MONOTONIC), called from C |
1,798 | 1,634 |
- Two clocks.
clock_gettime()ran 68,756 µs ahead ofmicros()on the S3 and 75,383 on the WROOM, and stayed there within 3 µs until 19 s: ESP-IDF's clock andesp_timer's start apart. The probe's first version took them for one clock, and its check failed at every read there; the last holds them to their distance. No read went back on either board, in either thread, and no event's time was out of order. delay_ms(50)came back early on the WROOM: after 49,770 µs. On the S3 it took 50,087. That is a fault of its own, in the findings (correctness item 9), and isn't fixed here.- The H7 ran the probe's last version later. Its ST-Link stopped
answering after the runs above, so the tables here are the first
version's. Once it answered again, the last version passed on main
(
365c0d6) and on the change as merged (9594ece), withmicros()andclock_gettime()0 to 1 µs apart: a read ofmicros()took 303 to 306 cycles on main and 50 after.
What it leaves.
- C's read is now the slower one on the H7, by its one division.
bench/cis left as it is: it's the reference. - A timer that counts microseconds would need no division at all, as Rust's read shows (40 ns against 127). Not tried here.
clock_gettime()on the ESP32s costs 1.6 to 1.8 µs a read, againstmicros()'s 0.6 to 0.9. The runtime's timed waits read it. Not looked into.
8. Timer wake-ups on the ESP32s¶
Cause. The new tests/stress/timer_wake.goose takes 1,000 ticks of
a 1 ms timer after 10 not counted, in three rounds, and keeps three times
for every tick, all on one clock (timer_wake.h): when the tick was due,
when its callback or interrupt read the clock (its timestamp), and when
the waiting task had it. It times the port's timer_every() and
event_wait() from Goose, and the same wait written in C on an
esp_timer and on a hardware timer's interrupt. On main (d45dd85),
with nothing else running, medians in µs, the same in every round:
| a 1 ms tick, nothing else running | S3 | WROOM |
|---|---|---|
| the port: due → timestamp | 10 | 11 |
| the port: timestamp → the program has it | 13 | 17 |
| the port: both | 23 | 28 |
C on an esp_timer: both |
22 | 27 |
| C on a hardware timer's interrupt: due → timestamp | 1 | 1 |
| C on a hardware timer's interrupt: timestamp → the task has it | 8 | 9–10 |
| C on a hardware timer's interrupt: both | 9 | 10–11 |
- Two hops.
timer_every()was anesp_timer. Its alarm interrupts core 0, andesp_timer's task runs the callback there, 10 to 11 µs after the tick was due. The callback stamps the tick and wakes the program, which is on core 1: 13 to 17 µs more. - The suite saw the second hop only.
timer_idle_wakeis the time from a tick's timestamp to the program having it, and Goose's timestamp was the callback's. C's reference stamps in its timer's interrupt. So the baseline's 13 µs against 8 and 17 against 10 left 10 to 11 µs of Goose's delay out. -
Beside one worker: one of two states. A worker spins at the program's priority, as in the suite's
timer_busy_wake. The probe also notes which core the worker is on at each tick:the port's tick beside one worker, main S3 WROOM round 0: median, due → the program has it 908 µs 28 µs round 0: ticks 500 µs late or more, of 1,000 1,000 0 round 1 23 µs; 0 28 µs; 0 round 2 641 µs; 946 28 µs; 0 - Slow with the worker on the program's core. In round 0 the worker was on core 1 at every tick, and core 0 was idle. In round 2 it was on core 0 for the first 54 ticks, each of which took 23 µs, and on core 1 for the other 946.
- Why it stays there, from FreeRTOS as ESP-IDF 6.1 builds it for
two cores (
tasks.c). A task made ready doesn't take a core from a task of its own priority (prvIsYieldRequiredSMP); it gets the core at that core's next 1 ms tick, where tasks of one priority take turns. MicroGoose ties the program to core 1 and leaves workers free. So the program gets each tick up to a time slice late. While it has core 1 the worker is ready and core 0 is idle, but an idle core looks for a task only at its own tick, and the program has the tick and waits again some 20 µs later: core 1 picks the worker back up. - Fast with the worker on core 0.
esp_timer's task has a higher priority and takes core 0 from the worker for each tick; core 1 is idle when the program is woken. - Which one a run lands in wasn't traced on main. One way a worker gets onto the program's core was traced on the change (below).
- The same wait every tick. A 1 ms timer's tick falls at one point
of the scheduler's 1 ms tick for a whole run, so in the slow state
every tick waits the same time, or one time slice more. The suite's
four timer benchmarks, run alone on main for this section, had every
wake of
timer_busy_wakeat 609 to 620 µs or at 1,601 to 1,608, in every boot: medians of 1,107 to 1,114.
-
Beside two workers, one on each core, every way waits for a time slice, C's on the hardware timer too: the port took a median of 972 to 988 µs on both boards, and C 415 to 585 on the S3 and 565 to 569 on the WROOM.
-
Core 0 paid for the ticks.
esp_timer's task takes core 0 for every tick, whichever task waits for it. The probe times a fixed piece of work, about 251 ms, alone and beside a timer:work, main (µs) S3 WROOM on a worker, alone 252,348–252,916 252,974–252,994 on a worker, while the program takes a 1 ms timer's ticks 255,227–257,539 257,756–257,987 on a worker, while the program takes a 100 µs timer's ticks 351,352–352,312 286,093–393,383 on the program, alone 251,074–251,076 250,931–250,932 on the program, while a worker takes the program's 1 ms timer's ticks 251,073–251,075 250,923 on the program, while a worker takes the program's 100 µs timer's ticks 251,078–258,363 250,923
Change. Five commits in components/microgoose/mg_hal.c:
76c40f2, b108ffb, eab216a, 3d20542 and d2ab934. The H7's port
doesn't change.
timer_every()is a hardware timer. Each timer is one of the chip's four general-purpose timers (ESP-IDF's GPTimer), counting microseconds, with an alarm that reloads itself. Its interrupt stamps the tick and queues it, as a pin's interrupt does. ESP-IDF takes the interrupt on the core of the task that started the timer: for a program's main task, the core it waits on. So there is one hop, and the timestamp is taken 1 to 3 µs after the tick was due.- A wake that was held up frees the core. Each event carries what
its interrupt saw: which core it was on, and whether queuing the event
let the waiting task take that core. That is FreeRTOS's answer to the
interrupt, and it is no when a task of the same priority is running
there. When
event_wait()has waited, and the interrupt found a task in the way, and the event is more than 30 µs old, it looks at the other core. If that core is idle,event_wait()tells it to pick its next task now, and goes on once it has, or after 100 µs. The task that was in the way moves over, and the next event finds the program's core idle. One event is late; the ones after it aren't. Pins' events go through the same wait, so this is theirs too. - An interrupt on the other core can't tell what the program's core was doing: a timer a worker started, say, while the main task waits. For those events the age alone decides.
- A hand-over that finds nothing backs off. The task in the way may be tied to the program's core, or gone by the time the program runs. The try then waits its whole 100 µs for nothing, so the next one comes no sooner than 10 ms later, and twice as long after each try that fails as soon as it is allowed, up to 2.56 s.
- What stays. Four timers at once, periods from 100 µs, the same
queue, and
timer_stop()dropping the timer's queued ticks. - New limits.
- All four hardware timers. The S3 and the ESP32 have four each. C
code built into a program (
--include) that takes one leaves three fortimer_every(), which returns -1 when none is free. - The interrupt is on the starter's core. A timer a worker starts interrupts whichever core that worker was on then, since workers aren't tied to one.
- All four hardware timers. The S3 and the ESP32 have four each. C
code built into a program (
- How it got there. The suite and the probe looked nearly the same
at every step; a second probe and a trace told the steps apart. Each step's
runs are kept (
firsttofourth, in the probes folder andbench/results/dev/).- First (
76c40f2): the hardware timers, and a hand-over at every wake more than 30 µs old. The newtests/stress/pin_wake.goose(Other movements) then found a build of a pin-only program on the WROOM where every wake took 32 µs with nothing else running (What it leaves). Each tried a hand-over that had nothing to hand over and waited its time out: the 99th percentile was 88 µs in two of the three idle rounds. - Second (
b108ffb): the back-off, which only a hand-over that worked brought down again. A build that logs every try (vary_trace.py) showed the WROOM's first event after a pause arriving 32 to 36 µs old with nothing else running: just past the 30. Each got a try that found nothing, 62 to 63 µs in all, and started the back-off or doubled it. By that rule a program that waits on a button would back off further at every press, and a real hold-up after a press would wait the back-off out. That last part is the code read, not a measurement. - Third (
eab216a): the interrupt says whether a task was in the way, so those wakes try nothing, and a failed try doubles the back-off only if it comes as soon as it is allowed. The same trace on this rule shows the WROOM's 35 µs wakes cleared, with no try. - Fourth (
3d20542): the interrupt's answer is tested before the clock is read. The third read the clock at every wake, which cost a pin's edge 1 µs: 11 µs on the S3 against main's 10, and 13 on the WROOM against 12. Now only a wake the interrupt doesn't clear reads it, and an edge takes what it took on main. - Last (
d2ab934): the hand-over waits up to 100 µs for the other core, not 50. The trace had the other core taking its task 6 to 35 µs after the wake was found late on the S3, and 7 to 51 on the WROOM. One that ran past 50 was counted as having found nothing.
- First (
- Not taken.
esp_timer's callback in its interrupt handler (CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD). In the probe, C waiting that way had the tick 17 µs after it was due on the S3 and 21 on the WROOM, against 9 and 10 to 11 on the hardware timer: the handler reads the clock 6 to 7 µs after the tick is due, and the wake still crosses to core 1. Turning the option on also made the port's own way slower on main (26 and 31 µs against 23 and 28).- Waiting one priority up. A task that waits above its workers' priority takes its core at once, with no hand-over, and C waiting that way on the hardware timer had the tick in 9 to 11 µs. But it has to come back down before it runs the program's code. With nothing else running it was back 17 to 25 µs after the tick, later than waiting at its own priority; beside two workers, 660 to 950 µs after.
- The hardware timer alone. Without the hand-over, a worker on the
program's core still holds every tick up (
no-hand-over, in Checks).
The probe after the change (d2ab934), medians in µs. Where the
three rounds differ, each is given.
| a 1 ms tick, due → the program has it | S3 main | S3 after | WROOM main | WROOM after |
|---|---|---|---|---|
| nothing else running: due → timestamp | 10 | 1–2 | 11 | 2 |
| nothing else running: timestamp → the program has it | 13 | 10–11 | 17 | 12 |
| nothing else running: both | 23 | 12 | 28 | 14 |
| beside one worker | 908, 23, 641 | 13 | 28 | 14–15 |
| – ticks 100 µs late or more, of 1,000 a round | 1,000, 0, 946 | 0 | 0 | 0 |
| the worker put on the program's core | 153, 815, 441 | 13, 13, 16 | 28, 716, 714 | 14–15 |
| – ticks 100 µs late or more, of 1,000 a round | 1,000 | 2, 1, 1 | 0, 1,000, 1,000 | 1 |
| beside two workers, one on each core | 972–978 | 568–591 | 972–988 | 647–684 |
| C on a hardware timer, nothing else running | 9 | 9–10 | 10–11 | 11 |
| C on a hardware timer, beside two workers | 415–585 | 593–604 | 565–569 | 599–605 |
- One hop. The timestamp is taken 1 to 2 µs after the tick is due, and the program has the tick 10 to 12 µs later: 12 and 14 µs in all, against 23 and 28. C's wait on its own hardware timer takes 9 to 11.
- Beside one worker no tick of 3,000 was late on either board.
- With the worker put on the program's core, one or two ticks of a round's 1,000 came late, and the hand-over moved the worker.
- A round the hand-over left alone. In the S3's third round the worker stayed on the program's core for all 1,000 ticks, and each took 16 µs. In the slow state a tick waits for the scheduler's next tick, and that distance is one value for a whole run (Cause): here it was a few µs, under the 30 that counts as late. The fourth step had such a round on the WROOM, at 33 µs by the probe and 30 or less by the port's own reading of the clock, which comes a µs or two sooner. That those rounds were this, and nothing else, isn't measured.
- Beside two workers there is no idle core to hand a task to, and every way waits for a time slice, as on main. The wait depends on where the timer's ticks fall in the scheduler's, which each run sets anew.
- Four timers at once kept their periods exactly: no gap between two ticks of one timer was off by a microsecond, where main's were off by up to 11 µs on the S3 and 13 on the WROOM.
-
Work beside ticks. The fixed piece of work again, in µs, the range of three rounds:
work (µs) S3 main S3 after WROOM main WROOM after on a worker, alone 252,348–252,916 252,354–252,919 252,974–252,994 252,940–252,952 on a worker, while the program takes a 1 ms timer's ticks 255,227–257,539 251,535–251,574 257,756–257,987 251,912 on a worker, while the program takes a 100 µs timer's ticks 351,352–352,312 251,622–252,814 286,093–393,383 251,929–253,943 on the program, alone 251,074–251,076 251,076 250,931–250,932 250,923–250,931 on the program, while a worker takes the program's 1 ms timer's ticks 251,073–251,075 253,440–253,448 250,923 253,776–253,799 on the program, while a worker takes the program's 100 µs timer's ticks 251,078–258,363 276,996–277,000 250,923 282,788–282,789 - The worker's core is left alone. A worker computing while the
program takes ticks lost 0.9 to 2.1% at 1 ms and 13 to 55% at 100 µs
on main, to
esp_timer's task. It now takes no longer than alone, but for one WROOM round at 100 µs (0.4% over). - The starter's core pays instead. A timer's interrupt is on the core of the task that started it. When the program starts a timer, computes, and leaves the ticks to a worker, each tick takes 8.6 µs from the program on the S3 and 10.4 on the WROOM: 0.9 and 1.1% at 1 ms, 10 and 13% at 100 µs. On main that cost the program nothing.
- A timer a worker starts interrupts the core that worker was on then. With the program computing and a worker running its own 100 µs timer, the program's work took 251,076 to 251,819 µs on the S3 and 282,785 to 283,098 on the WROOM, which fits the worker having started its timer on the other core on the S3 and on the program's core on the WROOM.
- The worker's core is left alone. A worker computing while the
program takes ticks lost 0.9 to 2.1% at 1 ms and 13 to 55% at 100 µs
on main, to
How a worker gets onto the program's core. One way was traced, on
the WROOM, with a build of the second step that logs every hand-over
(vary_trace.py, in the probes folder).
- Late wakes 100 ms apart. In one round of the probe with that build, with the worker put on core 0 by the first tick's hand-over, 10 more ticks came about 700 µs late, each exactly 100 ticks after the last. Each found the worker on the program's core again, and each hand-over moved it back within 7 µs.
- The runtime's stack check. MicroGoose looks at its stack canaries
again every 100 ms, in a callback
esp_timer's task runs on core 0. That task takes core 0 from a worker there. If core 1's tick comes while the worker is set aside and the program is waiting, core 1 picks the worker up: that much is the scheduler's code read, and the table below is the measurement. -
Moving the check moves the late wakes. A program that takes a 1 ms timer's ticks beside one worker, 30 rounds of 1,010 ticks (
hand_trace_busy.goose), on three builds:the stack check rounds with a late wake after the first tick hand-overs every 100 ms, as it is 0 of 30 0 every 100.037 ms 12 of 30 14 left out 0 of 30 0 At 100.037 ms the check slides across the 1 ms tick, 37 µs a time. The 14 late wakes came a whole number of checks apart (6 to 33 of them), all within 0.8 ms of one point of the check's period, and all 14 hand-overs found the worker.
-
All or nothing at 100 ms. The check then falls at one point of the tick for a whole run. So a run has none of these, as that build's 30 rounds, or one every 100 ms.
- On main a worker that got onto the program's core this way stayed there: nothing moved it back. Whether that is how the slow rounds above began wasn't measured on main.
Before and after. 5 boots each, built from git worktrees with paths of the same length.
- S3. Before: 20261202T051326Z-s3-goose-d45dd85-main. After: 20261202T112837Z-s3-goose-d2ab934.
- WROOM. Before:
20261202T064427Z-esp32-goose-d45dd85-main.
After:
20261202T112839Z-esp32-goose-d2ab934.
As in every WROOM run, each boot stops at
scale_2wfor lack of memory (findings item 3). Its timer rows (†) are the four timer benchmarks run alone: before, after. - The S3's timer benchmarks alone, as a check on the full run: before, after.
d2ab934holds this change's code. Later commits change only a comment, results and docs.- The earlier steps' runs are in
bench/results/dev/: the first's (76c40f2), the second's (6192126), the third's (277a032) and the fourth's (3d20542).
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,659 | 4,659 | 1.000 | 1.93 |
mandel_f64 |
166,167 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
1,688 | 1,688 | 1.000 | 1.31 |
queue_2000 |
2,809 | 2,795 | 0.995 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
219 | 221 | 1.009 | 2.73 |
scale_1w |
627,769 | 627,807 | 1.000 | 1.03 |
scale_2w |
323,000 | 324,011 | 1.003 | 1.03 |
scale_4w |
327,435 | 327,944 | 1.002 | 1.04 |
scale_main |
631,150 | 631,151 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
13 | 10 | 0.769 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
574 | 10 | 0.017 | 0.01 |
fault_contained |
341 | 393 | 1.152 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,541 | 5,541 | 1.000 | 1.94 |
mandel_f64 |
171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
3,081 | 2,994 | 0.972 | 0.63 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
276 | 285 | 1.033 | 2.91 |
scale_1w |
653,101 | 653,174 | 1.000 | 1.04 |
timer_idle_gap † |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake † |
17 | 11 | 0.647 | 1.10 |
timer_busy_gap † |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake † |
17 | 12 | 0.706 | 0.01 |
timer_idle_wakeis not the whole gain. It runs from a tick's timestamp to the program having it. Before, the timestamp was taken 10 to 11 µs after the tick was due, and now 1 to 2 µs after: from when the tick was due, the probe has 23 to 12 µs on the S3 and 28 to 14 on the WROOM.timer_busy_wakeon the S3: a boot of main had a median of 174 to 886 µs. After, every one of the 5,000 wakes took 10 µs, idle and busy. Run alone, main's wakes took 609 to 620 µs or a time slice more, in every boot, and the change's 10 µs, every one.- On the WROOM main was in its fast state in every boot: 17 µs at every wake. After, every wake took 11 or 12 µs, idle and busy.
- One value a run. With the timestamp in the interrupt, the 1,000 wakes of a run nearly all take the same time: 10 µs on the S3, and 11 in some runs and 12 in others on the WROOM.
timer_idle_gapandtimer_busy_gap: every gap between two ticks' timestamps was 1,000 µs, but for 10 of the WROOM's 5,000 busy gaps, at 999 or 1,001. On main, 5 of the S3's 5,000 busy gaps were 1,001.
Against the other implementations. Medians in µs. Goose is after this change; the others are their latest runs, in Results:
timer_idle_wake |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 10 | 8 | 8 | 91,467 | 497 |
| WROOM | 11 | 10 | 11 | 91,467.5 | 1,349 |
timer_busy_wake |
Goose | C | Rust | TinyGo | MicroPython |
|---|---|---|---|---|---|
| S3 | 10 | 1,385.5 | 261 | 68 | 8,048 |
| WROOM | 12 | 1,356.5 | 288 | 156 | 7,884 |
- Idle, Goose is 1 to 2 µs behind C, and for the first time the two are timed alike: both stamp the tick in a hardware timer's interrupt. At the baseline Goose's 13 and 17 µs left out the 10 to 11 µs before its timestamp. C's wait is a FreeRTOS queue; Goose's is a binary semaphore and the event queue's lock. Where the difference goes wasn't measured.
- Beside a worker, C and Rust wait as before: C for the time slice, Rust for the worker's next yield. Goose now takes 10 µs on the S3 and 11 or 12 on the WROOM in every boot, where the S3's medians ran from 14 to 1,058 µs across the baseline's boots.
- TinyGo's
timer_idle_wakeis a TinyGo bug on the ESP32s (findings): the woken goroutine runs only when the scheduler's sleep ends. With its multicore scheduler (tinygo-cores, S3) it takes 13 µs.
Other movements.
-
A pin's edge goes through the same wait, so the new
tests/stress/pin_wake.goosetimes one: a pin puts out a 997 Hz wave and watches its own rising edges, 1,000 after 10, in three rounds idle and three beside a worker. A 1,003 µs timer goes through the same loop. Neither period divides the scheduler's 1 ms tick, so the events fall everywhere in it. Medians in µs from the event's timestamp to the program having it, and how many of a round's 1,000 took 100 µs or more:pin_wakeS3 main S3 after WROOM main WROOM after a pin's edge, nothing else running 10 10 12 12 a pin's edge, beside a worker 491, 11, 10 11 12, 13, 12 12 – late, of 1,000 a round 866, 376, 0 1, 0, 0 0, 331, 0 1, 0, 0 a timer's tick, nothing else running 13 10 17 12 a timer's tick, beside a worker 241–249 10–11 17 11–12 – late, of 1,000 a round 641–649 0 302–365 1, 1, 0 - Pins had the slow state too. On main an edge beside a worker took a median of 491 µs in one round on the S3, and a third of the WROOM's were late in one. The hand-over is theirs as well: at most one late edge a round.
- The third step cost a pin 1 µs: 11 µs idle on the S3 and 13 on the WROOM, for the clock read. With the read behind the interrupt's answer an edge takes what it took on main.
-
scale_2won the S3: 0.3% slower, 323,000 to 324,011 µs, in every boot.scale_4wwent from 327,435 to 327,944 andscale_1wfrom 627,769 to 627,807.- It is the heap's start, not the timers. The image takes 896 bytes more of internal RAM. With two workers alive, the second one's copy of the globals (37 KB in the suite) then finds no free block there and goes to PSRAM, where clearing it costs 1.1 ms (section 4).
-
Shown on main. An experiment build counts where each copy went (
exp_globals.py; 3 boots each, counters of 16 bytes):S3 scale_2w(µs)copies scale_2wputs in PSRAM, of its 34 a bootmain, with the counters 323,055 0 main, with the counters and an unused array of 864 bytes 324,003 17 the first step ( 76c40f2), with the counters324,003 17 The array puts the heap's start where the first step's is.
-
An instrument can tip it by itself. Section 5's record of where the globals went took 2,564 bytes of static RAM, and found 69 copies a boot in PSRAM. Here a boot of main has 51 with the 16-byte counters and 68 with the array added: about 18 of that section's 69 were there because of the record. Its conclusion, about
spawn_wait's workers, doesn't rest on them.
-
fault_containedon the S3: 341 to 393 µs (1.152), in every boot (389.5 to 402.5). It runs none of the changed code. It took 438 µs at the first step, 363 to 366 at the second, 397.5 at the third and 394 at the fourth, whose fault path is the same; and 394.5, 348.5 and 406 in the three experiment builds above, so it didn't follow the heap's start either. Why it moves with the build isn't looked into. spawn_wait: 219 to 221 µs on the S3, 276 to 285 on the WROOM (1.033; 281 to 285 in every boot, against 270 to 277). Inspawn_costs, whose image is the same size as on main because it uses no timer, a spawn and wait took a median of 299 and 300 µs on the WROOM (main: 299 and 299) and 203 and 204 on the S3 (main: 203 and 204). So the spawn's code costs what it did; the suite's image is 8.8 KB larger, and what in that movesspawn_waitby 3% isn't looked into.queue_2000: 2,809 to 2,795 µs on the S3 and 3,081 to 2,994 on the WROOM. Some of its 75 repetitions take two to six times the median, on main as on the change: 9 and 9 of them on the S3, 9 and 11 on the WROOM.- Free heap. At the program's start, 896 bytes less on the S3, all
internal RAM, and 160 less on the WROOM. Once timers have run and
stopped, 112 bytes more stay taken than on main (S3, after
fault_contained): ESP-IDF keeps a hardware timer group's state. In one WROOM boot of five, the heap afterqueue_2000was 2,580 bytes higher than in the other four; main's five were the same. - Size. The suite's image is 8,256 bytes larger on the S3 (253,792 to 262,048) and 8,848 on the WROOM (234,272 to 243,120), for ESP-IDF's hardware timer driver. Static RAM grows by 124 and 164 bytes. A program that starts no timer doesn't link the driver: eight of the nine stress programs build to images whose six segments are the sizes they are on main.
Checks.
- Results: every benchmark's result matches C's, in every boot.
- The probes' own checks pass on both boards, on main and on the change: no tick or edge missed, out of order or dropped in any run; four timers at once with a fifth refused, each stopped with nothing left queued, and all four started again.
- Variants of the change (
vary2.py, in the probes folder), each through the probe:- Without the hand-over (
no-hand-over), with nothing else running, a tick takes the same 12 and 14 µs: the hand-over's test costs a wake that isn't held up nothing that shows. With the worker put on the program's core, every tick of every round was late: medians of 327 to 846 µs on the S3 and 308 to 714 on the WROOM. - Late past 50 µs, not 30 (
late-50): nothing changed in this run. On the fourth step's build that limit left a whole S3 round alone, at 40 µs a tick with the worker on the program's core, which the 30 µs limit hands over. - Age alone (
age-only, the second step's rule): a tick with nothing else running takes 13 and 15 µs, a µs more, for the clock read at every wake. - Every wake counted as held up (
held-5): with nothing else running each try finds nothing, and the back-off is what keeps them apart: a median of 13 µs on the S3 and 15 to 17 on the WROOM, the slowest wake 113 to 118, and 6 of an idle round's 1,000 edges at 100 µs or more. It also shows what the back-off costs. After an idle round of tries that found nothing, the S3's first round beside a worker had 180 of its 1,000 edges late before a try was allowed again.
- Without the hand-over (
- Every hand-over logged (
vary_trace3.py), through the probe on both boards. Outside the runs beside two workers, where the other core is never idle, a wake more than 30 µs old was handed over 11 times on the S3 and 4 on the WROOM, the other core taking its task 6 to 39 µs later on the S3 and 27 to 46 on the WROOM. One more on the S3 found the other core busy. Five more on the WROOM, 35 to 49 µs old, had found the program's core free and were cleared with no try. None timed out. - The stack check at 100.037 ms on that build, on the WROOM, 30 rounds beside a worker: 10 late ticks, each handed over, the other core taking the worker 11 to 50 µs later.
- Where an event falls in the scheduler's tick (
phase_sweep.goose, nothing else running, 40 runs 50 µs apart for a pin and 40 for a timer): every run's median was 9 to 11 µs on the S3 and 11 to 13 on the WROOM, but for one WROOM run at 15, and no wake took more than 17. - Conformance:
tests/conformance.pypasses 24 of 24 on the S3 and 21 of 21 on the WROOM, on the boards. - Stress programs, against main built from a path of the same length.
Eight of the nine start no timer, and their images are the sizes they
are on main:
sync_stress,qnode_stressandpingpong_costspass. None ofsync_stress's 60 timed waits ended early; the latest ended 999 µs after its deadline on both boards (main: 999 and 1,017).spawn_costs: medians as on main (Other movements). Itsmemsetcheck passed.abort_records: on the S3 each round's cost is within 32 bytes of main's (460, 512, 992 and 0 against 460, 480, 992 and -32). On the WROOM its heap readings differ from run to run of one image: main's four rounds cost 492, 17,004, 17,516 and 0 bytes, and the change's 492, 512, 1,024 and 36.spawn_reclaim: its lowest free heap was 82,764 bytes below the start on the S3 and 66,168 on the WROOM, as on main.region_shrinkends with main's error on each board, and the same free heap.clock_costs, the one that takes a timer's ticks: no read of the clock went back in either thread, and none of the 4,000 ticks it took was dropped, out of order or stamped after themicros()read once the program had it. Its worker made 1.1% more reads in the 4 s on the S3 and 2.4% more on the WROOM than on main.delay_early: none of its waits ended early on either board.
- Unit tests (
tests/test_fixes.py,tests/test_fs.py,tests/test_headers.py,tests/test_bench.py) pass, but for one intest_fs.pythat needs a C compiler on the host and is skipped. - Boots that repeat. Three of the S3's five boots printed the same console to the byte, every sample included, and two of the WROOM's. Main's five differ on each board. Each file starts at the boot ROM's banner, so they are separate boots. Why some now repeat isn't looked into.
- Raw data. Every probe, stress, conformance and experiment log behind this section is in bench/results/probes/20261202-timer.
What it leaves.
- Both cores busy. With a worker on each core, an event waits for the scheduler's tick, as on main: 568 to 684 µs at the median here, and up to a time slice more. There is no idle core to hand a task to. Waiting one priority up gets the tick at once but not the core back (Change).
- The stack check moves workers. Every 100 ms, in
esp_timer's task on core 0. When it falls on core 1's tick, a worker lands on the program's core, one event is late, and the hand-over moves the worker back: in a run where the two line up, one tick in a hundred at 1 kHz. Running the check where it takes no worker's core isn't tried here. - A wait of 30 µs or less is left alone. In the slow state the wait is the distance from the event to the scheduler's next tick, one value for a whole run when the events are a 1 ms timer's. Two rounds had a worker on the program's core for all 1,000 ticks and nothing handed over: every tick took 16 µs in one, and 33 in the other (The probe after the change). That it was this distance, and nothing else, wasn't measured.
- The 32 µs build. The first step's build of a pin-only program on
the WROOM, where every wake took 32 µs with nothing else running, isn't
explained. One small function added to the program made it 13. Main's
build of the program took 12, and the second, third and fourth
steps' 12 to 13, and the change's 12. A sweep of where an event falls
in the scheduler's tick, 50 µs at a time (
phase_sweep.goose), found no such wake on the change (Checks). fault_containedandspawn_waitmoved on code this change doesn't touch (Other movements). Not looked into.- Goose's idle wake is 1 to 2 µs behind C's. Not profiled.
- A program's timers are the chip's. A fifth
timer_every()is refused, as before, but so is a third or fourth when built-in C code holds hardware timers of its own.esp_timerhad no such limit on what C code could take.
9. The WROOM's memory rules¶
Cause. On the WROOM every boot of the suite stopped at scale_2w:
its second worker was refused a data stack (cannot reserve data stack
address space), in a program that had 131,528 bytes free when it
started. The findings named two rules behind that. Every data stack in a
program has one size, and the ESP32 port holds 64 KB of internal RAM back
for everything else.
An experiment patch (memprobe.py in the probes folder, not part of the
change) measured where the memory went. It fills each data stack with a
pattern when it is reserved and looks for how far the stack was written
when it is released, or when the program ends. It records each
reservation, release, refusal and copy of the globals with the heap's
free bytes and largest free block, and each thread's native stack at its
fullest. The suite's program ran once with it on each board, on main
(1a8cf86):
| the suite's memory, in bytes | reserved | written on the WROOM, up to scale_2w |
written on the S3, the whole suite |
|---|---|---|---|
| main's six data stacks | 16,448 each | 16,384, 122, 120, 0, 0, 0 | 16,384, 8,000, 122, 15, 120, 121 |
| a worker's native stack | 16,384 | 1,092 at most | 1,844 at most |
| a worker's data stack | 16,448 | 0, in each of 88 workers | 0, in each of 211 workers |
| a worker's copy of the globals | 37,136 | none of it is used | none of it is used |
- A worker held 70 KB and wrote 1 KB of it. 122,556 bytes were free
before
scale_1wspawned its worker and 50,512 once the worker had its copy of the globals: 72,044 bytes, of which its native stack, its data stack and that copy are 69,968. - The copy of the globals, which the findings didn't name. A worker's entry, as Goose generates it, allocates and zeroes a copy of every global in the program, then copies in the ones the worker's program uses. None of the suite's six kinds of worker uses a global, so each spawn allocated 37,136 bytes for nothing. Section 4 made that copy faster to zero on the S3 and the H7. It didn't ask whether a worker needs it.
- One size. A worker's data stack is 16 KB because main's are, and main's first is that large for one moment: the suite's 16 KB global is built there before it is copied to its place, which fills the stack to its last byte. The baseline found that 8 KB stacks overflow at that point. No worker wrote a byte to its own data stack. Five of main's six hold 8,000 bytes or less over the whole suite.
- 64 KB held back. The port gave a data stack from internal RAM only while that left 64 KB free. On the S3 a stack that doesn't pass goes to PSRAM. The WROOM has none, so the stack is refused, and a refused data stack ends the program.
- At the refusal both workers had their native stacks, and the first had its data stack and its copy of the globals. 33,840 bytes were free, the largest block 14,336. The second worker's 16,448 bytes fit no block, so on main the 64 KB rule wasn't yet what decided. After the first step below it was.
-
What the 64 KB is for.
tests/stress/heap_needs.goose(new) measures what a program without networking takes from the heap on the WROOM beyond its stacks: the free heap before each thing it can do, while that is open or held, and after it is closed.on the WROOM, in bytes held while open kept after a queue's first message 516 516 1,000 messages waiting in a queue 28,152 3,612 a worker with no data stack, waiting (the first, the second) 16,876, 16,668 208, 0 timer_every428 296 pin_watch16 16 uart_open2,488 204 i2c_open1,540 196 spi_open1,988 88 can_open1,156 460 pwm_start320 320 adc_read_mv, thenadc_capture436, 0 436, 0 dac_write104 88 a file written in RAM 312 312 a file written in flash/, then read0, 0 0, 0 The largest thing one call of the port's own takes is a UART's 2,488 bytes. Messages and workers are the program's to count. With WiFi up it is different: WiFi and its servers allocate as they run, whatever the program does. That wasn't measured here.
Change. Four commits. The first three are the change, each run on its own. The fourth is a correctness fix the third made necessary:
- A worker's copy of the globals holds only the part it uses
(
ce78e82:own_globalsinmicrogoose/core.py, on every board).- The part. In a board's build a worker's entry allocates the bytes from its first used global to its last, zeroed, and sets its globals pointer to where the whole struct would start, so the generated code reaches each field where it expects it. A worker that uses none allocates nothing: its pointer is an address no board maps, where a read ends the program (Checks).
- Which globals. The entry names them: the compiler copies into a worker's instance the globals its program uses (Goose's spec, §11.2), a worker it spawns included. MicroGoose takes that set only where the generated C agrees with it. It follows every function the entry can reach, by any mention of the function's name, and each must touch the globals only field by field, and only fields the entry names. A function named outside any function, in a table say, counts as reachable from every entry. Where anything disagrees, or the C isn't in the shape expected, the worker keeps a whole copy, through the same path.
- Across the repo: 50 worker entries in 24 programs (the suite,
the stress programs, the examples and Goose's samples). 43 use no
global, 7 use a part, and none fell back to a whole copy. Six of
the 7 are in the new
tests/stress/worker_globals.goose. - The host build is unchanged: the compiler's
callocof the whole struct.
--worker-reserve: a data-stack size of their own for workers (784b4f1:worker_stacksincore.py, a patch to the runtime'sgs_reserve_region, on every board). A region reserved on a worker's thread takes that size, in whole pages of the runtime's: 4 KB on the ESP32s, 32 bytes on the H7.microgoose buildandruntake the option, and so doesbench/run.py. Without it nothing changes: a worker's stacks are--reservebytes, as before. The overflow reports name both options.- Without PSRAM or WiFi, a data stack leaves 8 KB, not 64 KB
(
f1fc54f:components/microgoose/mg_compat.c, the ESP32s). A board with PSRAM keeps the 64 KB rule, where it only chooses which memory a stack gets. A networked program keeps it too, on any board: its start-up code says so before WiFi starts. A board with neither, the WROOM running a program without networking, gives a data stack while that leaves 8 KB (MG_INTERNAL_FLOOR): over three times the largest single need in the table above. - A spawn's native stack waits for finished workers too (
c704e27:mg_compat.c, the ESP32s). This one is a correctness fix the third step made necessary, and it is kept apart from the three above.- The defect.
thread_waitreturns once a worker is done, and ESP-IDF frees a task that deleted itself only when an idle task next runs. Since the baseline's correctness fix, a refused data stack waits up to five ticks for that. While 64 KB was held back, the data stack was the first thing a spawn on a full board couldn't have, so that wait covered the native stacks as well. With 8 KB a spawn can run out at its native stack first, and nothing waited there. - How it showed. With the third step alone (
f1fc54f) and 4 KB worker stacks, one boot in ten of the suite ended atscale_4wwithcannot create thread, right afterscale_2w's workers had ended. The probe caught the same in a run of its own. Eleven repetitions ofscale_4wor more got their four workers, then one found no block for its fourth native stack. The heap had 16,592 bytes less at that point than a repetition earlier: one finished worker's stack, not yet freed. - The fix. The firmware links with
--wrap=pthread_create, and apthread_createthat fails for lack of memory lets the idle tasks run, a tick at a time, up to five, before it gives up. Nothing changes for one that succeeds. The program's last line counts the stacks that waited. - The test.
tests/stress/low_heap.goose(new) takes workers until one more wouldn't fit above the floor, checks that the program still works with what is left, then lets them all go and takes as many again at once, 20 times. With 4 KB worker stacks it holds 8 workers with 15,420 bytes free. Atf1fc54fit ends withcannot create threadin those rounds. Atc704e27it passes, and one stack waited.
- The defect.
- Not taken.
- A smaller default for workers. A 4 KB default would let the suite run on the WROOM as built. It would also break a program whose workers build more than 4 KB of text or arrays, and past the stacks with a hardware guard an overflow is found by a canary only afterwards. One suite's workers aren't grounds for that.
- A size for each of main's stacks. Which stack a function's data lands on is the compiler's numbering, so sizes would have to come from a measured run and could go stale with any change to the program.
- Reserving a stack when it is first written. A PC does that with its MMU. Here a push is an unchecked move of a pointer, and the port can't see the first one.
- A smaller native stack for workers. 16 KB, of which the suite's workers used 1.8 KB at most. C's workers have the same 16 KB, and it is what a deep call chain in any worker has to fit.
Each step on the WROOM. Where the suite's boots stopped:
| build | built with | the suite on the WROOM |
|---|---|---|
main, 1a8cf86 |
the defaults | stops at scale_2w: its second worker's data stack is refused |
the globals step, ce78e82 |
the defaults | stops at scale_2w with 77,040 to 79,624 bytes free: the 64 KB rule alone refuses the same request (16,448 + 65,536 is more) |
with a size for workers' stacks, cd6dafa |
--worker-reserve 4K |
runs scale_2w, stops at scale_4w with 51,812 to 58,588 bytes free |
with the floor, f1fc54f |
the defaults | runs scale_2w, stops at scale_4w: no block is left for a 16 KB data stack |
with the floor, f1fc54f |
--worker-reserve 4K |
all 18 benchmarks in 9 boots of 10, cannot create thread at scale_4w in one |
with the wait, c704e27 |
the defaults | runs scale_2w, stops at scale_4w, as at f1fc54f |
with the wait, c704e27 |
--worker-reserve 4K |
all 18 benchmarks, in 15 boots of 15 |
- A worker's memory. On main one worker of the suite took
72,044 bytes. After the change a worker with one data stack takes
33,212 bytes as built, and 20,924 with
--worker-reserve 4K(low_heap's workers, which have the same stacks as the suite's). - No boot needed the wait. In those 15 boots no spawn found its
native stack refused, so the suite doesn't show the last step working.
low_heapdoes. - With the defaults the WROOM still stops at
scale_4w. Four workers at 33,212 bytes are 132,848, and the suite has 127,088 to 129,668 free there. The probe shows how it ends: all four native stacks and one data stack are allocated, 39,424 bytes are free, and the largest block is 14,336, so the next 16,448-byte data stack fits nowhere. The floor would have allowed it. - With 4 KB worker stacks it has little to spare. In the probe's run
of the whole suite at
c704e27the heap's lowest point was 18,428 bytes free. The probe keeps 4,532 bytes of tables of its own, so about 23,000 without it.
A spawn. tests/stress/spawn_costs.goose is
section 4's probe: the
suite's globals, an empty worker spawned and waited for 200 times, twice
a run. Medians in µs, each pair one run:
| spawn and wait | S3 | WROOM | H7 |
|---|---|---|---|
main, 1a8cf86 |
202, 204 and 212, 214 | 299, 299 (twice) | 101, 99 |
the globals step, ce78e82 |
157, 169 | 202, 203 (twice) | not run |
a size for workers' stacks, 784b4f1 |
not run | 229, 229 | not run |
the floor, f1fc54f |
153, 158 and 153, 161 | 276, 277 and 277, 277 | 62, 62 |
the wait, c704e27 |
152, 166 | 261, 263 | 62, 62 (at cd73810) |
- The S3 and the H7 save what the copy cost: 49 to 59 µs of a spawn
on the S3 and 37 to 39 on the H7. The probe times a
callocandfreeof the globals' 36,864 bytes at 56.5 and 41.5 µs. - The WROOM's probe moved with every build, and not by what the
builds changed. Each commit's number repeats exactly when it is built
again. But
f1fc54fwith 12 nops added tommap()and nothing else took 285 µs, and with 40 nops 254.f1fc54fwith the floor's file put back to the step before took 229. The probe's own empty pthread, C code none of this touches, took 100.6 µs atce78e82, 107.4 at784b4f1, 116.3 atf1fc54f, 119.0 with 12 nops and 102.5 with 40. So where a build puts the code is worth 30 µs of a spawn on this board (section 4 measured 39), which is as much as the steps after the first appear to cost. What they cost can't be read from this probe. Atc704e27it took 261 µs, with the empty pthread at 96.9. In the suite's imagespawn_waittook 285 µs on main, 214 atce78e82and 218 atc704e27, or 212.5 with 4 KB worker stacks. Why the WROOM is this sensitive to placement isn't known. A hypothesis, as in section 4: what the flash cache holds. It wasn't tested.
The probe after the change, the suite's program again: at f1fc54f
on the S3 and on the WROOM as built, where it stops at scale_4w, and
at c704e27 on the WROOM with 4 KB worker stacks, the whole suite.
| WROOM, as built | WROOM, --worker-reserve 4K |
S3 | |
|---|---|---|---|
| copies of the globals allocated | 0 | 0 | 0 |
| a worker's data stack, reserved | 16,448 | 4,160 | 16,448 |
| written, in any worker | 0 | 0 | 0 |
| a worker's native stack, most used | 1,092 | 1,716 | 1,908 |
| internal RAM free at its lowest | 34,468 | 18,428 | 46,524 (main: 41,540) |
Main's six data stacks are as on main: 16,384, 8,000, 122, 15, 120 and 121 bytes written over the whole suite, on both boards.
Before and after. 5 boots each, built from git worktrees with paths of one length:
- S3. Before: 20261202T151352Z-s3-goose-1a8cf86-main. After: 20261202T155507Z-s3-goose-c704e27.
- WROOM. Before:
20261202T151406Z-esp32-goose-1a8cf86-main,
which stops at
scale_2w, with the timers and the fault benchmark each run alone (†) in the two folders beside it. After, as built: 20261202T161054Z-esp32-goose-c704e27, which stops atscale_4w, with the same two run alone (†). After, with--worker-reserve 4K: 20261202T161504Z-esp32-goose-c704e27-worker-reserve-4k, the first run of the whole suite on this board. - H7. Before:
20261202T143757Z-h7-goose-1a8cf86-main.
After:
20261202T144845Z-h7-goose-f1fc54f.
The last step changes only the ESP32s' port, so
f1fc54fis the change's code on the H7. c704e27holds the change's code. Later commits change a comment, the tests and the docs.- Each step's runs are in
bench/results/dev/.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,659 | 4,659 | 1.000 | 1.93 |
mandel_f64 |
166,167 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,452 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
1,688 | 1,688 | 1.000 | 1.31 |
queue_2000 |
2,827 | 2,802 | 0.991 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
210 | 153 | 0.729 | 1.89 |
scale_1w |
627,789 | 626,798 | 0.998 | 1.02 |
scale_2w |
324,018 | 322,795 | 0.996 | 1.02 |
scale_4w |
327,418 | 323,382 | 0.988 | 1.02 |
scale_main |
631,151 | 631,150 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
10 | 10 | 1.000 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 10 | 1.000 | 0.01 |
fault_contained |
404 | 353.5 | 0.875 | — |
| WROOM, medians (µs) | before | after, as built | after, with --worker-reserve 4K |
4K ÷ before | 4K ÷ c |
|---|---|---|---|---|---|
clock_1000 |
584 | 584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,541 | 5,541 | 5,541 | 1.000 | 1.94 |
mandel_f64 |
171,317 | 171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
3,041 | 2,922 | 3,030 | 0.996 | 0.64 |
pingpong |
25 | 25 | 25 | 1.000 | 1.19 |
spawn_wait |
285 | 218 | 212.5 | 0.746 | 2.17 |
scale_1w |
653,175 | 653,096 | 653,057 | 1.000 | 1.04 |
scale_2w |
stopped | 355,342 | 355,309 | — | 1.03 |
scale_4w |
stopped | stopped | 355,750 | — | 1.03 |
scale_main |
stopped | stopped | 653,093 | — | 1.04 |
timer_idle_gap |
1,000 † | 1,000 † | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
11 † | 12 † | 12 | 1.091 | 1.20 |
timer_busy_gap |
1,000 † | 1,000 † | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 † | 12 † | 12 | 1.000 | 0.01 |
fault_contained |
422 † | 336 † | 328 | 0.777 | — |
† run alone, on fresh boots, because the whole run stops before it. "stopped": the run ended before this benchmark.
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
127 | 124 | 0.976 | 0.51 |
crc32 |
1,682 | 1,682 | 1.000 | 0.69 |
heapsort |
1,777 | 1,774 | 0.998 | 2.15 |
mandel_f64 |
7,251 | 7,251 | 1.000 | 1.04 |
mandel_f32 |
3,394 | 3,394 | 1.000 | 0.92 |
matmul_f32 |
614 | 614 | 1.000 | 1.01 |
queue_2000 |
4,412 | 4,277 | 0.969 | 2.32 |
pingpong |
8 | 8 | 1.000 | 1.00 |
spawn_wait |
104 | 65 | 0.625 | 0.69 |
scale_1w |
26,585 | 26,552 | 0.999 | 1.04 |
scale_2w |
26,702 | 26,629 | 0.997 | 1.03 |
scale_4w |
26,918 | 26,767 | 0.994 | 1.03 |
scale_main |
26,479 | 26,479 | 1.000 | 1.04 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
4 | 4 | 1.000 | 0.01 |
fault_contained |
128 | 87 | 0.680 | — |
Against the other implementations.
- Spawning.
spawn_waitis now 1.89 times C's on the S3, 2.17 on the WROOM and 0.69 on the H7, against 2.59, 2.91 and 1.11 before. C creates a FreeRTOS task with the same 16 KB stack. Goose's worker is a pthread with a data stack of its own, and no longer a copy of 37 KB of globals. Rust on Embassy still starts a task in 1 to 4 µs. - Scaling on the WROOM, measured for the first time: two workers
take 0.544 of one worker's time and four take the same as two, on two
cores. Both are 1.03 times C's, as
scale_1wis 1.04. - Contained faults have no counterpart in C.
Other movements.
fault_contained: 404 to 353.5 µs on the S3, 422 to 336 on the WROOM (run alone) and 128 to 87 on the H7. Each contained fault is a spawn.- Scaling on the S3:
scale_4wtakes 1.2% less,scale_2w0.4% andscale_1w0.2%. Their workers no longer allocate and zero a copy of the globals. Where each copy landed on main wasn't traced.scale_2wis now below what it was before section 8 cost it 0.3% (322,795 µs against 323,000). queue_2000: 3.1% less on the H7 (4,412 to 4,277 µs, every boot). Not explained. On the ESP32s its boots' medians overlap before and after (2,790 to 2,827 against 2,780 to 2,828 on the S3).- The H7's
clock_1000took 124 µs against 127 and itsheapsort1,774 against 1,777, in every boot, on code the change doesn't touch. Not explained. As in earlier sections, a build that moves code moves these by a few cycles a loop. - The WROOM's
timer_idle_wake, run alone: boots' medians of 12 µs five times, against 12, 11, 11, 11, 11. Each wake is 11 or 12 µs before and after. In the whole-suite run with 4 KB worker stacks one boot'stimer_busy_wakehad a median of 18 µs, with the others at 11 and 12. Neither is explained. - The S3's timer wakes, in single boots. In the run at
c704e27one boot took 14 µs at every one of its 1,000 idle wakes, where the other four took 10. Another boot's median beside a busy worker was 13, where the other four's was 10. Main's boots were at 9 or 10 idle and all at 10 busy, and so were the five boots atf1fc54f. Not explained. This was left out of the section when it was first published. - Finished workers held longer on the S3.
spawn_reclaimspawns and waits 1,000 times and reports the free heap's lowest point. On main that was 16,764 bytes below the start on the S3: one finished worker not yet freed by the idle task. After the change it was 82,660, five of them. Spawns come faster and the idle task runs as often as it did. The memory comes back each time, and a spawn that needs it waits for it. On the WROOM it was 66,168 bytes before and after, four of them. On the H7, which frees a finished worker at the next spawn, 16,704 and 16,672. region_shrinkends with a different error on the WROOM. It holds workers until memory is nearly gone and starts one more. On main the port refused that worker's data stack with 72,236 bytes free. Now four workers are held, 39,804 bytes are free, and the next worker's native stack finds no block:cannot create thread, after the wait. The S3 ends that way before and after, and the H7 with its data stack refused, 10 workers held, before and after.- The H7's
clock_costs: its reader alone made 3.7% fewer reads of the clock in its 18 s (18,311,639 against 19,022,547), and its worker 3.1% more beside the ticks. The suite'sclock_1000went the other way. Not explained. - The image is 48 bytes smaller on the S3 (262,112 to 262,064), 32 larger on the WROOM (243,120 to 243,152) and 16 larger on the H7 (58,000 to 58,016). Static RAM is the same on the S3, 8 bytes more on the WROOM and 16 more on the H7. The suite's free heap at its start is the same on the S3, 8 bytes lower on the WROOM (131,520) and 32 lower on the H7 (362,664).
Checks.
- Results: every benchmark's result matches C's, in every boot.
tests/stress/worker_globals.goose(new) has workers that use no global, the first scalar, the first and the last, an array in the middle, a global with a data stack of its own, the two ends of the struct, and one that spawns a worker of its own. It checks what each reads and writes, that main's globals never change, and that the heap comes back, over 800 spawns. It passes on main and on the change on all three boards, and with--worker-reserve 4Kon the ESP32s and 1 KB on the H7. On main the H7 ran it before its heap check allowed for the runtime's pools (below), which that board didn't need.tests/stress/low_heap.goose(new), on the WROOM: as built it holds 4 workers with 49,972 bytes free, and with 4 KB worker stacks 8 with 15,420. With that left it passes 200 messages through a queue, takes 50 ticks of a timer, writes and reads a file and opens a UART, then takes its workers again 20 times. On main the port refuses its fourth worker's data stack, which ends the program with 65,768 bytes free.- An address no board maps. A read or a write at the address a
worker without globals is given ends the program on each board:
LoadProhibitedandStoreProhibitedon the ESP32s, a bus fault on the H7. - The free heap after spawns. In both new tests the heap ends up to
1,548 bytes below where it started, on main as on the change. It is
the runtime's own pools: the queue's node cache takes chunks of 512
bytes, 8 at most, and each wait going on at once keeps a 96-byte
semaphore. Over 1,000 rounds of
worker_globalson the WROOM the heap went down by 516 bytes five times, the last by the 512th round. - Conformance:
tests/conformance.pypasses 24 of 24 on the S3, 21 of 21 on the WROOM and 22 of 22 on the H7, on the boards. The H7's also passes 22 of 22 in Renode. - Stress programs, against main built in the same worktree: on the
S3 and the WROOM at
c704e27, and on the H7 at the branch's head then (cd73810), where that board's code isf1fc54f's:sync_stress,qnode_stressandpingpong_costspass. None ofsync_stress's 60 timed waits ended early. The H7's two were run atf1fc54f.spawn_costs: above. Itsmemsetcheck passed.abort_records: its four rounds cost 492, 512, 1,024 and 0 bytes on the S3 and 528, 480, 992 and -32 on the WROOM. On main, 480, 992 and -32 after a first reading of -81,968 on the S3, and 492, 548, 1,092 and -32 on the WROOM. The first reading on main had finished workers' stacks in it. On the H7, 492, 512, 1,024 and 0 against 496, 512, 1,024 and 0.spawn_reclaimandregion_shrink: above.clock_costs: no read of the clock went back, and none of its 4,000 or 4,001 ticks was dropped or out of order.delay_early: none of its waits ended early.
- The H7's board runs came in two parts. Its ST-Link stopped
answering during the last round of runs and was replugged. Before
that, at
f1fc54f, the board ran the suite (5 boots),spawn_costs,worker_globals, the read at the address above,sync_stressandqnode_stress. After it, atcd73810, it ran the other seven stress programs,worker_globalsas committed, the write, and the conformance samples one at a time with 20 s between flashes. In between, conformance,spawn_reclaimandregion_shrinkran in Renode and ended the same way. - Unit tests:
pytestpasses 94 tests, with 24 skipped that need a C compiler on the host, which CI has. The new ones cover the part each worker's entry takes and the cases that must keep a whole copy, with the Goose compiler and without it, and the size of workers' stacks. - Raw data. Every probe, stress, conformance and experiment log behind this section is in bench/results/probes/20261202-memory.
What it leaves.
- As built, the WROOM still stops at
scale_4w. Four workers with 16 KB data stacks don't fit beside main's six. It takes--worker-reserve, which a program has to be given. - Main's stacks are still one size. They reserve 98,688 bytes and the whole suite writes 24,762 of them, 16,384 in the one moment a global is built. A program can't say which of its stacks need room.
- No report of what a stack used. The numbers here come from an
experiment patch. A program's author has nothing to size
--worker-reserveby but an overflow. - A part is one run of bytes. A worker that uses the first and the last global gets everything between them too.
- A networked program on the WROOM still leaves 64 KB. What WiFi needs there wasn't measured.
- The WROOM's spawn probe moves by 30 µs with where a build puts its code. The cause isn't found.
- Finished workers' stacks wait for the idle task: five at once on the S3 in a loop of spawns, 82,660 bytes. A spawn that needs the room waits up to five ticks for it.
10. A worker's native stack, and a stack report¶
Cause. Section 9 left two things open:
a program had no report of what its stacks used, and a worker's native
stack was 16 KB whatever the worker did. On the WROOM
tests/stress/heap_needs.goose has a waiting worker with no data stack
hold 16,868 bytes, and tests/stress/low_heap.goose, built with
--worker-reserve 4K, 20,932 for each of its workers: 8 fit above the
floor. Section 9's probe had found the suite's workers using 1.8 KB of
their native stacks at most.
Change. Two commits, on every board unless said, and two more to the
report itself (6ffe495, 6aa1273), which the first bullet's account
includes:
--worker-stack,--stack-reportandstack_report()(1bc11cb).--worker-stack SIZEis the native stack of eachthread_spawnworker:worker_native_stackinmicrogoose/core.py, a patch to the runtime's thread start, which sets the size on the thread's attributes where the program's C names one. At least 2K, in multiples of 16 bytes. Without it nothing changes: 16 KB.--stack-reporthas the port fill each data stack with a pattern when it is reserved and read back how far it was written when it is released. A worker's native stack is read as the worker ends, by the fill FreeRTOS gives every new stack: on the ESP32s the worker's thread starts in the port and comes back to it when the worker's function returns. The report is printed when the program ends and atstack_report(), a new call inmcu. Built without the option, a program pays for none of this, andstack_report()prints the main program's native stack alone.- A native stack that overflows says so, on the ESP32s. A worker
with a 2 KB stack that wrote a file crashed as an
IllegalInstructionwith a corrupted backtrace: its calls had run down into its own data stack, and the file's text was written over their return addresses. FreeRTOS checks a stack's last bytes when the task is switched out, and the crash came first. When the board crashes, the port now looks at the task running on each core: with fewer than 256 bytes of its stack still holding FreeRTOS's fill, it printsnative call stack overflow, most likely, and names--worker-stackwhen the task is a worker. That worker had 128 such bytes on the S3 and none on the WROOM: a call's frame doesn't write every byte it takes, so the lowest bytes alone don't tell. The H7's port already guards each native stack with the MPU and ends the program withnative call stack overflow.
- A worker's data stack in a block the next one can take (
0a78d44, the ESP32s). This one is a correctness fix the first made necessary.- The defect. With
--worker-reserve 4K --worker-stack 4K,low_heapheld 19 workers of 8,644 bytes. When all 19 ended and were taken again at once, three of the new ones were refused their data stacks, which ends the program, with over 29 KB free. A build that prints the heap at a refusal showed 29,712 bytes free and a largest free block of 4,096, and a 4 KB data stack takes 4,160. With 6 KB and 8 KB native stacks it ended the same way: 16 and 13 workers, 14,532 and 19,956 bytes free, the largest block 4,096 each time. - Why. A data stack was an aligned allocation. For one, ESP-IDF's heap (TLSF) takes a block of the size plus the alignment and a block header, then gives the extra back, so the hole a freed data stack leaves is the size alone, and the same call doesn't fit it. And the heap looks for a free block in the size class at or above the request's: 32 classes to each power of two. While old workers' native stacks were still waiting to be freed, the new workers' native stacks took what was free, and the data stacks' own holes were all that was left for the data stacks.
- Not the wait. A refused data stack already waits five ticks for finished workers' memory. Waiting a tick more for every task there is (25 to 31 ticks) changed nothing: the memory came back, in blocks of that size. That experiment's logs are with the others.
- The fix. A worker's data stack is a plain block, with the stack placed at a 64-byte boundary inside it and the block's own address kept in the word below the stack. The heap gives a plain block its class's size, and finds it again for the next request of that size. The main program's stacks last as long as it does and leave no hole to fill, so they are allocated as before.
- The defect. With
What a stack holds. tests/stress/stack_use.goose (new) runs five
kinds of worker one after another and asks for the report after each. On
the boards, built with --stack-report:
| bytes used | S3 | WROOM | H7, in Renode |
|---|---|---|---|
| the main program's native stack | 1,512 of 24,576 | 1,092 of 24,576 | 648 of 121,824 |
| the main program's five data stacks | 66, 19, 21, 0, 0 of 16,384 | the same | 68, 19, 21, 0, 0 |
| a worker that does nothing: its native stack | 916 | 692 | 376 |
| one that builds a 670-byte text and prints it | 1,284 | 1,076 | 488 |
| one that writes a file and reads it back | 3,284 | 3,060 | 2,464 |
| one that faults and is contained, and one that calls itself 40 deep | no more | no more | no more |
| a worker's data stack, at most | 670 of 16,384 | 670 | 670 |
With --worker-stack 4K the program passes on all three (3,292, 3,060
and 2,464 of 4,096 used). With 2K it ends at the worker that writes a
file, on all three, each with the report above.
What a worker holds, on the WROOM.
| as built | --worker-stack 8K |
6K |
4K |
|
|---|---|---|---|---|
a waiting worker with no data stack (heap_needs), bytes |
16,868 | 4,580 | ||
a worker of low_heap, with --worker-reserve 4K, bytes |
21,124 | 12,932 | 10,884 | 8,836 |
workers low_heap holds above the floor |
8 | 13 | 15 | 19 |
| all of them ended and taken again at once, 20 times | passes | passes | passes | passes |
| the same before the second commit | passes (8 of 20,932) | ends: a data stack refused | ends | ends |
- A worker's data stack takes more: 192 bytes at 4 KB (8,836 against 8,644, and 21,124 against 20,932), which is its class's size, 4,352, against the 4,160 the aligned call kept. At 16 KB the class's size is 448 more. That one wasn't measured on its own.
Before and after. The whole suite, 5 boots each, medians. Before is
main with the pin benchmarks (8388422), measured the same day.
- S3. Before: 20261204T225518Z-s3-goose-8388422-pins-tip. After: 20261204T213708Z-s3-goose-0a78d44-worker-stack-branch.
- WROOM, built with
--worker-reserve 4K, which is how it runs the whole suite. Before: 20261204T230054Z-esp32-goose-8388422-pins-tip-worker-reserve-4k. After: 20261204T215356Z-esp32-goose-0a78d44-worker-reserve-4k-branch, and with--worker-stack 4Kbeside it: 20261204T214509Z-esp32-goose-0a78d44-worker-reserve-4k-worker-stack-4k. section 11in both tables is the suite at the code of section 11,5b709e0, inbench/results/dev/too.- The H7 wasn't run: its debug probe had stopped answering.
- These runs aren't in Results, whose tables are the runs the report was written from.
| S3, medians | before (µs) | after (µs) | section 11 (µs) | after ÷ before | section 11 ÷ before |
|---|---|---|---|---|---|
clock_1000 |
855 | 855 | 855 | 1.000 | 1.000 |
crc32 |
3,567 | 3,567 | 3,567 | 1.000 | 1.000 |
heapsort |
4,659 | 4,659 | 4,659 | 1.000 | 1.000 |
mandel_f64 |
166,167 | 166,167 | 166,167 | 1.000 | 1.000 |
mandel_f32 |
7,452 | 7,452 | 7,452 | 1.000 | 1.000 |
matmul_f32 |
1,688 | 1,688 | 1,688 | 1.000 | 1.000 |
queue_2000 |
2,805 | 2,810 | 2,820 | 1.002 | 1.005 |
pingpong |
25 | 25 | 25 | 1.000 | 1.000 |
spawn_wait |
162 | 122 | 125 | 0.753 | 0.772 |
scale_main |
630,266 | 630,266 | 630,228 | 1.000 | 1.000 |
scale_1w |
626,828 | 626,797 | 626,750 | 1.000 | 1.000 |
scale_2w |
322,816 | 322,753 | 322,717 | 1.000 | 1.000 |
scale_4w |
323,444 | 323,359 | 323,304 | 1.000 | 1.000 |
timer_idle_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.000 |
timer_idle_wake |
10 | 10 | 10 | 1.000 | 1.000 |
timer_busy_gap |
1,000 | 1,000 | 1,000 | 1.000 | 1.000 |
timer_busy_wake |
10 | 10 | 10 | 1.000 | 1.000 |
pin_idle_gap |
1,003 | 1,003 | 1,003 | 1.000 | 1.000 |
pin_idle_wake |
10 | 10 | 10 | 1.000 | 1.000 |
pin_busy_gap |
1,003 | 1,003 | 1,003 | 1.000 | 1.000 |
pin_busy_wake |
11 | 11 | 11 | 1.000 | 1.000 |
fault_contained |
357 | 361.5 | 326 | 1.013 | 0.913 |
WROOM, --worker-reserve 4K, medians |
before (µs) | after (µs) | section 11 (µs) | after, 4 KB native (µs) | after ÷ before | section 11 ÷ before | after, 4 KB native ÷ before |
|---|---|---|---|---|---|---|---|
clock_1000 |
584 | 584 | 584 | 584 | 1.000 | 1.000 | 1.000 |
crc32 |
3,702 | 3,702 | 3,702 | 3,702 | 1.000 | 1.000 | 1.000 |
heapsort |
5,544 | 5,541 | 5,544 | 5,541 | 0.999 | 1.000 | 0.999 |
mandel_f64 |
171,317 | 171,316 | 171,316 | 171,316 | 1.000 | 1.000 | 1.000 |
mandel_f32 |
8,425 | 8,425 | 8,425 | 8,425 | 1.000 | 1.000 | 1.000 |
matmul_f32 |
1,961 | 1,961 | 1,961 | 1,961 | 1.000 | 1.000 | 1.000 |
queue_2000 |
2,959 | 2,985 | 3,019 | 2,790 | 1.009 | 1.020 | 0.943 |
pingpong |
25 | 25 | 25 | 25 | 1.000 | 1.000 | 1.000 |
spawn_wait |
207 | 182 | 172.5 | 161 | 0.879 | 0.833 | 0.778 |
scale_main |
648,505 | 648,505 | 647,585 | 648,505 | 1.000 | 0.999 | 1.000 |
scale_1w |
653,063 | 653,025 | 653,010 | 653,006 | 1.000 | 1.000 | 1.000 |
scale_2w |
355,286 | 355,239 | 355,230 | 355,436 | 1.000 | 1.000 | 1.000 |
scale_4w |
355,795 | 355,342 | 355,696 | 355,551 | 0.999 | 1.000 | 0.999 |
timer_idle_gap |
1,000 | 1,000 | 1,000 | 1,000 | 1.000 | 1.000 | 1.000 |
timer_idle_wake |
12 | 11 | 12 | 11 | 0.917 | 1.000 | 0.917 |
timer_busy_gap |
1,000 | 1,000 | 1,000 | 1,000 | 1.000 | 1.000 | 1.000 |
timer_busy_wake |
12 | 12 | 12 | 12 | 1.000 | 1.000 | 1.000 |
pin_idle_gap |
1,002 | 1,002 | 1,002 | 1,002 | 1.000 | 1.000 | 1.000 |
pin_idle_wake |
12 | 12 | 12 | 12 | 1.000 | 1.000 | 1.000 |
pin_busy_gap |
1,002 | 1,001 | 1,002 | 1,002 | 0.999 | 1.000 | 1.000 |
pin_busy_wake |
12 | 12 | 12 | 12 | 1.000 | 1.000 | 1.000 |
fault_contained |
327 | 293 | 309 | 275 | 0.896 | 0.945 | 0.841 |
What moved:
- A spawn is faster:
spawn_waittakes 0.75 of its time on the S3 (122 µs against 162) and 0.88 on the WROOM (182 against 207). Before each aligned allocation the port asked the heap for its largest free block, which walks the heap, and for a worker's plain block it doesn't. That step wasn't timed on its own. With 4 KB native stacks a spawn on the WROOM takes 161 µs. fault_containedmoved both ways between builds: 357, 361.5 and 326 µs on the S3, and 327, 293 and 309 on the WROOM. It holds a spawn, which accounts for part of the WROOM's first step. The rest isn't explained: its boots agree within 3% in every run, so it is the build that decides, as with the 30 µs section 9 left unexplained.queue_2000stays within what its boots differ by: 2,779 to 2,826 µs before on the S3 and 2,791 to 2,830 after, and 2,698 to 3,022 before on the WROOM and 2,771 to 3,176 after.- Everything else is within 0.2% of before, or 1 µs.
- The free heap when the suite starts on the WROOM was 1,168 bytes
lower at
0a78d44(128,416 against 129,584). 896 of them were a copy of the table of data stacks that the report kept for itself. A later commit (6ffe495) reads the table in place: at the final code (6aa1273) the suite starts with 129,256 bytes there, 328 fewer than before, and 320 fewer on the S3. - One boot of five waited for finished workers' stacks, in the
WROOM's run at
5b709e0: every fifth spawn ofspawn_waittook 2 to 3 ms, nine in all (9 stacks waited for finished workers to be freed). That is section 9's wait doing its work. No other boot of these runs waited, before or after, and what set that boot apart isn't known.
Checks.
- Results: every benchmark's result matches C's, in every boot.
stack_usepasses on the S3 and the WROOM, as built and with--worker-stack 4K, with and without--stack-report, and on the H7 in Renode. With 2K it ends with the overflow report on all three.low_heappasses on the WROOM with--worker-reserve 4Kalone and with 8K, 6K and 4K native stacks beside it. The logs of the same runs before the second commit are inbefore-the-blocks/.heap_needs,worker_globals,spawn_reclaimandabort_recordspass on both ESP32s, andregion_shrinkends there as on main, at its last spawn.- Conformance: 24 of 24 samples on the S3 in QEMU and 22 of 22 on the
H7 in Renode, at the code of section 11.
In Renode
10_sudokuneeded 400 seconds allowed, not 60, while other builds were running on the PC. - The unit tests hold
--worker-stack's path from the command line to the runtime's patch, and its floor. - Logs:
bench/results/probes/20261204-worker-stacks/.
With Goose v2026.5. Main moved to Goose v2026.5 the same day, and this
work was merged with it (3ee980a, then cf2516d). Every run above names
its commit, and those build with v2026.4. On the merged code, on the two
ESP32s:
stack_use: the worker that writes a file uses 2,836 bytes of native stack on the S3 and 2,612 on the WROOM, where it used 3,284 and 3,060. The other workers are within 32 bytes of before.- A worker ran past its stack and nothing noticed. Built with
--worker-stack 2K, the program passed on the S3. The report read 1,908 of 2,048 for the workers' native stack, and 16,366 of 16,384 for the main program's first data stack, which holds 66: the file writer's calls had run 0.8 KB past the end of its stack and into that one. The WROOM's run of the same build crashed and said why, as before. A native stack read within 256 bytes of its size is now marked(too close to tell: it may have run past the end)(8e1c4c5). low_heapon the WROOM: 8 workers of 21,240 bytes, and 19 of 8,952 with 4 KB native stacks, 116 bytes more each than with v2026.4. Both are taken again 20 times.- The suite, 5 boots each
(S3,
WROOM):
spawn_waittakes 121 µs on the S3 and 156 on the WROOM, andfault_contained241 and 274. The timer and pin wakes are as above. matmul_f32was wrong in those two runs: a check of 0 where C prints 5, in 52 and 137 µs. This is a defect of main's step to v2026.5, not of this section. The compiler now writes an in-order sum whose count is a whole number of blocks as the blocked loop alone, and section 6's change turned that loop off, so the sum skipped every term. The fix (04800bf) leaves such a loop as the compiler wrote it. With it the check is 5 again andmatmul_f32takes 3,870 µs on the S3 and 4,976 on the WROOM, against 1,688 and 1,961 with v2026.4: section 6's gain was lost for sums of that shape until section 12 rewrote the blocked loop instead of leaving it.- Logs: the
merged/folder beside the others, and the runs namedmerged,sums-fixandfinalinbench/results/dev/.
What it leaves.
- 16 KB is still what a worker gets. A smaller default would break a
worker with a deep call chain, and what a worker needs can't be told
from its source. A program has to be given
--worker-stack. - The report is what one run used. Another run can take a path that uses more. It is a number to size by with room to spare, not a bound.
- Nothing stops a native stack's overflow on the ESP32s. It is reported only if the board crashes with that task running. The 2 KB worker above ran 0.8 KB past its stack, over the main program's data, and the program passed. Only a guard the chip enforces would stop that, and each core's two watchpoints go to the main program's data stacks.
matmul_f32with Goose v2026.5 took 2.3 to 2.5 times what it took with v2026.4, from the fix above until section 12.- The H7 on the board. Its debug probe stopped answering on 2026-12-04, so the H7's part of this section ran in Renode only.
- The H7's data stacks are still aligned allocations from newlib's
malloc. Whether a nearly full heap refuses them there the same way wasn't looked at. - Main's stacks are still one size. The report now shows what each
holds: 66 bytes at most in
stack_use, of 16,384 each.
11. Events ahead of the workers¶
Cause. Section 8 left one case as
it was: with a worker computing on each core of an ESP32, an event waits
for the scheduler's tick, up to a millisecond, "as in C". In C the answer
is a line of code: the task that waits for the event runs at a higher
priority than the tasks that compute. A Goose program couldn't say that.
Every thread of a program runs at one priority, 5 on the ESP32s. Section
8 tried raising the waiting thread inside event_wait and lowering it
again before the program's code ran: it got the tick at once and the core
late, 660 to 950 µs after the tick beside two workers.
Change. One commit (ddd7b35): task_priority(up), a new call in
mcu, on every board.
- What it does. It sets the calling thread's priority
upsteps above the one threads start at and returns the level it set: 0 to 3 on the ESP32s, under the tasks ESP-IDF runs its own work in, and 0 or 1 on the H7, whose kernel has four priorities.task_priority(0)puts the thread back. The thread stays where it was put while it runs the program's code, which is what section 8's attempt couldn't do. - On the ESP32s nothing else changes. The interrupt that queues an event wakes the waiting thread, FreeRTOS sees a task of a higher priority than the one running on that core, and switches to it as the interrupt returns. The hand-over of section 8 has nothing to do then: the interrupt found no task in the way.
- On the H7 a waiting thread is already raised for as long as it waits, and the port now raises it from its own level and returns it there. A thread one step up keeps the chip from the workers until it next waits. At 0 it shares the chip with them again at the next tick.
- The cost is the program's to choose. While a raised thread runs, a worker on its core doesn't. The port doesn't raise any thread by itself.
A second commit (5b709e0) gives the benchmark harness two options for
measuring this, in Goose and C: --prio-up N runs the suite's main
program N priorities above its workers, and --busy N spins N workers
beside timer_busy and pin_busy. --only events is the timers and the
pins together. Runs with either option aren't part of the published
tables, which keep priority 0 and one busy worker: they are in
bench/results/priority/.
Before and after. The suite's timers and pins, 3 boots of 1,000
events each, at the change's code: the waiting program at its workers'
priority and one step up, beside one busy worker and two. Medians in µs,
with the slowest of the 3,000 after the comma. C is the same suite with
its main task raised the same way (vTaskPrioritySet), which is what a C
programmer would write.
| ESP32-S3: median, slowest (µs) | Goose at 0 | Goose one step up | C at 0 | C one step up |
|---|---|---|---|---|
timer_busy_wake, one busy worker |
10, 10 | 10, 10 | 1,365.5, 1,865 | 9, 9 |
pin_busy_wake, one busy worker |
11, 18 | 11, 15 | 469.5, 1,989 | 9, 13 |
timer_busy_wake, two busy workers |
1,075.5, 1,564 | 10, 10 | 1,365, 1,864 | 8, 9 |
pin_busy_wake, two busy workers |
1,000, 2,008 | 11, 15 | 1,000, 2,000 | 9, 13 |
timer_idle_wake, nothing else running |
10, 10 | 10, 10 | 8, 8 | 9, 9 |
pin_idle_wake, nothing else running |
10, 15 | 10, 15 | 8, 13 | 8, 13 |
| ESP32 WROOM-32: median, slowest (µs) | Goose at 0 | Goose one step up | C at 0 | C one step up |
|---|---|---|---|---|
timer_busy_wake, one busy worker |
12, 12 | 11, 12 | 1,344.5, 1,844 | 10, 11 |
pin_busy_wake, one busy worker |
12, 216 | 12, 17 | 422, 1,986 | 10, 14 |
timer_busy_wake, two busy workers |
759.5, 1,248 | 12, 12 | 1,351.5, 1,851 | 10, 10 |
pin_busy_wake, two busy workers |
998, 2,009 | 12, 17 | 997, 2,005 | 10, 14 |
timer_idle_wake, nothing else running |
12, 12 | 12, 12 | 10, 10 | 10, 10 |
pin_idle_wake, nothing else running |
12, 17 | 12, 17 | 10, 14 | 10, 14 |
Checks.
-
tests/stress/event_prio.goose(new) takes 1,000 ticks of a 1,003 µs timer with 0, 1 and 2 workers spinning, at priority 0 and one step up, and passes if no wake one step up took 100 µs or more:median, p99, slowest (µs); wakes of 100 µs or more in 1,000 S3 at 0 S3 one step up WROOM at 0 WROOM one step up no worker 10, 13, 14; 0 10, 13, 14; 0 11, 14, 15; 0 12, 14, 16; 0 one busy worker 10, 13, 17; 0 10, 13, 14; 0 12, 14, 556; 1 11, 13, 16; 0 two busy workers 999, 1,975, 2,001; 948 10, 13, 15; 0 1,007, 1,977, 2,007; 947 12, 14, 17; 0 -
The published configuration didn't move. The whole suite at priority 0 with one busy worker, 5 boots on each ESP32 at the final code (
6aa1273, the runs namedtipinbench/results/dev/): the timer and pin wakes are what they were before section 10, 10 and 11 µs on the S3 and 12 on the WROOM, no benchmark is slower than before, and no event was missed in any run here.spawn_waitis at 0.79 and 0.90 of before andfault_containedat 0.82 and 0.97, which is section 10's doing. - Results: every benchmark's result matches C's, in every boot.
- Conformance: 24 of 24 samples on the S3 in QEMU and 22 of 22 on the
H7 in Renode, at
5b709e0, and again on the code merged with Goose v2026.5 (3ee980a). - With Goose v2026.5,
event_priopasses on both boards again: one step up beside two busy workers, a median of 10 µs on the S3 and 12 on the WROOM, 15 and 16 at the slowest. Those logs are inmerged/. - Logs:
bench/results/probes/20261204-priority/and the run folders inbench/results/priority/.
What it leaves.
- A program has to ask. At priority 0 nothing changes: with both cores busy an event still waits for the tick.
- Goose is 1 to 2 µs behind C here too, as in the idle wakes.
- Priority and locks. A raised thread that waits for a lock a worker holds, a queue's or the console's, waits for that worker. The runtime's locks don't lend a priority. No test here looks for an inversion.
- The H7 on the board. Its debug probe stopped answering on
2026-12-04, so
event_prioand these runs weren't made there. The H7's part of the change built and ran in Renode, whose clock is too coarse to time a wake. - Rust, TinyGo and MicroPython weren't run with a raised waiter.
12. Sums in blocks alone, with Goose v2026.5¶
Cause. Main moved to Goose v2026.5 on 2026-12-04. That version writes
an in-order sum whose count is a whole number of blocks, such as
matmul's for k in MAT_N with MAT_N 32, as its blocked loop alone,
with no one-at-a-time loop after it.
Section 6's change turned
every blocked loop off, so such a sum skipped all its terms. The fix
(04800bf, section 10
has the account) left a loop of that kind as the compiler wrote it:
right, and as slow as section 6 measured that shape. On main (dd0baa9)
matmul_f32 took 3,870 µs on the S3, 4,976 on the WROOM and 969 on the
H7, against 1,688, 1,961 and 614 with v2026.4. Section 6's probe had C in
blocks of 8 with int64_t counters at 3,980, 5,112 and 972.
Change. Two commits, 76c1c42 and 181987f, in
microgoose/core.py, for every board. plain_sums rewrites a sum
written as blocks alone into the loop v2026.4 wrote after its blocks:
the term's own lines, then the term added to the sum. matmul's sum as
v2026.5 writes it:
/* loop shape: sum terms in blocks of 8 */
while (t204 - k_g_2 >= 8) {
gs_f32 t208[8];
for (int64_t t207 = 0; t207 < 8; t207++, k_g_2++) {
t209 = ...; /* the index into MA */
t210 = ...; /* the index into MB */
t208[t207] = ((gs_f32)((gs_f32)(GS_GL->MA_g.e[t209])) * (gs_f32)(GS_GL->MB_g.e[t210]));
}
for (int64_t t211 = 0; t211 < 8; t211++) s_g_5 = s_g_5 + t208[t211];
}
and as MicroGoose now compiles it:
/* loop shape: sum terms in blocks of 8, run one at a time by microgoose */
for (; k_g_2 < t204; k_g_2++) {
t209 = ...;
t210 = ...;
s_g_5 = (s_g_5 + (gs_f32)(((gs_f32)((gs_f32)(GS_GL->MA_g.e[t209])) * (gs_f32)(GS_GL->MB_g.e[t210]))));
}
The terms are worked out and added in the same order, so every rounding
and every result is the same. Each term is converted to the block's
element type, as storing it in the block did (181987f): mat_check
adds f32 values to an f64 sum through an f64 block. A blocked loop
of any other shape, or one whose term uses the block's own counter or
array, is still left as the compiler wrote it. A blocked loop with a
one-at-a-time copy after it is turned off, as before.
In the suite two loops change, matmul's sum and mat_check's, which
isn't timed. The suite's generated C differs from main's in those two
loops and nothing else. Of the other 57 programs compiled with v2026.5,
Goose's samples and tests/stress, only the probe loops_costs changes,
in its copies of the same two loops. 31_mini_doom's four sums have
their one-at-a-time copies and are turned off as before.
Before and after. 5 boots each with Goose v2026.5, built from git worktrees with paths of the same length.
- S3. Before: 20261205T013553Z-s3-goose-dd0baa9-main. After: 20261205T014108Z-s3-goose-181987f.
- WROOM. Before:
20261205T014626Z-esp32-goose-dd0baa9-main.
After:
20261205T015150Z-esp32-goose-181987f,
and with
--worker-reserve 4K20261205T015921Z-esp32-goose-181987f-worker-reserve-4k. Every boot of these two runs got through the whole suite,scale_4wincluded, with 16 KB worker stacks as built. That is near the WROOM's limit: one boot of the check run below was refusedscale_4w's threads. Runs of thetimers,faultsandpinsgroups alone, made in case the full run stopped early, weren't needed and are inbench/results/dev/. - H7. Before: 20261205T010529Z-h7-goose-dd0baa9-main. After: 20261205T011351Z-h7-goose-181987f. The H7's debug probe answers again.
76c1c42is the change without181987f's conversion, which makes no difference to these two loops' types. Its H7 run is inbench/results/dev/(matmul_f32614 µs).
These are now the latest runs on each board, so Results and the report's tables show Goose v2026.5 from here on.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,815 | 4,659 | 0.968 | 1.93 |
mandel_f64 |
166,166 | 166,166 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
3,870 | 1,689 | 0.436 | 1.31 |
queue_2000 |
2,752 | 2,796 | 1.016 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
129 | 137 | 1.062 | 1.69 |
scale_main |
630,228 | 630,228 | 1.000 | 1.03 |
scale_1w |
625,862 | 625,843 | 1.000 | 1.02 |
scale_2w |
321,770 | 321,756 | 1.000 | 1.02 |
scale_4w |
322,631 | 322,878 | 1.001 | 1.02 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
10 | 10 | 1.000 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 10 | 1.000 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
10 | 10 | 1.000 | 1.25 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
11 | 11 | 1.000 | 0.02 |
fault_contained |
234 | 231 | 0.987 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,614 | 5,544 | 0.988 | 1.94 |
mandel_f64 |
171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,432 | 8,425 | 0.999 | 1.08 |
matmul_f32 |
4,976 | 1,961 | 0.394 | 1.37 |
queue_2000 |
2,723 | 2,869 | 1.054 | 0.61 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
156 | 156 | 1.000 | 1.59 |
scale_main |
647,585 | 647,585 | 1.000 | 1.03 |
scale_1w |
652,942 | 652,933 | 1.000 | 1.04 |
scale_2w |
355,156 | 355,160 | 1.000 | 1.03 |
scale_4w |
355,534 | 355,536 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
12 | 11 | 0.917 | 1.10 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 | 12 | 1.000 | 0.01 |
pin_idle_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_idle_wake |
12 | 12 | 1.000 | 1.20 |
pin_busy_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_busy_wake |
12 | 12 | 1.000 | 0.02 |
fault_contained |
282 | 292 | 1.035 | — |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
122 | 122 | 1.000 | 0.50 |
crc32 |
1,682 | 1,682 | 1.000 | 0.69 |
heapsort |
1,782 | 1,748 | 0.981 | 2.12 |
mandel_f64 |
7,258 | 7,251 | 0.999 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
969 | 614 | 0.634 | 1.01 |
queue_2000 |
4,385 | 4,281 | 0.976 | 2.33 |
pingpong |
8 | 8 | 1.000 | 1.00 |
spawn_wait |
66 | 65 | 0.985 | 0.69 |
scale_main |
26,472 | 26,480 | 1.000 | 1.04 |
scale_1w |
26,556 | 26,554 | 1.000 | 1.04 |
scale_2w |
26,634 | 26,629 | 1.000 | 1.03 |
scale_4w |
26,769 | 26,764 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
0 | 1 | — | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
4 | 4 | 1.000 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | — |
pin_idle_wake |
1 | 1 | 1.000 | — |
pin_busy_gap |
1,003 | 1,003 | 1.000 | — |
pin_busy_wake |
5 | 5 | 1.000 | — |
fault_contained |
86 | 85 | 0.988 | — |
What moved.
matmul_f32takes 0.436 of its time on the S3, 0.394 on the WROOM and 0.634 on the H7: 1,689, 1,961 and 614 µs, what it took with v2026.4. Its check is 5 in every boot, as C's.heapsortis faster on every board (0.968, 0.988 and 0.981), and its code didn't change. Its time is the same in every boot of an image and moves from one image to the next, as section 6 found: on the S3 it took 4,327, 4,659 and 4,815 µs in the runs of 2026-12-04 and 05.queue_2000takes 1.016 and 1.054 of before on the ESP32s and 0.976 on the H7. Its boots differ more than that: on the WROOM their medians were 2,720 to 2,747 µs before and 2,759 to 3,009 after, and 2,720 to 3,263 across the ESP32 runs of those two days.spawn_waiton the S3: 137 µs against 129, with the boots' medians at 128.5 to 137 after and 124 to 137 before.fault_containedon the WROOM takes 292 µs against 282, in every boot. That code didn't change either, and the WROOM's runs of the last two days put it anywhere from 274 to 316 µs.- Slow spawns on the WROOM. In 4 of the 20 boots made after the
change, one spawn took 2,153 to 4,303 µs, early in
spawn_wait's series. The median stays at 156 µs, and no other boot's slowest spawn took more than 356. None of the 15 boots on main had one: their slowest took 312. The 20 boots are the two runs above and a check run of 10; the 15 are main's run above and a check run of 10 (main, after). The change doesn't touch spawning, and the console shows nothing at those moments. What causes them isn't known, and 4 boots in 20 against none in 15 is too few to say the change does. scale_4wrefused on the WROOM, once. Boot 8 of the check run after the change stopped atscale_4w: ESP-IDF couldn't create its workers' threads, and 41,120 bytes were free when the program ended. In the other 14 boots as built after the change, and in all 15 on main, the suite ran whole. Four workers with 16 KB native stacks and 16 KB data stacks fit, but only just.
Checks.
- Results: every benchmark's result matches C's, in every boot of the runs in the tables above.
- Unit tests: 196 pass. The new ones check
matmul's andmat_check's sums as v2026.5 writes them, word for word after the rewrite, and that a loop of another shape is left running. - Conformance and stress: their generated C is the same as main's, so the runs made on main with v2026.5 stand. CI runs the conformance samples in QEMU and Renode on this change.
What it leaves.
- The slow spawns and the refused
scale_4won the WROOM, above, unexplained. - One shape. A sum the compiler writes as blocks alone in another
shape runs as it is written: right, and slower on these boards. The
unit tests compile such sums with the pinned Goose and fail if it
stops writing them in the shape
plain_sumsknows. matmul_f32is still 1.31 and 1.37 times C on the ESP32s. C fuses each multiply and add into one instruction; Goose rounds twice (section 6).
13. Queues: nodes for a backlog¶
Cause. On the Pico 2 W, queue_2000 took 9,209 µs against C's 6,939,
1.33 times, and its samples spread from 8,974 to 10,654 µs. On the H7 it
took 2.33 times C's. A probe on the Pico 2 W
(20261205-pico-crc-queue)
found:
- The cache was full. Every one of
qnode_flow's 17 runs filled the node cache, section 3's 8 chunks of 512 bytes, and 770 to 987 of its 2,000 nodes came frommalloc. With one core, the worker that sends keeps the CPU while main waits at the same priority, so most of the messages wait at once. - A node from
malloccost about 3.6 µs more, from a least-squares line through the runs' times against their nodes frommalloc(r = 0.937). Amallocandfreeof 32 bytes take 3.7 to 4.0 µs there, about 2.8 µs of it newlib's lock, which suspends the scheduler. A node from the cache and back takes 0.9. - A bigger cache closed the gap. With 8 chunks of 8 KB, no node came
from
malloc, and in the suitequeue_2000took 6,215 µs, 0.90 of C's, with 45 KB more kept until reset. With 128 chunks of 512 bytes the runs got slower as chunks were added:mg_qnode_freewent through the chunk table one entry at a time.
The H7 is the same case: on main, 1,360 to 1,744 of each qnode_flow
run's 2,000 nodes come from malloc (below).
Change. components/microgoose/mg_qnode.c, in four commits:
f156180, 9b3ff3b, c8c2d20 and c47a0bd. The last two took the
ESP32s back to section 3's cache.
- Chunks that double, on one core. On the H7 and the Pico 2 W, a size class's first chunk is 512 bytes, as before, and each next one twice the one before, up to 8 KB, 32 chunks in all. A program that sends a few messages of a size keeps 512 bytes for them, and a burst gets chunks in proportion to how many of its messages wait at once.
- A share of the heap. The cache takes no chunk that would make its
chunks more than a quarter of the memory that would be free without
them:
mg_free_heap()on the H7 and the Pico 2 W, internal RAM on the ESP32s. Past that, nodes come frommallocas before, and the next 64 nodes that would need a chunk go straight tomallocbefore the cache looks at the heap again: the look takes newlib's lock. Chunks are still kept until reset, so a quarter is what one burst can leave taken. A quarter was chosen, not measured to be best:queue_2000needs 40 to 65 KB of chunks on these two boards, well under a quarter of the 341 and 362 KB they have free when the suite starts. - The free, on one core.
mg_qnode_freelooks first at the chunk the last free found its node in, then through the table newest first: a burst's nodes come back in the order they went out, mostly from one chunk. - The ESP32s keep 8 chunks of 512 bytes, and a free that looks
through them oldest first, as on main (
c8c2d20,c47a0bd). With two cores the receiver runs on the other core, so fewer messages wait at once: inqnode_flowon main, 14 of the S3's 17 runs and all 17 of the WROOM's took no node frommalloc. With the chunks that double there too (9b3ff3b),queue_2000took 0.986 of its time on the S3 and 0.993 on the WROOM, and the cache kept 20 KB more internal RAM after it: on the S3, the RAM a worker's data stack and globals get while 64 KB of it stays free (mg_compat.c). With chunks of one size, the hint cost more than it saved (below). - A fix on the way (
9b3ff3b): the count of nodes left before the next look is read once. Read twice, as inf156180, the other core could take it to 0 in between, and the count down would wrap it to 4 billion, which stops the cache from growing. Found by reading the code, not in a run.
For the H7 and the Pico 2 W, mg_qnode.c is the same at 9b3ff3b,
c8c2d20 and c47a0bd once preprocessed (arm-none-eabi-gcc -E -P,
compared byte for byte), so their runs at 9b3ff3b stand for the
change.
Before and after. 5 boots each, built in git worktrees whose paths have the same length.
- Pico 2 W. Before: dev/20261205T035545Z-pico2w-goose-81080b2-qnode-control, the probe's control: main's node cache, built in the same worktree. The commits from it to main changed the port only for the emulator's build. After: 20261205T042925Z-pico2w-goose-9b3ff3b.
- H7. Before: 20261205T055054Z-h7-goose-932815d-main. After: 20261205T055246Z-h7-goose-9b3ff3b.
- S3. Before: 20261205T043509Z-s3-goose-932815d-main. After: 20261205T070003Z-s3-goose-c47a0bd.
- WROOM. Before:
20261205T053049Z-esp32-goose-932815d-main.
After:
20261205T070748Z-esp32-goose-c47a0bd,
and with
--worker-reserve 4K20261205T071903Z-esp32-goose-c47a0bd-worker-reserve-4k. - The ESP32s at
9b3ff3bandc8c2d20, and the runs of thethreadsgroup alone below, are inbench/results/dev/.
The runs on main match the latest published ones on each board (181987f)
within 6 µs, but for queue_2000 (4,202 µs against 4,281 on the H7,
2,805 against 2,796 on the S3) and fault_contained on the WROOM (308
against 292). These are now the latest runs on each board, so
Results and the report's tables show them.
| Pico 2 W, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
200 | 200 | 1.000 | 1.04 |
crc32 |
6,889 | 6,889 | 1.000 | 1.17 |
heapsort |
5,335 | 5,335 | 1.000 | 1.52 |
mandel_f64 |
88,832 | 88,832 | 1.000 | 1.02 |
mandel_f32 |
8,604 | 8,604 | 1.000 | 1.10 |
matmul_f32 |
2,024 | 2,024 | 1.000 | 1.12 |
queue_2000 |
9,233 | 6,026 | 0.653 | 0.87 |
pingpong |
21 | 21 | 1.000 | 1.05 |
spawn_wait |
845 | 816 | 0.966 | 1.01 |
scale_main |
328,330 | 328,330 | 1.000 | 1.02 |
scale_1w |
326,297 | 326,272 | 1.000 | 1.01 |
scale_2w |
327,568 | 327,522 | 1.000 | 1.01 |
scale_4w |
329,193 | 329,122 | 1.000 | 1.01 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
3 | 3 | 1.000 | 0.60 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 | 12 | 1.000 | 0.02 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
4 | 4 | 1.000 | 0.80 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
13 | 13 | 1.000 | 0.03 |
fault_contained |
989 | 980 | 0.991 | — |
| H7, medians | before (µs) | after (µs) | after ÷ before | after ÷ c-fpu |
|---|---|---|---|---|
clock_1000 |
122 | 122 | 1.000 | 0.50 |
crc32 |
1,681 | 1,682 | 1.001 | 0.69 |
heapsort |
1,748 | 1,748 | 1.000 | 2.12 |
mandel_f64 |
7,251 | 7,251 | 1.000 | 1.04 |
mandel_f32 |
3,391 | 3,391 | 1.000 | 0.92 |
matmul_f32 |
614 | 614 | 1.000 | 1.01 |
queue_2000 |
4,202 | 2,492 | 0.593 | 1.35 |
pingpong |
8 | 8 | 1.000 | 1.00 |
spawn_wait |
65 | 62 | 0.954 | 0.66 |
scale_main |
26,480 | 26,480 | 1.000 | 1.04 |
scale_1w |
26,554 | 26,550 | 1.000 | 1.04 |
scale_2w |
26,630 | 26,625 | 1.000 | 1.03 |
scale_4w |
26,763 | 26,750 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
1 | 1 | 1.000 | 0.50 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
4 | 5 | 1.250 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | — |
pin_idle_wake |
1 | 1 | 1.000 | — |
pin_busy_gap |
1,003 | 1,003 | 1.000 | — |
pin_busy_wake |
5 | 5 | 1.000 | — |
fault_contained |
85 | 82 | 0.965 | — |
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,659 | 4,659 | 1.000 | 1.93 |
mandel_f64 |
166,166 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,451 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
1,689 | 1,688 | 0.999 | 1.31 |
queue_2000 |
2,805 | 2,785 | 0.993 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
137 | 138.5 | 1.011 | 1.71 |
scale_main |
630,227 | 630,229 | 1.000 | 1.03 |
scale_1w |
625,843 | 625,860 | 1.000 | 1.02 |
scale_2w |
321,752 | 321,767 | 1.000 | 1.02 |
scale_4w |
322,895 | 322,929 | 1.000 | 1.02 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
10 | 10 | 1.000 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 10 | 1.000 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
10 | 10 | 1.000 | 1.25 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
11 | 11 | 1.000 | 0.02 |
fault_contained |
237 | 257 | 1.084 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,541 | 5,541 | 1.000 | 1.94 |
mandel_f64 |
171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
2,868 | 2,862 | 0.998 | 0.61 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
156 | 157 | 1.006 | 1.60 |
scale_main |
647,585 | 647,585 | 1.000 | 1.03 |
scale_1w |
652,934 | 652,909 | 1.000 | 1.04 |
scale_2w |
355,140 | 355,144 | 1.000 | 1.03 |
scale_4w |
355,687 | 355,646 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
11 | 12 | 1.091 | 1.20 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 | 12 | 1.000 | 0.01 |
pin_idle_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_idle_wake |
12 | 12 | 1.000 | 1.20 |
pin_busy_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_busy_wake |
12 | 12 | 1.000 | 0.02 |
fault_contained |
308 | 249 | 0.808 | — |
What moved.
queue_2000takes 0.653 of its time on the Pico 2 W, 6,026 µs, now 0.87 of C's, and 0.593 on the H7, 2,492 µs, 1.35 of C's against 2.28. On the Pico 2 W its samples spread over 69 µs (5,993 to 6,062), where they spread over 1.5 ms; on the H7 every boot's median is 2,492.- Memory kept. At the end of the suite the free heap is 44,928 or
53,128 bytes lower than before on the Pico 2 W (283,996 or 292,196
against 337,124) and 61,204 lower on the H7 (296,248 against 357,452):
the chunks
queue_2000's backlog took, kept until reset. How many depends on the boot's backlog. C'squeue_2000holds its messages in a 2,000-deep FreeRTOS queue of 32 KB. At the start the free heap is 376 bytes lower on the Pico 2 W and 252 on the H7: the larger chunk table. spawn_waitis faster on both, in every boot: 816 µs against 845 on the Pico 2 W, 62 against 65 on the H7. On the Pico 2 W, the probe's run with 8 KB chunks left it at 845, so it isn't the nodes. Why isn't measured.timer_busy_wakeon the H7 is 5 µs in every boot, against 4 in four of main's five and in all five of the published run's: 1 µs slower, in code the change doesn't touch. Not looked into.- Late edges on the Pico 2 W. Beside the busy worker, 17 of the 5,000 edges took 991 to 998 µs, two to four in every boot, against 8 in the run before and 6 and 7 in the probe's and the published runs: an edge that waits for the scheduler's tick. The median stays at 13 µs. Not looked into. The run before had its own: in its first boot, 500 of the 1,000 timer ticks beside the busy worker took 998 µs, and none did in the others or after.
- The scaling runs are 0 to 71 µs faster on the two one-core boards,
0.02% at most.
fault_containedis 9 µs faster on the Pico 2 W and 3 on the H7. - On the ESP32s, with main's cache and free:
queue_20002,785 µs against 2,805 on the S3 (its boots' medians 2,740 to 2,984, against 2,802 to 2,818) and 2,862 against 2,868 on the WROOM.spawn_wait138.5 against 137 on the S3 and 157 against 156 on the WROOM, andpingpong25 on both, as before.fault_containedtakes 257 µs against 237 on the S3, a loss, its boots at 243.5 to 275 against 228 to 241.5, and 249 against 308 on the WROOM: that one has moved from 274 to 316 µs between builds before (section 12). The idle timer wake on the WROOM is 12 µs against 11, its boots at 11 to 14. The free heap is 48 bytes lower at the start on the S3 and 60 on the WROOM, and lower by the same afterqueue_2000. With--worker-reserve 4Kthe WROOM's run matches the published one (181987f) within 1 µs, but forqueue_2000, 2,814 against 3,004, andfault_contained, 256 against 309. - The hint, on the ESP32s. At
c8c2d20the ESP32s had the one-core free, hint and all. In thethreadsgroup alonequeue_2000took 2,800 µs on the S3 (5 boots) and 2,798.5 on the WROOM (10 boots), against 2,746 and 2,724.5 on main; the same code without the hint took 2,752 and 2,757.5. By the code, a run's nodes come back mixed from a few chunks of one size, so the hint mostly misses, and then the free looks through the table and writes the hint as well; the misses weren't counted.c47a0bdtakes the hint out there. - Where the code lands, on the S3. At
c8c2d20the S3's suite hadspawn_waitat 178 µs against 137,pingpongat 28 against 25 andfault_containedat 252.5 against 237. The code after the cache's sits 180 bytes further on in flash (nm). Main with 180 zero bytes added inmg_qnode_free's section, bytes that never run, puts 804 of the 808 functions after it wherec8c2d20has them, and main without it none (place_check.pyin the probe folder), as section 5 did forsleep_on. In thethreadsgroup alone, 5 boots each,spawn_waittook 132 µs on main (boots' medians 129 to 134), 172 with the padding (167 to 178.5), 156 atc8c2d20(151.5 to 165.5) and 141 without the hint (138 to 153).pingpongwas 25 in all four. Sospawn_waiton the S3 moves by up to a third with where the code after the cache lands, and that loss wasn't the cache's work.c8c2d20's comment and commit message gave it as a reason to keep the ESP32s at 4 KB;c47a0bdcorrects them. queue_2000in the WROOM's suite atc8c2d20took 3,564 µs against 2,868, four of its boots' medians at 3,367 to 3,814. Neither that code'sthreadsgroup alone (10 boots, 2,753 to 2,854) norc47a0bd's suite (2,729 to 2,967) did so again. Why it did isn't known. A boot can also start slow: one ofc47a0bd's, and two of main's, began at 19 ms and came down within five samples.- With the chunks that double on the ESP32s (
9b3ff3b): on the S3,queue_20002,766 µs against 2,805 (0.986),spawn_wait174 against 137 (1.270) andfault_contained295 against 237 (1.245), and the free heap 20,312 bytes lower afterqueue_2000. On the WROOM,queue_20002,848 against 2,868 (0.993) and 20,308 bytes lower after it,spawn_wait157 against 156, andfault_contained233 against 308. Every boot ranscale_4w. Itsspawn_waiton the S3 is the placement above.
Checks.
- Results: every benchmark's result matches C's, in every boot of the after runs.
qnode_stresspasses on all four boards, and in Renode and QEMU. On the H7 and the Pico 2 W its bursts of 3,157 messages leave the cache at 109,568 bytes, stopped by the share: the next 8 KB chunk would have made it more than a quarter. On the ESP32s it stays at 8 chunks of 512 bytes, and each burst sends 117 to 132 messages. The WROOM's first run on the board ended with the free heap 21,300 bytes under its start, where the others end 4,808 to 4,828 under: by its size, most likely a finished worker's native stack, which ESP-IDF frees only when an idle task next runs (mg_compat.c). A second run ended 4,808 under.- Other stress programs:
sync_stressandworker_globalspass on the Pico 2 W (on the board), the H7 (in Renode) and both ESP32s (in QEMU),pingpong_costson the H7 and the ESP32s, andlow_heapon the WROOM.worker_globalsends with the free heap where it started on the one-core boards, and 516 and 616 bytes under on the S3 and the WROOM: a chunk, and on the WROOM a 96-byte semaphore too, which the program allows for.pingpong_costsdoesn't build for the Pico 2 W (it uses a static FreeRTOS semaphore, which that port's config leaves out), as before. - Conformance: 22 of 22 on the Pico 2 W in picoem and on the H7 in
Renode, each with one sample given longer than the default 60 s
(
23_mandelbrot_threadstakes about 4 minutes in picoem;10_sudokutook 87 s in Renode beside ESP-IDF builds, and 58 to 62 s in Renode before). Atc47a0bdin QEMU, 24 of 24 run on the S3 and 21 of 21 on the WROOM, with the samples that don't fit skipped as before (one on the S3, four on the WROOM);c8c2d20's runs gave the same. - Unit tests: 198 pass, 24 skipped.
- Raw data. Every probe, stress and conformance log behind this section is in bench/results/probes/20261205-qnode-growth, with what each shows.
What it leaves.
- Memory kept until reset. On the H7 and the Pico 2 W a burst can
leave up to a quarter of the free heap in the cache:
qnode_stress's bursts of 3,157 messages left 109,568 bytes. The cache never gives a chunk back, so a program that bursts once and later needs that memory for stacks finds less of it. - Frees of other blocks. A free of a block that isn't a node but lies between the lowest and the highest chunk goes through the chunk table, up to 32 entries on one core.
- The ESP32s are where they were: a backlog past 4 KB of nodes still
takes them from
malloc, at 6 to 10 µs a node. Inqnode_flow, the 11th of 17 runs on each ESP32 had such a backlog and took 444 and 332 of its nodes frommalloc: 5,697 and 6,678 µs, against medians of 2,838 and 2,860. On the S3 each of the six runs after it took 1 to 27, at 2,877 to 3,080 µs, and on the WROOM one took 8. Main's S3 run went the same way: 268 in its 12th run, then 21 and 17 in two of the five after it. spawn_waiton the S3 moves by up to a third with where code lands in flash, above. Which function's place does it isn't known.fault_containedon the S3, 257 µs against 237, above.crc32on the Pico 2 W is still 1.17 times C's: Goose countsfor k in 8in a 64-bit integer, and GCC 14.2 then counts up and compares, one cycle more a bit (the probe). A fix belongs in the Goose compiler or incore.py's rewrites.timer_busy_wakeon the H7 and the late edges on the Pico 2 W, above.
14. Events straight to the waiting thread¶
Cause. With nothing else running, an event reached a Goose program
2 µs after it reached C's on the ESP32s: pin_idle_wake and
timer_idle_wake took 10 µs on the S3 against C's 8, and 12 on the
WROOM against 10. Section 8 left it
there, not profiled. Both build on one ESP-IDF and one FreeRTOS, so the
2 µs had to be in MicroGoose's own path, from the interrupt to
event_wait() returning.
The probe. wake_probe.h reads the cycle counter at seven points of
each edge's path, in MicroGoose's runtime and in the C suite's ESP32 port
alike, and prints the median of each stretch for the suite's 1,000
pin_idle edges. It adds about 1 µs to both: with it in, Goose took 11
µs on the S3 and C 9. Main's code against C's queue, in cycles at 240
MHz, the median of 3 boots' medians (each boot gave the same):
| a pin's edge, nothing else running: cycles | S3 Goose | S3 C | WROOM Goose | WROOM C |
|---|---|---|---|---|
the handler's timestamp (esp_timer_get_time()) |
193 | 193 | 122 | 122 |
| the rest of the handler | 1,006 | 757 | 1,261 | 934 |
| FreeRTOS: the handler's end to the task back from its wait | 1,034 | 1,086 | 1,284 | 1,335 |
| the wait's own return | 260 | 26 | 321 | 37 |
| the program, from the wait to its clock read | 51 | 46 | 66 | 64 |
the clock read (micros()) |
193 | 194 | 124 | 125 |
| from the timestamp to the program having the time | 2,544 | 2,109 | 3,056 | 2,495 |
- Not the kernel. The switch into the woken task is FreeRTOS's code in both, and Goose's took some 50 cycles less.
- The handler: 249 and 327 cycles more. C's sends the stamp to a FreeRTOS queue. Goose's takes the events' spinlock, keeps the pin's count, its debounce and the guard against a flood of edges, queues the event, and gives a binary semaphore, which in FreeRTOS is a queue send of its own.
- The wait's return: 234 and 284 cycles more. C's task comes back
from
xQueueReceive()holding the stamp. Goose's came back from the semaphore and took the spinlock again to take the event off the queue.
Change. In components/microgoose/mg_hal.c and
firmware/sdkconfig.defaults, for the ESP32s. The H7's and the Pico 2
W's ports don't change.
- A task notification, not a semaphore. A thread asleep in
event_wait()sleeps on its task-notification slot 1, and the interrupt wakes it withvTaskNotifyGiveIndexedFromISR().mg_sync's waits keep slot 0.CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=2gives each task the second slot, and the runtime refuses to build with fewer. - The event goes straight to the thread asleep. An event that finds
a thread asleep in
event_wait()is written into that thread's record, and the thread returns it without taking the spinlock again. An event that finds none is queued, as before. The thread that fell asleep first gets the first event. Four can sleep at once; a fifth looks again at each tick. - The records are in internal RAM. The interrupt runs while the flash cache is off, and it writes the record, so the four records are static variables in DRAM, not on the waiting thread's stack: a worker's stack can be in PSRAM.
- The pin's level from its register. The pin's handler is in IRAM so
that it runs while flash is read or written, with the flash cache off,
but it read the pin's level with
gpio_get_level(), which ESP-IDF keeps in flash unlessCONFIG_GPIO_CTRL_FUNC_IN_IRAMis set, and it isn't. In main's imagesgpio_get_levelis at0x4200d6fcon the S3 and0x400e1370on the WROOM, in flash, and the handler at0x4037523cand0x40081188, in IRAM. The handler now reads the register (gpio_ll_get_level()). On main a pin's edge while a program used a file underflash/stopped the board.tests/stress/flash_edges.goose(new, under Checks) writes a file there 30 times with a 997 Hz wave on the pin it watches. On main the S3 was reset by its interrupt watchdog (TG1WDT_SYS_RST) at the first write, 25 times in 90 s. The WROOM stopped with "Cache disabled but cached memory region accessed", its program counter atgpio_get_level(0x400dd030in that program's image), called from the handler while ESP-IDF'sesp_flash_readhad the cache off for the file system. Main with only this line changed passes it on both boards, as the change does.
How it got there. One step at a time, each measured with the probe:
the median cycles from the timestamp to the program having the time, for
the pin_idle edges.
| step | S3 | WROOM | what moved |
|---|---|---|---|
| main | 2,544 | 3,056 | |
| 1. the level from the register | 2,535 | 3,041 | the handler, 11 and 15 cycles less |
| 2. a notification for the semaphore | 2,274 | 2,717 | the handler, 270 and 337 less |
| 3. the event straight to the thread, its record on the thread's stack | 2,022 | 2,408 | the wait's return, 206 and 254 less; the handler, 47 and 52 less |
| 4. the records in internal RAM: the change | 2,037 | 2,429 | the wait's return, 16 and 20 more |
| C: a FreeRTOS queue, the suite's | 2,109 | 2,495 | |
| C: a ring and a notification | 1,769 | 2,089 |
The last row is C written the fastest way known here for this path, to
see how far the change is from it: the handler puts the stamp in a ring
only it writes, and notifies the task, which is the only one to read the
ring. It was built from the same patch (BP_PIN_NOTIFY), and isn't the
suite's C.
Before and after. The probe out, the suite's timers and pins
(--only events), 5 boots of 1,000 events each, from 773c8c6 without
and with the change, and C from the same revision. A worker spins beside
the busy ones, at the program's priority, as in the published tables.
Median, 99th percentile and slowest, in µs.
| ESP32-S3 | Goose before | Goose after | after ÷ before | C, a queue (the suite's) | C, a ring and a notification |
|---|---|---|---|---|---|
timer_idle_wake |
10, 10, 10 | 8, 8, 8 | 0.80 | 8, 8, 8 | 9, 9, 9 |
pin_idle_wake |
10, 13, 15 | 8, 11, 13 | 0.80 | 8, 11, 13 | 7, 10, 12 |
timer_busy_wake |
10, 11, 11 | 8, 8, 10 | 0.80 | 1,365.5, 1,865, 1,865 | 1,392, 1,891, 1,892 |
pin_busy_wake |
11, 13, 933 | 8, 12, 899 | 0.73 | 477, 1,975, 1,989 | 483, 1,973, 1,990 |
| ESP32 WROOM-32 | Goose before | Goose after | after ÷ before | C, a queue (the suite's) | C, a ring and a notification |
|---|---|---|---|---|---|
timer_idle_wake |
12, 12, 12 | 10, 10, 10 | 0.83 | 10, 10, 10 | 10, 10, 10 |
pin_idle_wake |
12, 14, 17 | 10, 12, 14 | 0.83 | 10, 12, 14 | 8, 11, 13 |
timer_busy_wake |
12, 12, 12 | 9, 10, 10 | 0.75 | 1,348.5, 1,847, 1,848 | 1,345, 1,844, 1,845 |
pin_busy_wake |
12, 14, 20 | 10, 12, 17 | 0.83 | 437, 1,963, 1,986 | 465, 1,960, 1,989 |
The gaps (timer_idle_gap and the rest) are 1,000 to 1,003 µs in every
column, as before, and every check is 0: no event was missed.
- Level with C's queue with nothing else running, on both boards, timers and pins: 8 µs on the S3 and 10 on the WROOM.
- Beside a busy worker the wake is 2 to 3 µs shorter too. C waits for the scheduler there, as section 11 measured.
- One slow wake a boot on the S3, beside the busy worker: 922 to 933 µs before and 895 to 899 after, one in each of the 5 boots, both times. The WROOM has none. The change didn't bring it or take it away.
- Behind C's ring by 1 µs on the S3 and 2 on the WROOM for a pin. Its timer is the queue's code, unchanged, and took 9 and 10 µs in that build.
The whole suite. Goose, 5 boots on each ESP32, main and the change built on the same day in worktrees of one path length:
- S3. Before: 20261205T145734Z-s3-goose-773c8c6-main. After: 20261205T150214Z-s3-goose-773c8c6-full-after.
- WROOM. Before: 20261205T150717Z-esp32-goose-773c8c6-main. After: 20261205T151158Z-esp32-goose-773c8c6-full-after.
The H7 and the Pico 2 W weren't run: the change is in the ESP32s' code alone. The runs are of the change before it was committed, so they stay with the probe's data, and Results and the report keep section 12's runs.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,568 | 3,567 | 1.000 | 0.90 |
heapsort |
4,731 | 4,731 | 1.000 | 1.95 |
mandel_f64 |
166,167 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,452 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
1,688 | 1,688 | 1.000 | 1.31 |
queue_2000 |
2,810 | 2,774 | 0.987 | 0.80 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
127 | 145 | 1.142 | 1.79 |
scale_main |
630,228 | 630,228 | 1.000 | 1.03 |
scale_1w |
625,855 | 625,916 | 1.000 | 1.02 |
scale_2w |
321,778 | 321,817 | 1.000 | 1.02 |
scale_4w |
322,946 | 322,443 | 0.998 | 1.02 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
9 | 8 | 0.889 | 1.00 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 8 | 0.800 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
10 | 8 | 0.800 | 1.00 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
11 | 8 | 0.727 | 0.02 |
fault_contained |
224 | 287 | 1.281 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,701 | 3,702 | 1.000 | 0.89 |
heapsort |
5,491 | 5,490 | 1.000 | 1.92 |
mandel_f64 |
171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
2,787 | 2,939 | 1.055 | 0.62 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
156 | 163 | 1.045 | 1.66 |
scale_main |
647,585 | 647,585 | 1.000 | 1.03 |
scale_1w |
652,935 | 652,911 | 1.000 | 1.04 |
scale_2w |
355,184 | 355,111 | 1.000 | 1.03 |
scale_4w |
355,448 | 355,622 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
12 | 9 | 0.750 | 0.90 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
11 | 9 | 0.818 | 0.01 |
pin_idle_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_idle_wake |
12 | 10 | 0.833 | 1.00 |
pin_busy_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_busy_wake |
12 | 10 | 0.833 | 0.02 |
fault_contained |
285 | 253 | 0.888 | — |
- The wakes are 1 to 3 µs shorter on both boards, idle and beside a busy worker. The WROOM's timer wake is 9 µs in this run, where the events runs above and C's latest run have 10.
spawn_waitandfault_containedmoved, and not the same way on the two boards: 14% and 28% slower on the S3, 4.5% slower and 11% faster on the WROOM. Neither runs any of the event code.queue_2000was 5.5% slower on the WROOM and 1.3% faster on the S3. The next paragraph measures what moves them.
What moves spawn_wait and fault_contained. The threads and
faults groups alone, 3 boots, for builds of 773c8c6: main; main
with only the change's line in sdkconfig.defaults, the second
notification slot; that with an unused function of 256 or 1,024 nops
added to mg_hal.c, which runs nowhere and only moves the code after
it; on the WROOM, that with an unused array of 256 or 1,024 bytes in
internal RAM instead, which moves the static data after it and where the
heap starts; and the change. Medians, µs (the runs are in the probe's
runs/, labelled main, notify2, notify2-pad256, notify2-pad1024,
notify2-data256, notify2-data1024 and after):
| build | S3 spawn_wait |
S3 fault_contained |
WROOM spawn_wait |
WROOM fault_contained |
|---|---|---|---|---|
| main | 130.5 | 269.5 | 155 | 314 |
| the second slot alone | 117 | 216.5 | 155 | 327 |
and 256 unused nops |
138 | 332.5 | 155 | 330 |
and 1,024 unused nops |
145 | 332 | 155 | 331 |
| the second slot and 256 unused bytes of data | — | — | 156 | 330.5 |
| the second slot and 1,024 unused bytes of data | — | — | 156 | 332 |
| the change | 174 | 257.5 | 161 | 256.5 |
- On the S3, code that never runs moved them by as much as the
change did:
spawn_waitbetween 117 and 145 µs andfault_containedbetween 216.5 and 332.5. A spawn runs a long stretch of code from flash once each time, and how much of it the cache still holds depends on where the linker put it. The change's 174 µs is past that range, so these five builds don't separate a cost of the change's own from where it moved the code. It adds no instruction to a spawn. - On the WROOM, unused code didn't move
spawn_wait: 155 µs in all four builds, and 161 with the change. Unused data didn't either: 156 µs with 256 or 1,024 bytes. The change adds no instruction to a spawn there either, so what makes it 6 µs slower on the WROOM isn't known. Itsfault_containedis faster there: 256.5 µs, against 314 to 332 in the other builds. - The second notification slot made neither slower on the S3.
Where the rest of the wake goes. Of the 2,037 cycles the S3 now takes in the probe, 1,042 are FreeRTOS leaving the interrupt and switching to the woken task, the same code C runs, much of it written in assembly in ESP-IDF's port. 194 are the program's own clock read, which goes to the system timer over the peripheral bus. What separates the change from C's ring, 257 cycles on the S3 and 327 on the WROOM, is in the handler: the spinlock, which events from both cores' interrupts and threads on both cores need; the pin's count, debounce and flood guard; and the list of threads asleep. It isn't split further here.
Checks. On both ESP32s, one board at a time with the other held in its bootloader, from the change's code unless named.
tests/stress/event_waiters.goose(new) puts out a 997 Hz wave on a pin and watches its own rising edges for 3 s a round: one thread waiting 1 ms or 100 ms at a time, one polling, two, three, two workers alone, and five, one more than can sleep at once. It passes if every edge reached exactly one thread and none was dropped. It passes on main and with the change, on both boards, about 2,990 edges a round. With the change the thread that fell asleep first gets the next event, so main's share with two to five threads waiting went from 1 to 25% of the edges to 29 to 63%.tests/stress/flash_edges.goose(new) puts out a 997 Hz wave on a pin and watches its rising edges while the program writes a file of about 7 KB underflash/30 times: first with main taking the events after each write, then with a worker waiting for them as they come. It passes if every write succeeded and every edge counted reached the program once. With the change it passes on both boards, 1,419 and 1,483 edges on the S3 and 1,281 and 1,847 on the WROOM, none dropped, the slowest write 47 to 69 ms. On main it stopped both boards, above.pin_wake,timer_wake,event_prio,clock_costs,sync_stressanduart_framespass on both boards, andheap_needsandlow_heapon the WROOM.pin_wakehas an edge in 8 µs on the S3 and 10 on the WROOM, with nothing else running and beside a busy worker.timer_wakehas the tick 10 and 12 µs after it was due, where section 8 left it at 12 and 14.event_prio, one step up beside two busy workers, has its median at 8 and 9 µs, where section 11 measured 10 and 12.- Conformance in QEMU: 24 of 24 samples on the S3 and 21 of 21 on the WROOM, as before.
- Results match C's in every boot of the runs above, the whole suite's included, and every boot of those finished.
What it leaves.
- C's ring is 1 to 2 µs ahead, above.
spawn_waitandfault_containedare slower on the S3 in this build, by 14% and 28% in the whole suite, and how much of that is where the code landed isn't measured apart, above. On the WROOMspawn_waitis 4.5% slower in the whole suite, and in the groups alone 161 µs against 155 to 156 in every other build: why isn't known.- An event given to a thread asleep is that thread's.
pin_watch()andtimer_stop()drop the source's queued events, and an event already written into a waiting thread's record isn't queued: that thread returns it, since it came before the stop. Before, another thread's stop dropped it if the waiting thread hadn't taken it off the queue yet. A program with one thread can't tell the difference. - Four threads asleep at once. A fifth that waits looks for an event again at each tick, so it can have an event up to 1 ms late. Before, any number could sleep on the semaphore.
event_us()andevent_level()are the last event's for the whole program, not for each thread, as before: with two threads waiting, a thread'sevent_us()can be the other's event.- One full build. The new FreeRTOS setting compiles everything again once, for each ESP32 board.
- The H7 and the Pico 2 W have paths of their own: an event reaches the program in under 1 µs on the H7, and in 3 to 4 µs on the Pico 2 W against C's 5. They weren't profiled here.
15. Spawning: a parked thread¶
Cause. On main (e38087e) spawn_wait took 145 µs on the S3 and 157
on the WROOM, against C's 81 and 98.
Section 4 timed a spawn's
steps, and the longest was from pthread_create until the worker ran: 76
µs on the S3 and 92 on the WROOM. In tests/stress/spawn_costs.goose on
main, an empty pthread created and waited for takes 82 and 100 µs, and an
empty FreeRTOS task 69 and 81. C's spawn does that work too:
bench/c/bench.c creates a FreeRTOS task for each spawn and deletes it at
the join. A thread that is already there skips it.
Change. In components/microgoose/mg_compat.c and
microgoose/core.py, for the ESP32s, and only on a board with PSRAM (the
WROOM's runs below show why).
- A finished worker's thread parks. When a worker ends, its pthread
waits for the next spawn instead of ending, two at most. A spawn that
asks for a native stack of the same size hands the parked thread its
worker and wakes it with a binary semaphore; otherwise it creates a
pthread as before.
core.pyturns the runtime's onepthread_createfor a worker into a call tomg_worker_create, under#if defined(MICROGOOSE) && defined(ESP_PLATFORM), and stops the build if the call isn't where it expects. - Memory comes first. A parked thread keeps its native stack and its task, about 16.5 KB of internal RAM with the default stack. A thread parks only while 32 KB of internal RAM stays free, and ends after 1 s with no spawn to take it, so a program's memory is back within a second of its last spawn. A data stack, a native stack or a worker's globals that can't be had sends the parked threads away first, then waits for the idle tasks to free their stacks, as it waited for finished threads' before.
- A parked thread starts a worker as a new one would. It keeps its
thread-locals. The runtime's, in Goose v2026.5, are each set as a
worker starts or given back as it ends (its data stacks, globals,
contained fault, id, regions and native stack bounds), checked by
reading the runtime.
core.pylists them and stops the build if the runtime has one that isn't on the list. A program's own thread-locals, which the compiler makes for each program (for areturn ... from, for one), are set back to their first values at each worker's entry. The thread's priority, whichtask_priority()may have changed, goes back to pthread's default before it parks. - Not with
--stack-report: the report reads each worker's native stack as its thread ends (section 10). - The H7 and the Pico 2 W don't build the new code: it's all under
ESP_PLATFORM.
C with a parked task. To see how far the same idea goes in C,
c_parked.py (in the probe folder) patches bench/c/bench.c so that a
joined task waits for the next spawn, two at most, as the change does. It
isn't the suite's C.
Before and after. 5 boots each, main and the change built on the same day in worktrees of one path length:
- S3. Before: 20261205T160200Z-s3-goose-e38087e-main. After: 20261205T163135Z-s3-goose-e38087e-full-final.
- WROOM. Before: 20261205T161602Z-esp32-goose-e38087e-main. After: 20261205T164200Z-esp32-goose-e38087e-full-final, which doesn't park.
- C, the
threadsgroup alone (--only threads), frome38087e: the suite's C and C with a parked task, on each board, in the probe'sruns/(labelledc-threadsandc-parked).
The runs are of the change before it was committed, so they stay with the probe's data, and Results and the report keep section 13's runs. They don't have section 14's change.
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,735 | 4,735 | 1.000 | 1.96 |
mandel_f64 |
166,166 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,449 | 7,452 | 1.000 | 1.03 |
matmul_f32 |
1,688 | 1,688 | 1.000 | 1.31 |
queue_2000 |
2,789 | 2,780 | 0.997 | 0.80 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
145 | 54 | 0.372 | 0.67 |
scale_main |
630,228 | 630,228 | 1.000 | 1.03 |
scale_1w |
625,874 | 625,763 | 1.000 | 1.02 |
scale_2w |
321,783 | 321,667 | 1.000 | 1.02 |
scale_4w |
322,104 | 322,438 | 1.001 | 1.02 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
10 | 10 | 1.000 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 10 | 1.000 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
10 | 10 | 1.000 | 1.25 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
11 | 11 | 1.000 | 0.02 |
fault_contained |
233 | 146.5 | 0.629 | — |
| WROOM, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,490 | 5,490 | 1.000 | 1.92 |
mandel_f64 |
171,317 | 171,316 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
2,903 | 2,887 | 0.994 | 0.61 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
157 | 157 | 1.000 | 1.60 |
scale_main |
647,585 | 647,585 | 1.000 | 1.03 |
scale_1w |
652,936 | 652,955 | 1.000 | 1.04 |
scale_2w |
355,155 | 355,168 | 1.000 | 1.03 |
scale_4w |
355,660 | 355,669 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
12 | 12 | 1.000 | 1.20 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 | 12 | 1.000 | 0.01 |
pin_idle_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_idle_wake |
12 | 12 | 1.000 | 1.20 |
pin_busy_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_busy_wake |
12 | 12 | 1.000 | 0.02 |
fault_contained |
252 | 254 | 1.008 | — |
spawn_wait against C. Medians, µs:
spawn_wait |
S3 | WROOM | WROVER-E |
|---|---|---|---|
| MicroGoose, main | 145 | 157 | 160 |
| MicroGoose, the change | 54 | 157 | 77 |
| MicroGoose with the pool on the WROOM, not kept (below) | 77 | ||
| C, a task for each spawn (the suite's) | 81 | 98 | |
| C, a parked task | 22 | 31 |
What moved.
spawn_waiton the S3 takes 0.372 of its time, 54 µs in every boot: 0.67 of C's, where it was 1.79. Inspawn_costsa spawn and wait took 51 µs, against 109 on main, and 204 of its 205 spawns ran on a parked thread: the first one made it.- C with a parked task is ahead, at 22 µs on the S3 and 31 on the WROOM: 2.5 times faster than the change on the S3. What's left of a Goose spawn is mostly what section 4 timed after the thread starts: the worker's data stack (36 µs on the S3 there), its globals, and its release and completion (34 µs). It wasn't timed again here.
fault_containedon the S3 takes 146.5 µs against 233, 0.629: each contained fault starts a worker. A first build of the change, whose code on the S3 differs only by the PSRAM check, had it at 136 (its run). Where the code lands in flash moves it on the S3, as section 13 found forspawn_wait, and section 14 measured it moving between 216.5 and 332.5 µs with code that never runs.- The free heap reads lower while a thread is parked. After the
threadsbenchmarks the suite reads it 16,560 to 33,360 bytes lower than main on the S3, one or two parked threads, and 16,780 lower afterfault_contained. At the start it's 240 bytes lower: the table of parked threads and their semaphores. - Nothing else moved by more than 0.3%:
queue_20002,780 µs against 2,789 andscale_4w322,438 against 322,104 on the S3.
The WROOM keeps creating a thread. The first build parked on every
ESP32. On the WROOM, spawn_wait took 77 µs, 0.49 of main's and 0.79 of
C's, and fault_contained 181 against 252
(its run).
But it ran out of memory where main doesn't:
- 2 of the suite's 5 boots stopped at
scale_4wwith "cannot create thread", 40,696 bytes free. Main got through all 5. With two threads parked the suite read the free heap at 91,284 bytes afterscale_2w, against main's 124,656. low_heapwas refused a data stack ("cannot reserve data stack address space") once it had held 4 workers and let them go. Main passes.heap_needs: a worker that has ended kept 16,864 of its 16,984 bytes: its parked thread.
The WROOM has no PSRAM, so a worker's data stack, its native stack and
its globals all come from the internal RAM a parked thread holds, 129 KB
of it free when the suite starts. Holding it changed how the heap was
packed, and the 32 KB a thread leaves free to park wasn't enough. A larger
margin wasn't tried. So mg_worker_create creates a thread as before on a
board without PSRAM. The WROOM's logs with the pool are the -pool ones
in the probe folder.
The WROVER-E parks. It runs the WROOM's image, but it has 8 MB of
PSRAM, so its threads park. 5 boots of the suite with main and with the
change, built in the same two worktrees as the S3's and the WROOM's, are
in the probe's runs/ (labelled wrover-main and wrover-final).
Results has its runs of main from before this change.
spawn_waittakes 77 µs against 160, 0.481. Main's had a tail: its 99th percentile was 2,201 µs and its slowest 2,276. With the change they are 99 and 129. Inspawn_costsa spawn and wait took 71 µs against 149, and 203 of its 205 spawns ran on a parked thread.- Every boot of both builds stopped at
scale_4wwith "cannot create thread": ESP-IDF couldn't create a worker's task there, as in every WROVER-E run since section 4. So the benchmarks after it didn't run on this board. Of the 11 before it, all butspawn_waitare within 0.5% of main's:queue_20002,911 µs against 2,897,pingpong25 in both. With threads parked, the free heap read 16,816 bytes lower than main's after thethreadsgroup, and 33,380 lower afterscale_2w. - Its stress programs went as on the S3:
spawn_reclaimfell 16,772 bytes under its start and no further (main: 33,228); afterabort_records' 200 waited aborts the free heap is where it was (main: 16,492 under);worker_globalsloses 0 bytes;contained_abortscatches 3,000 of 3,000; and inheap_needsa worker that has ended keeps 16,856 of its 16,976 bytes, its parked thread.event_prioandstack_usepass, andqnode_flowandregion_shrinkend as on the other ESP32s.
Checks. One board at a time, with the other ESP32 held in its bootloader; the S3 with the pool, the WROOM without it, from the final code unless named.
spawn_costs: above. Itsmemsetcheck passes.spawn_reclaimspawns and waits in a loop and records how far the free heap falls under its start. On the S3 it fell 16,768 bytes at the first spawn and never further, against 49,676 on main, where up to three finished threads' stacks waited for the idle tasks at once.abort_recordson the S3: after its 200 waited aborts the free heap is where it was (main: 16,492 bytes under), and after the unwaited and the plain ones 5,404 and 5,796 under (main: 21,896 and 22,284): a finished thread's native stack less in each.worker_globalsloses 0 bytes over its 100 rounds on the S3, now with 1.2 s waits before its baseline and at its end, so that no thread is parked at either.contained_abortscatches 3,000 of 3,000 on the S3.event_priopasses on the S3: a worker that raised its priority withtask_priority()doesn't hand it to the next.stack_use(with--stack-report, which doesn't park) passes, andqnode_flowandregion_shrinkend as before:region_shrinkwith "cannot create thread", as its header says it does on the ESP32s.- The WROOM, which doesn't park now, passes the same programs, and
heap_needsandlow_heap:low_heapholds 4 workers and ends 804 bytes under its start, against 288 on main, within what it allows for the runtime's pools.spawn_coststakes 146 µs, as on main, andworker_globalsends 1,548 bytes under its start, under its limit of 4,200.abort_recordsandspawn_reclaimgive other numbers than main's there, in steps of one finished worker's memory (16,492 bytes), as section 2 found they do from one run to the next. - Conformance in QEMU: 24 of 24 samples on the S3 and 21 of 21 on the WROOM, with the samples that don't fit skipped as before (one on the S3, four on the WROOM).
- Unit tests: 202 pass, 24 skipped.
tests/test_fixes.pyhas four new ones: the worker'spthread_createturned intomg_worker_create, the build stopping where the runtime's thread start isn't where it expects or where it keeps a thread-local that isn't on the list, and the program's thread-locals set back at a worker's entry. - Results match C's in every boot of the runs above.
- Raw data. Every log and run behind this section is in bench/results/probes/20261205-spawn-pool, with what each shows.
What it leaves.
- C with a parked task is 2.5 times faster on the S3, above. The rest of a Goose spawn wasn't timed again.
- The WROOM's spawn is where it was, 1.6 times C's.
free_heap()reads lower for up to a second after a program's last spawn, by 16.5 KB for each parked thread, two at most. A program that reads it right after its workers end sees them.- A newer Goose whose runtime keeps something new per thread stops
the build until
core.py's list is checked and extended. - The thread handle is reused. The runtime starts its workers detached
and never joins them, so nothing reads a worker's
pthread_tafter it ends.
16. Finished workers' stacks, freed as they end¶
Cause. On the WROVER-E every boot of the suite stopped at scale_4w,
before section 15 and after it. ESP-IDF
printed E pthread: Failed to create task! six or seven times, then the
program ended with goose runtime error: cannot create thread and 4.25
MB of heap free, nearly all of it PSRAM. A worker's native stack has to
be in internal RAM. A probe on e38087e, before section 15, printed the
internal RAM before each spawn, and at the first refused one every block
of it and every task
(its runs,
with its experiment.patch). In boot 2 the refused spawn was the 149th,
the fourth of a scale_4w round:
- Not a shortage. 53,247 bytes of internal RAM were free, in four blocks of 15,536, 14,648, 14,636 and 7,928 bytes. A native stack is 16,384.
- Room for four. The one block that holds 16 KB stacks is the top
111 KB of the ESP32's data RAM. With PSRAM, ESP-IDF keeps the first 32
KB of it for DMA and for internal-only allocations
(
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL), as a heap of its own, and the rest held four native stacks and 14,636 bytes. Three were the round's running workers. The fourth was a worker of the round before: finished, its task deleted, its stack not yet freed. - Why it wasn't freed. The runtime starts a worker as a detached pthread, and on ESP-IDF a detached pthread deletes its own task when it ends. A task can't free the stack it runs on, so FreeRTOS leaves that to an idle task, which runs only when its core has nothing else to do. Both idle tasks were ready and not running: the three workers, at priority 5, kept both cores. The spawn's wait of up to five ticks (section 9) ran out, and the stack was freed just after the spawn had given up.
- Why not five. Main's data stacks were in internal RAM too, six of 16 KB, and the sixth was in the 32 KB pool, which left 15,536 bytes of it: without it the pool would have held a fifth native stack. The workers' data stacks were in PSRAM. The WROOM has no PSRAM and no pool.
- With section 15's parked threads two finished threads wait for the
next spawn, holding their stacks, and the rest end as before and wait
for an idle task. On main (
e5c3555) the suite still stopped atscale_4win all 5 boots, and each boot's last line says a stack waited. - The same on the WROOM, less often. Section 12 left two things there
unexplained: 4 boots in 20 with a spawn of 2,153 to 4,303 µs, and one
boot refused
scale_4w's threads. In the WROOM's runs since, every spawn over 1,000 µs (2,025 to 4,303) came in a boot whose last line says a stack waited, and no boot without one had such a spawn: 2 to 4 ticks of waiting for an idle task. The refused boot had waited too.
Change. Four commits in components/microgoose/mg_compat.c, for the
ESP32s with PSRAM (the S3 and the WROVER-E), with --wrap=pthread_exit
in the component's CMakeLists.txt. On the WROOM-32 the new code
doesn't run: the last commit takes the first one off it, for what it
cost there (below). A fifth, 79be83d, changes only comments, so the
runs below are of e53e7d1.
- Joined as it ends (
d7a8437). A thread the runtime asks for detached is created joinable. When its function returns, it puts itself on a list, takes priority 9 and wakes a reaper task at priority 9: above the 5 to 8 the program's tasks take withtask_priority, so busy workers can't hold it off.pthread_exitthen suspends the thread. Once the thread runs on neither core, the reaper joins it, andpthread_joindeletes a task that isn't running, which frees its stack and control block there and then. Any detached pthread is made joinable this way, the program's own as well as the runtime's workers. - Off both cores first. Deleted while it still ran on the other core, the task would be left to an idle task again. At priority 9 the thread keeps its own core when it wakes the reaper, which runs on the other and waits the microseconds the thread takes to stop: up to 200 µs of spinning, then a tick at a time.
- Parked threads aren't joined. A pool thread's function returns only
when the thread ends: when it doesn't park, when a refused allocation
sends it away, or after its second with no spawn. Only then is it
reaped. Counted (
patches/reaperstack.pywithtrace),spawn_costson the WROVER-E ran 408 of its 410 spawns on a parked thread, and the reaper joined 102 threads: the 100 empty detached pthreads the program times, and two pool threads that ended. - A refused allocation joins too. A spawn, a data stack or a worker's globals that finds no room first sends the parked threads away, as before, then joins any stopped thread the reaper hasn't reached, then waits up to five ticks as before. The count on the program's last line is of the allocations that found no room at first, whether or not they waited a tick.
- The reaper's cost. It starts with the first worker, so a program
without workers doesn't pay for it: 2 KB of stack and a control block,
in internal RAM, from then on. A thread that ends through
pthread_exitis put on the list there. - A worker's data stack keeps out of broken-up internal RAM
(
a47d689). With PSRAM, a worker's data stack took internal RAM while the free total left 64 KB beside it. Now it also needs one block that holds it and the 64 KB, and otherwise goes to PSRAM, as it did when the free total was short. Without PSRAM nothing changes: there the alternative is to refuse the stack. With the first commit alone,low_heapon the WROVER-E was refused a native stack with 59 KB of internal RAM free, none of it in a piece over 15 KB: the next workers' data stacks had taken the places of the stacks just freed, where on main those were still held (below). - A parked thread reads its own handle before it parks (
259161d). Section 15's pool thread calledpthread_self()inside the critical section it parks in.pthread_selftakes pthread's own lock, a mutex, and inside a critical section newlib can't wait for one: if another task holds it at that moment, creating, joining or ending a pthread, newlib aborts the program:abort() was called, with no message. With the reaper joining threads as they end,spawn_costsaborted that way in every run on the WROVER-E, within its first 205 spawns, and with the reaper's stack at 2,560 bytes or more it didn't: it depends on timing. The thread now reads its handle once, as it starts. Main has the same code, butspawn_costsran to its end there on the S3 and the WROVER-E, the boards that park. - Only with PSRAM (
e53e7d1). On the WROOM-32 the reaper joined every worker's thread, since none park there, and the suite'sspawn_waittook 234 µs against main's 157. So on a board without PSRAM a detached thread deletes itself as before, and a refused allocation waits for an idle task, as on main.
Before and after. 5 boots each, main (e5c3555) and the change
(e53e7d1) built on the same day in worktrees of one path length. Main
stopped at scale_4w in every boot on the WROVER-E, so 11 of its
benchmarks have no "before" there.
- WROVER-E. Before:
20261205T180520Z-esp32-goose-e5c3555-wrover-main.
After:
20261205T203655Z-esp32-goose-e53e7d1-wrover,
and with
--worker-reserve 4K20261205T204127Z-esp32-goose-e53e7d1-wrover-worker-reserve-4k: the board's columns in Results and the report now. - S3. Before: 20261205T180921Z-s3-goose-e5c3555-main. After: 20261205T210501Z-s3-goose-e53e7d1.
- WROOM-32, where the change runs main's code. Before:
20261205T181531Z-esp32-goose-e5c3555-main.
After:
20261205T204552Z-esp32-goose-e53e7d1,
and with
--worker-reserve 4K20261205T205012Z-esp32-goose-e53e7d1-worker-reserve-4k.
The S3's and the WROOM's runs stay with the probe's data, and Results and
the report keep section 13's runs for
those boards. ÷ c is against each board's published C.
| WROVER-E, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,702 | 3,702 | 1.000 | 0.89 |
heapsort |
5,490 | 5,493 | 1.001 | 1.92 |
mandel_f64 |
171,317 | 171,317 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
2,824 | 2,873 | 1.017 | 0.64 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
77.5 | 84 | 1.084 | 0.85 |
scale_main |
— | 647,585 | — | 1.03 |
scale_1w |
652,855 | 652,857 | 1.000 | 1.04 |
scale_2w |
355,405 | 355,397 | 1.000 | 1.03 |
scale_4w |
— | 355,060 | — | 1.03 |
timer_idle_gap |
— | 1,000 | — | 1.00 |
timer_idle_wake |
— | 12 | — | 1.20 |
timer_busy_gap |
— | 1,000 | — | 1.00 |
timer_busy_wake |
— | 12 | — | 0.01 |
pin_idle_gap |
— | 1,002 | — | 1.00 |
pin_idle_wake |
— | 12 | — | 1.20 |
pin_busy_gap |
— | 1,002 | — | 1.00 |
pin_busy_wake |
— | 12 | — | 0.02 |
fault_contained |
— | 234 | — | — |
| S3, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
855 | 855 | 1.000 | 1.00 |
crc32 |
3,567 | 3,567 | 1.000 | 0.90 |
heapsort |
4,735 | 4,735 | 1.000 | 1.96 |
mandel_f64 |
166,166 | 166,167 | 1.000 | 1.00 |
mandel_f32 |
7,452 | 7,451 | 1.000 | 1.03 |
matmul_f32 |
1,688 | 1,688 | 1.000 | 1.31 |
queue_2000 |
2,759 | 2,785 | 1.009 | 0.81 |
pingpong |
25 | 25 | 1.000 | 1.67 |
spawn_wait |
54 | 53 | 0.981 | 0.65 |
scale_main |
630,228 | 630,228 | 1.000 | 1.03 |
scale_1w |
625,762 | 625,757 | 1.000 | 1.02 |
scale_2w |
321,669 | 321,661 | 1.000 | 1.02 |
scale_4w |
322,438 | 322,481 | 1.000 | 1.02 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
9 | 10 | 1.111 | 1.25 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
10 | 10 | 1.000 | 0.01 |
pin_idle_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_idle_wake |
10 | 10 | 1.000 | 1.25 |
pin_busy_gap |
1,003 | 1,003 | 1.000 | 1.00 |
pin_busy_wake |
11 | 11 | 1.000 | 0.02 |
fault_contained |
134 | 139 | 1.037 | — |
| WROOM-32, medians | before (µs) | after (µs) | after ÷ before | after ÷ c |
|---|---|---|---|---|
clock_1000 |
584 | 584 | 1.000 | 1.00 |
crc32 |
3,701 | 3,702 | 1.000 | 0.89 |
heapsort |
5,490 | 5,490 | 1.000 | 1.92 |
mandel_f64 |
171,316 | 171,316 | 1.000 | 1.00 |
mandel_f32 |
8,425 | 8,425 | 1.000 | 1.08 |
matmul_f32 |
1,961 | 1,961 | 1.000 | 1.37 |
queue_2000 |
2,925 | 2,928 | 1.001 | 0.62 |
pingpong |
25 | 25 | 1.000 | 1.19 |
spawn_wait |
157 | 159 | 1.013 | 1.62 |
scale_main |
647,585 | 647,585 | 1.000 | 1.03 |
scale_1w |
650,345 | 652,937 | 1.004 | 1.04 |
scale_2w |
355,188 | 355,176 | 1.000 | 1.03 |
scale_4w |
355,517 | 355,574 | 1.000 | 1.03 |
timer_idle_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_idle_wake |
12 | 12 | 1.000 | 1.20 |
timer_busy_gap |
1,000 | 1,000 | 1.000 | 1.00 |
timer_busy_wake |
12 | 11 | 0.917 | 0.01 |
pin_idle_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_idle_wake |
12 | 12 | 1.000 | 1.20 |
pin_busy_gap |
1,002 | 1,002 | 1.000 | 1.00 |
pin_busy_wake |
12 | 12 | 1.000 | 0.02 |
fault_contained |
242 | 322 | 1.331 | — |
What moved.
- The WROVER-E runs the suite to its end in 5 boots of 5, as built
and with
--worker-reserve 4K, and no boot's last line says a stack waited. Its compute and scaling medians are within 0.9% of the WROOM-32's published ones, andscale_4wtakes 355,060 µs, 1.03 of C's. Of the 11 benchmarks main got through, onlyqueue_2000andspawn_waitmoved by more than 0.1%.queue_2000moves from boot to boot there: 2,728 to 2,971 µs a boot with the change, 2,767 to 2,972 on main. spawn_waiton the WROVER-E takes 84 µs against 77.5, 1.084, and 84 with--worker-reserve 4K. Inspawn_costsa spawn and wait takes 71 µs with both builds, 408 of its 410 spawns on a parked thread with both, so the parked spawn itself costs no more. The suite's loop moved from build to build when thethreadsgroup ran alone (in the probe'sruns/): 77 µs on main, 81 on259161dand 85 on259161dwithout the placement rule. What makes the suite's 84 wasn't found.fault_containedon the WROVER-E takes 234 µs, and 242 with--worker-reserve 4K. Main never got there.- The S3 moved by under 1% but for
fault_contained, 139 µs against 134, which the same code gave as 125 in259161d's run, andtimer_idle_wake, 10 µs against 9. - The WROOM-32 runs main's code, and is slower at two things.
fault_containedtakes 322 µs against 242, in every boot, and 330 with--worker-reserve 4Kagainst the published run's 256. Inspawn_costsa spawn and wait takes 173 µs against 158, and an empty detached pthread 125.2 against 109.6. The suite'sspawn_waittakes 159 against - A thread started there goes through none of the change's new code, only a check for PSRAM whose answer is kept. What makes the difference wasn't found (below).
- The free heap reads 2,200 bytes lower on the S3 and the WROVER-E
from the first worker on, the reaper's stack and control block: after
the
threadsgroup, 4,296,955 bytes against 4,299,155 on the WROVER-E. At the start it's 12 to 16 bytes lower on every board. - Size. The image grows by 1,284 to 1,340 bytes and static RAM by 16, on each board: 269,218 bytes on the WROVER-E against 267,934.
The WROOM-32's slower faults. The faults group alone, 5 boots, and
spawn_costs, on the WROOM, with things added to each build that its
code never runs (layout_pad.py, comppad.py, mainpad.py and
nowrapexit.py in the probe folder; the runs are in its runs/):
| build | fault_contained (µs) |
spawn_costs: a spawn and wait (µs) |
|---|---|---|
| main | 262 | 158 |
main, 256 unused nops at the end of mg_hal.c |
265 | 158 |
| main, 1,024 of them | 265 | 158 |
main, 1,024 at the top of mg_compat.c |
268 | 158 |
| main, a thread-local pointer, as the change adds | 264 | 158 |
| main, 16 bytes of static RAM, as the change adds | 266 | 158 |
| the change | 334 | 173 |
the change, 256 unused nops at the end of mg_hal.c |
337 | 173 |
| the change, 1,024 of them | 337 | 173 |
the change, 1,024 at the top of mg_compat.c |
339 | 170 |
the change without --wrap=pthread_exit |
350 | 162 |
Main stays at 262 to 268 µs a fault with each, and the change at 334 to
350, 66 µs or more above any of main's. Section 14 saw the WROOM's fault_contained
move between 256.5 and 332 µs in builds that added no instruction to it,
and unused code didn't move it either. What the change does to it here
isn't known.
The reaper on the WROOM-32. Each column is one build on 259161d, the
change before its last commit, with the reaper as it was and in three
other forms, each patched in for the trial (patches/ in the probe
folder). The suite's medians come from 5 boots; the rest is one run of
each stress program.
| WROOM-32 | main (e5c3555) |
reaper at priority 9 (the change) | reaper at priority 1 (lazy) |
lazy with its stack in static RAM (lazystatic) |
ending at 9, reaper at 1, stack in static RAM (hybrid) |
|---|---|---|---|---|---|
spawn_wait (µs) |
157 | 234 | 169 | 174 | |
fault_contained (µs) |
242 | 561 | 369.5 | 360 | |
spawn_costs: a spawn and wait (µs) |
158 | 225 | 190 | 189 | 182 |
spawn_costs: an empty detached pthread (µs) |
109.6 | 172.3 | 120.0 | 120.7 | 128.4 |
spawn_reclaim: lowest free heap, under its start |
82,668 | 18,972 | 68,596 | 83,048 | 66,456 |
heap_needs: kept after the first worker ends |
296 | 2,460 | 2,364 | 216 | 312 |
low_heap |
passes | refused a data stack | refused a data stack | refused a data stack | refused a data stack |
- Every spawn pays for the join. Joining a thread at priority 9 puts
its end, and
pthread_join's own work, on the core the program runs on at the moment it ends, ahead of the next spawn. A reaper just above the idle tasks costs less, since it joins when a core has nothing else to do, but still 12 to 32 µs a spawn, and 118 to 128 infault_contained. heap_needsfinds the reaper's stack and control block, about 2 KB, held from the first worker on, except where its stack is in static RAM.spawn_reclaimfell 18,972 bytes under its start, against 82,668: without a reaper at priority 9, several finished threads' stacks wait for an idle task at once.low_heapwas refused a data stack taking its four workers again, with every reaper. Its 2 KB isn't why: main holding 2,148 bytes of internal RAM from its first spawn on, no reaper (patches/mainfoot.py,low_heap-esp32-main-2k.log), passes, 384 bytes under its start. Why the joins move the refusal there wasn't measured.- On the WROVER-E the slower reapers don't do the job. With
lazyand withhybrid, the suite stopped atscale_4win 5 boots of 5, after 7 to 13Failed to create task!each, as on main. With workers on both cores a reaper at priority 1 doesn't run, as the idle tasks don't. A refused spawn joins the stopped threads itself, but a reaper that takes the list and is then preempted keeps it from the spawn. That is the likely way they fail, not checked.
low_heap on the WROVER-E. The program is written for a board
without PSRAM: it takes workers until internal RAM won't hold another,
lets them go, takes as many again 20 times, and checks that the free heap
ends where it started. On the WROVER-E it fails before the change and
after it, at different places, and that is where the change's cost
shows. Each line is one run, in the probe folder:
| build | workers held | taken again 20 times | free heap at the end |
|---|---|---|---|
main (e5c3555) |
5 | yes | 17,548 bytes under its start: fails |
the reaper alone (d7a8437) |
5 | refused a native stack | |
| main with the placement rule alone | 8 | yes | 17,320 under: fails |
| the same, holding 2,148 bytes from its first spawn on | 8 | refused a native stack | |
| the change | 8 | refused a native stack | |
the change with lazy, hybrid or lazystatic |
8 | refused a native stack |
- Main holds five because a worker's data stack takes internal RAM beside its native stack while the free total leaves 64 KB. With the placement rule the data stacks go to PSRAM, since no block of internal RAM holds one and 64 KB more (the largest is 77,824 bytes at the start), and eight native stacks fit.
- The reaper alone freed each finished worker's stacks at once, and the next workers' data stacks took their places: the refused native stack found 58,947 bytes free, the largest piece 15,360. That is what the placement rule is for.
- The eighth worker isn't taken again with the reaper's 2 KB in
internal RAM. Main with the placement rule and 2,148 bytes held from its
first spawn, no reaper (
patches/mainfoot.py), is refused at the same place, and so is a reaper whose stack is in static RAM, which comes out of the same internal RAM. At the refusal, with the change, 60,635 bytes were free and the largest piece was 14,848 (low_heap-wrover-heapwalk.log). - Main's end check comes 20 ms after its last workers end, while up
to two finished threads can still be parked
(section 15), and finds 17,548 bytes
held where it allows 4,200.
low_heapdoesn't wait a second for them, asworker_globalsdoes.
Checks. One board at a time, the S3 and the WROOM never awake
together while either ran, with e53e7d1 unless named.
- The reaper doesn't take a parked thread: above, 102 joins in
spawn_costs' 410 spawns, 408 of them on a parked thread. - Parking alone doesn't get the WROVER-E through: main, which parks,
stopped at
scale_4win 5 boots of 5 (Cause, above). - The WROVER-E's stress programs.
spawn_reclaimfell 19,048 bytes under its start and no further (main: 16,772), the reaper's 2 KB more. Afterabort_records' 200 waited aborts the free heap is where it was, as on main, and after the unwaited and the plain ones 6,172 and 6,536 bytes under (main: 6,152 and 23,096).worker_globalsloses 0 bytes over its 100 rounds (main: 1,548).contained_abortscatches 3,000 of 3,000. Inheap_needsa worker that has ended keeps 19,048 bytes: its parked thread, and the reaper's stack, started by that first worker (main: 16,856).sync_stress,qnode_stress,event_prioandstack_usepass, andqnode_flowandregion_shrinkend as on main:region_shrinkwith "cannot create thread", as its header says it does on the ESP32s.low_heap: above. - The S3's stress programs.
spawn_reclaimfell 18,952 bytes under its start and no further, against 16,768: the reaper's 2 KB more. Afterabort_records' waited, unwaited and plain aborts the free heap is 0, 6,152 and 6,536 bytes under (main: 0, 6,184 and 6,536).worker_globalsloses 1,548 bytes (main: 0), under its limit of 4,200: its runs in this section's and section 15's probes lost 0 to 1,548, in steps of 516, with main and with the change.contained_abortscatches 3,000 of 3,000,sync_stress,qnode_stress,event_prioandstack_usepass, andqnode_flowandregion_shrinkend as on main. - The WROOM-32's stress programs pass as on main.
low_heapholds 4 workers, takes them again 20 times and ends 384 bytes under its start (main: 900).worker_globalsloses 516 bytes, as on main. Inheap_needsa worker that has ended keeps 312 bytes (main: 296).spawn_reclaimfell 66,180 bytes under its start, against 82,668, andabort_recordsgives other numbers than main's, in steps of one finished worker's memory (16,492 bytes), as section 2 found they do from one run to the next. - Conformance in QEMU: 24 of 24 samples on the S3 and 21 of 21 on the WROOM, with the samples that don't fit skipped as before (one on the S3, four on the WROOM).
- Unit tests: 203 pass, 24 skipped, as on main. The change is in the port's C, which the stress programs above check on the boards.
- Results match C's in every boot of the runs above.
- Raw data. Every log and run behind this section is in bench/results/probes/20261205-worker-reaper, with what each shows.
Not taken.
- Native stacks in PSRAM. ESP-IDF can put a task's stack in PSRAM
(
CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM, off by default), but such a task must not run while the flash cache is off, so a flash write from the file tree or NVS would have to go through another task. It also leaves the cause in place on every board. - Main's sixth data stack out of the pool. That gives the WROVER-E a fifth place for a native stack, and the same race at five workers.
- A longer wait. While workers keep both cores, no number of ticks makes an idle task run.
- Joining in
thread_wait. It would put the join on main's path, and a worker nobody waits for would still be freed only by an idle task. - A reaper that waits for an idle core (
lazy,hybrid), with or without its stack in static RAM: cheaper on the WROOM, but the WROVER-E's suite stopped atscale_4wagain (above). - The reaper on the WROOM-32. It made every spawn slower there and
broke
low_heap, for a refusal seen in one boot of 15 and a slow spawn in 4 boots of 20.
What it leaves.
- The WROOM-32 still waits for an idle task. A finished worker's
stack is freed there as before, so the slow spawns and the refusal
section 12 saw can still
come: there, a spawn of 2 to 4 ms in 4 boots of 20, and
scale_4w's threads refused in one boot of 15 as built. - A program's own detached pthreads are joined by the reaper too, on
the boards with PSRAM, and their creator pays for it: in
spawn_costsan empty one, created detached and waited for, takes 119.7 µs against 83.0 on the S3, and 175.5 and 195.3 in two runs against 115.7 on the WROVER-E. The runtime's workers there mostly run on parked threads, which aren't joined. - The WROOM-32's contained faults are slower, 322 µs against 242 in
the suite, and its spawns in
spawn_costs173 against 158, with none of the change's new code running there. Why wasn't found (above). - 2 KB of internal RAM go to the reaper from the first worker on, on
the S3 and the WROVER-E. On the WROVER-E that is the eighth worker
low_heapcan't take again. low_heapon the WROVER-E still fails, at another place than before: it's written for a board without PSRAM.- Not measured: why a reaper that waits for an idle core lets the
WROVER-E's
scale_4wstop, and why every reaper tried made the WROOM'slow_heaprefuse a data stack. - Main has the race
259161dfixes until this change is merged: a parked thread that reads its handle while another task creates, joins or ends a pthread aborts the program. Main's runs didn't hit it.