refactor(drivers/uavcan): de-templatize GlobalDataTypeRegistry::registerDataType

registerDataType<Type> 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
This commit is contained in:
Balduin committed 2026-10-12 17:21:18 +02:00
1 parent 15505c9f2e
commit f6249b453c
2 files changed
+47 -26

No files matched your search

@@ -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 <typename Type>
__attribute__((noinline))
RegistrationResult registerDataType(DataTypeID id);
/**
@@ -207,33 +220,9 @@ struct UAVCAN_EXPORT DefaultDataTypeRegistrator
template <typename Type>
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());
}
}
@@ -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;