From f6249b453cb076fc0cced24c64a4a1f4310e33a5 Mon Sep 17 00:00:00 2001 From: Balduin Date: Mon, 31 Aug 2026 13:45:00 +0200 Subject: [PATCH] refactor(drivers/uavcan): de-templatize GlobalDataTypeRegistry::registerDataType registerDataType expanded the full registration logic once per data type, 56 times on px4_fmu-v6x_default. Move the type-independent body into an out-of-line registerDataTypeImpl() so the template only constructs the per-type descriptor arguments. The template is marked noinline so the thin per-type body stays a single weak symbol the linker can deduplicate, instead of being inlined into every translation unit's static initializer. px4_fmu-v6x_default flash: -1904 B --- .../uavcan/node/global_data_type_registry.hpp | 41 +++++++------------ .../src/node/uc_global_data_type_registry.cpp | 32 +++++++++++++++ 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/drivers/uavcan/libdronecan/libuavcan/include/uavcan/node/global_data_type_registry.hpp b/src/drivers/uavcan/libdronecan/libuavcan/include/uavcan/node/global_data_type_registry.hpp index 0148ce0f509..0c9bb01b8c4 100644 --- a/src/drivers/uavcan/libdronecan/libuavcan/include/uavcan/node/global_data_type_registry.hpp +++ b/src/drivers/uavcan/libdronecan/libuavcan/include/uavcan/node/global_data_type_registry.hpp @@ -85,6 +85,14 @@ private: RegistrationResult remove(Entry* dtd); RegistrationResult registImpl(Entry* dtd); + /** + * Type-independent body of registerDataType<>(). Keeping it out of line means the + * template only has to construct the descriptor arguments, instead of expanding the + * whole registration logic once per data type. + */ + RegistrationResult registerDataTypeImpl(Entry& entry, DataTypeKind kind, DataTypeID id, + const DataTypeSignature& signature, const char* name); + public: /** * Returns the reference to the singleton. @@ -100,8 +108,13 @@ public: * libuavcan DSDL compiler from DSDL definitions. * * @param id Data Type ID for this data type. + * + * noinline: the same type is typically registered from several translation units. Keeping + * the thin per-type body a single weak symbol lets the linker deduplicate it, instead of + * having it inlined into every translation unit's static initializer. */ template + __attribute__((noinline)) RegistrationResult registerDataType(DataTypeID id); /** @@ -207,33 +220,9 @@ struct UAVCAN_EXPORT DefaultDataTypeRegistrator template GlobalDataTypeRegistry::RegistrationResult GlobalDataTypeRegistry::registerDataType(DataTypeID id) { - if (isFrozen()) - { - return RegistrationResultFrozen; - } - static Entry entry; - - { - const RegistrationResult remove_res = remove(&entry); - if (remove_res != RegistrationResultOk) - { - return remove_res; - } - } - - // We can't just overwrite the entry itself because it's noncopyable - entry.descriptor = DataTypeDescriptor(DataTypeKind(Type::DataTypeKind), id, - Type::getDataTypeSignature(), Type::getDataTypeFullName()); - - { - const RegistrationResult remove_res = remove(&entry); - if (remove_res != RegistrationResultOk) - { - return remove_res; - } - } - return registImpl(&entry); + return registerDataTypeImpl(entry, DataTypeKind(Type::DataTypeKind), id, + Type::getDataTypeSignature(), Type::getDataTypeFullName()); } } diff --git a/src/drivers/uavcan/libdronecan/libuavcan/src/node/uc_global_data_type_registry.cpp b/src/drivers/uavcan/libdronecan/libuavcan/src/node/uc_global_data_type_registry.cpp index 7586201e2bd..84390897793 100644 --- a/src/drivers/uavcan/libdronecan/libuavcan/src/node/uc_global_data_type_registry.cpp +++ b/src/drivers/uavcan/libdronecan/libuavcan/src/node/uc_global_data_type_registry.cpp @@ -125,6 +125,38 @@ GlobalDataTypeRegistry::RegistrationResult GlobalDataTypeRegistry::registImpl(En return RegistrationResultOk; } +GlobalDataTypeRegistry::RegistrationResult GlobalDataTypeRegistry::registerDataTypeImpl(Entry& entry, + DataTypeKind kind, + DataTypeID id, + const DataTypeSignature& signature, + const char* name) +{ + if (isFrozen()) + { + return RegistrationResultFrozen; + } + + { + const RegistrationResult remove_res = remove(&entry); + if (remove_res != RegistrationResultOk) + { + return remove_res; + } + } + + // We can't just overwrite the entry itself because it's noncopyable + entry.descriptor = DataTypeDescriptor(kind, id, signature, name); + + { + const RegistrationResult remove_res = remove(&entry); + if (remove_res != RegistrationResultOk) + { + return remove_res; + } + } + return registImpl(&entry); +} + GlobalDataTypeRegistry& GlobalDataTypeRegistry::instance() { static GlobalDataTypeRegistry singleton;