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;