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 <michael@membrowse.com>
This commit is contained in:
Michael Rogov Papernov authored and Beat Küng committed 2026-10-20 16:45:03 +02:00
1 parent 878877ad02
commit 3fe7acc19d
3 files changed
+65 -7

No files matched your search

@@ -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
+2 -2
View File
@@ -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})
+51
View File
@@ -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 <px4_platform_common/events.h>
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 */