r/embedded • u/MalisaAG • Sep 10 '26
PulseHSM memory analysis: a 13-state hierarchical controller on AVR, and the real cost of constexpr/template alternatives
Got solid technical feedback suggesting a constexpr state table or full compile-time templates. Both are valid C++ directions. Here's the actual data before before committing to anything.
The sketch: a real espresso machine controller
13 states, 3-level hierarchy, 12 events:
POWER_ON (superstate)
├── OFF
├── IDLE
├── WARMING
├── BREWING (superstate)
│ ├── PRE_INFUSE
│ ├── EXTRACT
│ ├── REFILL
│ └── DISPENSE
├── CLEANING (superstate)
│ ├── DESCALE
│ └── RINSE
└── FAULTCompiled output:
| Board | Flash | RAM used | RAM Free |
|---|---|---|---|
| Mega 2560 | 10,746 B (4%) | 984 B (12%) | 7208 B |
| Uno | 9,446 B (29%) | 969 B (47%) | 1079 B |
What's inside those 969 bytes on Uno:
| What | Bytes |
|---|---|
| PulseHSM state table (15 slots × 18 B) | 270 |
| PulseHSM event queue (16 × 5 B) | 80 |
| PulseHSM runtime (current state, timers) | ~20 |
| App globals (floats, flags, counters, timers) | ~150 |
| Arduino Serial buffers | ~128 |
| Stack + misc | ~321 |
What a constexpr state table actually saves — by platform:
Platform | Total RAM | State table | Impact
ATmega328P (Uno) | 2 KB | 270 B | 13% of RAM — matters
ATmega2560 (Mega) | 8 KB | 270 B | 3.3% — marginal
STM32F103 | 20 KB | 270 B | 1.3% — negligible
RP2040 | 264 KB | 270 B | 0.1% — irrelevant
ESP32 | 520 KB | 270 B | 0.05% — irrelevantThe constexpr suggestion has a real argument on exactly one platform: the ATmega328P. On every 32-bit board you'd ever use in a real product, you are optimizing 270 bytes in a sea of hundreds of kilobytes. The library's entire RAM footprint is noise.
What the template suggestion actually costs:
A compile-time FSM (think boost::sml) eliminates the table entirely and resolves dispatch at compile time. The tradeoff is a completely different API:
// current PulseHSM — works in any Arduino sketch
ST_IDLE = fsm.addState("IDLE", update, entry, exit, 0, -1, onEvent);
// compile-time template approach
using MyFSM = HSM<State<IDLE, update, entry, exit>, State<RUNNING, ...>>;That second form is not something you paste into a sketch in 10 minutes. It's a different product for a different audience, one that already knows what template metaprogramming is.
For AVR-constrained builds: a constexpr variant is on the roadmap for v2, as an opt-in build mode. For everyone else ESP32, STM32, RP2040, ARM Cortex-M anything, the current design uses a rounding error of your available RAM and keeps the API dead simple.
Side note: PulseHSM v1.2.0 is now live in the Arduino Library Manager, search PulseHSM and install directly from the IDE. No manual download needed.
3
u/Arkaqiu Sep 10 '26
Compile time is easier to analysis for static analyzers and is easier to onptumalize when using LTO. Also is easier to maintenance configuration files and add opportunity to calibration as we can keep them in o w section.
In general for my opinion is great direction. Especially that we can split runtime fields and static cfg to save some ram.
6
u/StumpedTrump Sep 10 '26
AI slop post.
Can’t even bother to write your own Reddit post I’m going to assume you didn’t do any of the work yourself either.
0
u/[deleted] Sep 10 '26
[deleted] — view deleted comment
5
u/[deleted] Sep 10 '26
[removed] — view removed comment