From 7ab1ea82d91fca8b720d70cd2f445cc63e3e9440 Mon Sep 17 00:00:00 2001 From: Pavel Guzenfeld Date: Sat, 7 Nov 2026 19:40:18 +0200 Subject: [PATCH] fix: reduce on_entry/on_exit code bloat at -Oz on ARM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The entry/exit dispatch chain (process_internal_event → process_internal_generic_event → process_event_impl → switch_stm::dispatch) instantiates per event type. At -O2 these inline away; at -Oz the lower inline threshold keeps each as a separate function, tripling .text on cortex-m0. Mark the chain BOOST_SML_DETAIL_ALWAYS_INLINE (__attribute__((always_inline)) / __forceinline) so the optimizer folds them regardless of -Oz threshold. Measured on clang 23, --target=arm-arm-none-eabi -mcpu=cortex-m0 -Oz: .text with on_entry/on_exit: 2343 → 1321 bytes (−44%) .text without entry/exit: 701 → 701 (no regression) -O2 / -Os: unchanged Closes #494 --- include/boost/sml.hpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/include/boost/sml.hpp b/include/boost/sml.hpp index a96925e6..741effd8 100644 --- a/include/boost/sml.hpp +++ b/include/boost/sml.hpp @@ -25,6 +25,7 @@ } #if defined(__clang__) #define BOOST_SML_DETAIL_UNUSED __attribute__((unused)) +#define BOOST_SML_DETAIL_ALWAYS_INLINE __attribute__((always_inline)) #define BOOST_SML_DETAIL_VT_INIT \ {} // Zero-size array trick shrinks SM objects but triggers UBSan "insufficient @@ -46,6 +47,7 @@ #define __has_builtin(...) 0 #endif #define BOOST_SML_DETAIL_UNUSED __attribute__((unused)) +#define BOOST_SML_DETAIL_ALWAYS_INLINE __attribute__((always_inline)) #define BOOST_SML_DETAIL_VT_INIT \ {} // Zero-size array trick shrinks SM objects but triggers UBSan "insufficient @@ -68,6 +70,7 @@ #define __has_builtin(...) __has_builtin##__VA_ARGS__ #define __has_builtin__make_integer_seq(...) 1 #define BOOST_SML_DETAIL_UNUSED +#define BOOST_SML_DETAIL_ALWAYS_INLINE __forceinline #define BOOST_SML_DETAIL_VT_INIT #define BOOST_SML_DETAIL_ZERO_SIZE_ARRAY(...) static_assert(true, "") #define BOOST_SML_DETAIL_ZERO_SIZE_ARRAY_CREATE(...) __VA_ARGS__ ? __VA_ARGS__ : 1 @@ -1817,12 +1820,12 @@ struct branch_stm { }; struct switch_stm { template - constexpr static bool dispatch(sm_impl &, State &, const TEvent &, TDeps &, TSubs &, const aux::type_list<> &) { + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr static bool dispatch(sm_impl &, State &, const TEvent &, TDeps &, TSubs &, const aux::type_list<> &) { return false; } template - constexpr static bool dispatch(sm_impl &self, State ¤t_state, const TEvent &event, TDeps &deps, TSubs &subs, + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr static bool dispatch(sm_impl &self, State ¤t_state, const TEvent &event, TDeps &deps, TSubs &subs, const aux::type_list &) { switch (current_state) { case N: @@ -2241,7 +2244,7 @@ struct sm_impl : aux::conditional_t, events_ids_t>::value)> - constexpr bool process_internal_generic_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_internal_generic_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { policies::log_process_event(aux::type_wrapper{}, d, event); #if BOOST_SML_DISABLE_EXCEPTIONS return process_event_impl, mappings>>(event, d, subs, states_t{}, @@ -2254,12 +2257,12 @@ struct sm_impl : aux::conditional_t, events_ids_t>::value && !aux::is_base_of, events_ids_t>::value)> - constexpr bool process_internal_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_internal_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { return process_internal_generic_event(event, d, subs, current_state); } template , events_ids_t>::value)> - constexpr bool process_internal_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_internal_event(const TEvent &event, TDeps &d, TSubs &subs, state_t ¤t_state) { policies::log_process_event(aux::type_wrapper{}, d, event); #if BOOST_SML_DISABLE_EXCEPTIONS return process_event_impl, mappings>>(event, d, subs, states_t{}, current_state); @@ -2269,12 +2272,12 @@ struct sm_impl : aux::conditional_t - constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, aux::index_sequence<0>) { return dispatch_t::template dispatch<0, TMappings>(*this, current_state_[0], event, d, subs, states); } template - constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, aux::index_sequence) { auto handled = false; #if defined(__cpp_fold_expressions) @@ -2287,7 +2290,7 @@ struct sm_impl : aux::conditional_t - constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, + BOOST_SML_DETAIL_ALWAYS_INLINE constexpr bool process_event_impl(const TEvent &event, TDeps &d, TSubs &subs, const aux::type_list &states, state_t ¤t_state) { return dispatch_t::template dispatch<0, TMappings>(*this, current_state, event, d, subs, states); }