Electronic – Embedded programming state machines

embeddedprogrammingstate-machines

I am looking at implementing a non-trivial finite state machine (specified as a UML hierarchical statechart) on a 32-bit MCU with gcc.

Are there any rules of thumb what works better and what works less well? My gut says that a switch-based (or even computed goto) implementation should be slightly more performant while a function-pointer based transition table is generally reputed to be more maintainable.

Also: has anybody evaluated Boost MSM for embedded applications? I know that Boost MSM is generally lauded as being very efficient, but for embedded applications efficiency might be measured differently than in the world of PC programming.

Does anybody know what the compiled state machine engine of MSM looks like? More like a switch jumptable or more like a function pointer transition table? Does it use dynamic memory allocation or can it used statically?

Best Answer

I'd be surprised if there's a big difference on a 32-bit MCU. Avoiding conditional branches could save you a few cycles, but are you really going to succeed or fail based on a few cycles? The number of wait states on your RAM and ROM are probably at least as important. So is the CPU instruction set.

Premature optimization is the root of all evil. Start with what's easier to implement and maintain, and only optimize where necessary based on profiling.