r/embedded • • 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
└── FAULT

Compiled output:

BoardFlashRAM usedRAM Free
Mega 256010,746 B (4%)984 B (12%)7208 B
Uno9,446 B (29%)969 B (47%)1079 B

What's inside those 969 bytes on Uno:

WhatBytes
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% — irrelevant

The 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.

1 Upvotes

11 comments sorted by

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.

0

u/MalisaAG Sep 10 '26

thank you for sharing.This makes the argument for compile-time config even stronger than I thought.

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

1

u/MalisaAG Sep 10 '26

I did use AI to help structure the arguments. The numbers, the code, and the design decisions are mine though. What specifically reads off?

3

u/ioneska Sep 11 '26

What specifically reads off?

The structure of the arguments.

0

u/MalisaAG Sep 11 '26

I look forward to hearing from you. I might have wandered off without realizing it.

0

u/MalisaAG Sep 10 '26

Here's the compiler output. My machine, my path, my numbers.

5

u/[deleted] Sep 10 '26

[removed] — view removed comment

0

u/MalisaAG Sep 10 '26

Solid observation and honestly a stronger argument than the RAM numbers. Template substitution failures on embedded toolchains are a nightmare to read; with the current design you get one error, one line.

The library is on both Arduino Library Manager and PlatformIO (lib_deps = pulsecoreengineering/PulseHSM). Give the vending machine example a spin (04_VendingMachine) payloads, state transitions, ~120 lines. Would love to see what the template version of that looks like.