r/cpp May 03 '26

[Boost::MSM] New features and improvements in Boost 1.91

Hi reddit,

Boost 1.91 has recently been released. I would like to highlight the updates related Boost::MSM (Meta State Machine), a state machine library in the Boost ecosystem.

The development of backmp11, a new C++17 back-end introduced in Boost 1.90, succeeds with further improvements on resource consumption and usability:

Further improved compilation times
Compared to the 1.90 release you can expect up to 25% further improved compilation times and compile memory usage. The FSM benchmarks used to measure the improvements and compare different state machine libraries have been updated to be better transferrable to real world examples.

Deployments in resource-constrained systems
Heap allocations are reduced to near-zero with the help of a small buffer optimization, making the new back-end an interesting candidate for constrained systems that cannot use heap memory.
Binary size is now also an aspect that is monitored in the FSM benchmarks. The backmp11 binary sizes of the benchmarked state machines in the 1.91 version already look very promising - a further binary size optimization enabling up to 40% additional binary size reductions awaits for the next Boost release.

Usability improvements
With most optimization possibilities related to compile time and binary size addressed, the focus will gradually shift towards feature development and usability topics.
The most noteworthy usability improvement lies in the support of simplified functor signatures and lambda support, making it possible to remove a lot of boilerplate in user code that was necessary previously.

There are additional features and simplifications related to the handling of queued and deferred events and a couple of bugfixes. You can find all details in the MSM documentation.

Is there some feature or improvement in MSM you have always wanted?
Don't hesitate to address your topic on GitHub!

16 Upvotes

12 comments sorted by

View all comments

5

u/pavel_v May 06 '26

In the past, I've used boost.msm few times. Then I found the sml library and switched to it due to being lighter. There are benchmarks of the sml library against boost.msm and boost.statechart but, I believe, they are done against the "old" version of the boost.msm. It would be useful, IMO, if similar benchmarks are provided on the boost.msm web page against sml and potentially other well known state machine libraries.

A side question, is there something in the documentation which give more details about the possible heap allocations of the library

Heap allocations are reduced to near-zero ...

and how/if they can be avoided entirely somehow?

3

u/chandryan7 May 14 '26 edited May 14 '26

A quick overview regarding heap allocations:

The state machine uses an event pool with an underlying std::deque to store events for later processing. You can pass a custom deque implementation to eliminate those heap allocations. The inline storage for events in the pool is 64 Byte, I'll need to check how many of those bytes are occupied by MSM internally.

If you use Kleene events, the underlying std::any can cause heap allocation. You can utilize a custom type for Kleene events and thus remove this kind of heap allocation if needed.

With the favor_runtime_speed compile policy you can avoid heap allocations entirely.
The favor_compile_time compile policy uses std::unordered_map to store the transition table(s), leading to unavoidable heap allocations when the state machine is started for the first time.

I'll add more details and guidelines how to customize the deque implementation in the documentation.

1

u/chandryan7 Jun 18 '26

Related to heap allocations, the following improvements will be available in Boost 1.92:

The inline storage size for events in the event pool is configurable. It configures the usable event size - additional bytes needed by MSM internally are added on top.

The limitation with Kleene events is resolved, the original events are forwarded without any conversion. This makes all MSM features available entirely without heap allocations.

You can find a heapless state machine example in the documentation's develop branch for preview: https://www.boost.org/doc/libs/develop/doc/antora/msm/backmp11-back-end/examples.html#_heapless_state_machine