From 3fe7acc19dffcd694e2fed5a86f7a913701202fe Mon Sep 17 00:00:00 2001 From: Michael Rogov Papernov Date: Mon, 19 Oct 2026 16:42:14 +0100 Subject: [PATCH] refactor(events): move the message-less send() out of line events::send(id, log_levels, message) was an inline function, so every module using it got either a private clone or the body inlined at each call site. The message is never part of the sent event: it only feeds the build-time event generator and the optional console print. Keep a thin inline wrapper so the compiler still drops the unused message string, and build the event in one out-of-line function that takes the two log levels as scalars to keep the call sites small. This saves 1760 bytes of FLASH on px4_fmu-v6x and 1184 bytes on px4_fmu-v2. Assisted-by: Claude:claude-fable-5-1 Signed-off-by: Michael Rogov Papernov --- .../include/px4_platform_common/events.h | 17 +++++-- src/lib/events/CMakeLists.txt | 4 +- src/lib/events/events_send.cpp | 51 +++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/lib/events/events_send.cpp diff --git a/platforms/common/include/px4_platform_common/events.h b/platforms/common/include/px4_platform_common/events.h index b3dad933ba5..f036aea806c 100644 --- a/platforms/common/include/px4_platform_common/events.h +++ b/platforms/common/include/px4_platform_common/events.h @@ -118,13 +118,20 @@ inline void send(uint32_t id, const LogLevels &log_levels, const char *message, send(e); } +namespace util +{ +/** + * publish/send an event without arguments. The message is deliberately not passed: it is only + * needed by the event generator and the optional console output, so the compiler can drop it. + * The levels are passed as two scalars so that each call site stays a few instructions. + */ +void sendWithoutMessage(uint32_t id, Log external, LogInternal internal); +} // namespace util + inline void send(uint32_t id, const LogLevels &log_levels, const char *message) { - event_s e{}; - e.log_levels = ((uint8_t)log_levels.internal << 4) | (uint8_t)log_levels.external; - e.id = id; - CONSOLE_PRINT_EVENT(e.log_level_external, e.id, message); - send(e); + CONSOLE_PRINT_EVENT(log_levels.external, id, message); + util::sendWithoutMessage(id, log_levels.external, log_levels.internal); } static constexpr uint16_t initial_event_sequence = 65535 - 10; // initialize with a high number so it wraps soon diff --git a/src/lib/events/CMakeLists.txt b/src/lib/events/CMakeLists.txt index fcdb971f642..f3efb10fd7a 100644 --- a/src/lib/events/CMakeLists.txt +++ b/src/lib/events/CMakeLists.txt @@ -122,8 +122,8 @@ add_dependencies(prebuild_targets events_header) # Build the interface(s) if (${PX4_PLATFORM} STREQUAL "nuttx" AND NOT CONFIG_BUILD_FLAT) list(APPEND EXTRA_SRCS events_ioctl.cpp) - add_library(usr_events usr_events.cpp) + add_library(usr_events usr_events.cpp events_send.cpp) add_dependencies(usr_events prebuild_targets) endif() -px4_add_library(events events.cpp ${EXTRA_SRCS}) +px4_add_library(events events.cpp events_send.cpp ${EXTRA_SRCS}) diff --git a/src/lib/events/events_send.cpp b/src/lib/events/events_send.cpp new file mode 100644 index 00000000000..7728b2308bf --- /dev/null +++ b/src/lib/events/events_send.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + + +#include + +namespace events +{ +namespace util +{ + +void sendWithoutMessage(uint32_t id, Log external, LogInternal internal) +{ + event_s e{}; + e.log_levels = ((uint8_t)internal << 4) | (uint8_t)external; + e.id = id; + send(e); +} + +} /* namespace util */ +} /* namespace events */